    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one
    003 * or more contributor license agreements.  See the NOTICE file
    004 * distributed with this work for additional information
    005 * regarding copyright ownership.  The ASF licenses this file
    006 * to you under the Apache License, Version 2.0 (the
    007 * "License"); you may not use this file except in compliance
    008 * with the License.  You may obtain a copy of the License at
    009 *
    010 * http://www.apache.org/licenses/LICENSE-2.0
    011 *
    012 * Unless required by applicable law or agreed to in writing,
    013 * software distributed under the License is distributed on an
    014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    015 * KIND, either express or implied.  See the License for the
    016 * specific language governing permissions and limitations
    017 * under the License.
    018 */
    019package org.apache.commons.compress.compressors.gzip;
    020
    021import java.nio.charset.Charset;
    022import java.nio.charset.StandardCharsets;
    023import java.util.LinkedHashMap;
    024import java.util.Map;
    025
    026import org.apache.commons.compress.compressors.FileNameUtil;
    027
    028/**
    029 * Utility code for the gzip compression format.
    030 *
    031 * @ThreadSafe
    032 */
    033public class GzipUtils {
    034
    035    private static final FileNameUtil fileNameUtil;
    036
    037    static {
    038        // using LinkedHashMap so .tgz is preferred over .taz as
    039        // compressed extension of .tar as FileNameUtil will use the
    040        // first one found
    041        final Map<String, String> uncompressSuffix = new LinkedHashMap<>();
    042        uncompressSuffix.put(".tgz", ".tar");
    043        uncompressSuffix.put(".taz", ".tar");
    044        uncompressSuffix.put(".svgz", ".svg");
    045        uncompressSuffix.put(".cpgz", ".cpio");
    046        uncompressSuffix.put(".wmz", ".wmf");
    047        uncompressSuffix.put(".emz", ".emf");
    048        uncompressSuffix.put(".gz", "");
    049        uncompressSuffix.put(".z", "");
    050        uncompressSuffix.put("-gz", "");
    051        uncompressSuffix.put("-z", "");
    052        uncompressSuffix.put("_z", "");
    053        fileNameUtil = new FileNameUtil(uncompressSuffix, ".gz");
    054    }
    055
    056    /**
    057     * Encoding for file name and comments per the <a href="https://tools.ietf.org/html/rfc1952">GZIP File Format Specification</a>
    058     */
    059    static final Charset GZIP_ENCODING = StandardCharsets.ISO_8859_1;
    060
    061    /**
    062     * Maps the given file name to the name that the file should have after compression with gzip. Common file types with custom suffixes for compressed
    063     * versions are automatically detected and correctly mapped. For example the name "package.tar" is mapped to "package.tgz". If no custom mapping is
    064     * applicable, then the default ".gz" suffix is appended to the file name.
    065     *
    066     * @param fileName name of a file
    067     * @return name of the corresponding compressed file
    068     * @deprecated Use {@link #getCompressedFileName(String)}.
    069     */
    070    @Deprecated
    071    public static String getCompressedFilename(final String fileName) {
    072        return fileNameUtil.getCompressedFileName(fileName);
    073    }
    074
    075    /**
    076     * Maps the given file name to the name that the file should have after compression with gzip. Common file types with custom suffixes for compressed
    077     * versions are automatically detected and correctly mapped. For example the name "package.tar" is mapped to "package.tgz". If no custom mapping is
    078     * applicable, then the default ".gz" suffix is appended to the file name.
    079     *
    080     * @param fileName name of a file
    081     * @return name of the corresponding compressed file
    082     * @since 1.25.0
    083     */
    084    public static String getCompressedFileName(final String fileName) {
    085        return fileNameUtil.getCompressedFileName(fileName);
    086    }
    087
    088    /**
    089     * Maps the given name of a gzip-compressed file to the name that the file should have after uncompression. Commonly used file type specific suffixes like
    090     * ".tgz" or ".svgz" are automatically detected and correctly mapped. For example the name "package.tgz" is mapped to "package.tar". And any file names with
    091     * the generic ".gz" suffix (or any other generic gzip suffix) is mapped to a name without that suffix. If no gzip suffix is detected, then the file name is
    092     * returned unmapped.
    093     *
    094     * @param fileName name of a file
    095     * @return name of the corresponding uncompressed file
    096     * @deprecated Use {@link #getUncompressedFileName(String)}.
    097     */
    098    @Deprecated
    099    public static String getUncompressedFilename(final String fileName) {
    100        return fileNameUtil.getUncompressedFileName(fileName);
    101    }
    102
    103    /**
    104     * Maps the given name of a gzip-compressed file to the name that the file should have after uncompression. Commonly used file type specific suffixes like
    105     * ".tgz" or ".svgz" are automatically detected and correctly mapped. For example the name "package.tgz" is mapped to "package.tar". And any file names with
    106     * the generic ".gz" suffix (or any other generic gzip suffix) is mapped to a name without that suffix. If no gzip suffix is detected, then the file name is
    107     * returned unmapped.
    108     *
    109     * @param fileName name of a file
    110     * @return name of the corresponding uncompressed file
    111     * @since 1.25.0
    112     */
    113    public static String getUncompressedFileName(final String fileName) {
    114        return fileNameUtil.getUncompressedFileName(fileName);
    115    }
    116
    117    /**
    118     * Detects common gzip suffixes in the given file name.
    119     *
    120     * @param fileName name of a file
    121     * @return {@code true} if the file name has a common gzip suffix, {@code false} otherwise
    122     * @deprecated Use {@link #isCompressedFileName(String)}.
    123     */
    124    @Deprecated
    125    public static boolean isCompressedFilename(final String fileName) {
    126        return fileNameUtil.isCompressedFileName(fileName);
    127    }
    128
    129    /**
    130     * Detects common gzip suffixes in the given file name.
    131     *
    132     * @param fileName name of a file
    133     * @return {@code true} if the file name has a common gzip suffix, {@code false} otherwise
    134     * @since 1.25.0
    135     */
    136    public static boolean isCompressedFileName(final String fileName) {
    137        return fileNameUtil.isCompressedFileName(fileName);
    138    }
    139
    140    /** Private constructor to prevent instantiation of this utility class. */
    141    private GzipUtils() {
    142    }
    143
    144}