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 * @ThreadSafe
031 */
032public class GzipUtils {
033
034    private static final FileNameUtil fileNameUtil;
035
036    static {
037        // using LinkedHashMap so .tgz is preferred over .taz as
038        // compressed extension of .tar as FileNameUtil will use the
039        // first one found
040        final Map<String, String> uncompressSuffix =
041            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
063     * compression with gzip. Common file types with custom suffixes for
064     * compressed versions are automatically detected and correctly mapped.
065     * For example the name "package.tar" is mapped to "package.tgz". If no
066     * custom mapping is applicable, then the default ".gz" suffix is appended
067     * to the file name.
068     *
069     * @param fileName name of a file
070     * @return name of the corresponding compressed file
071     * @deprecated Use {@link #getCompressedFileName(String)}.
072     */
073    @Deprecated
074    public static String getCompressedFilename(final String fileName) {
075        return fileNameUtil.getCompressedFileName(fileName);
076    }
077
078    /**
079     * Maps the given file name to the name that the file should have after
080     * compression with gzip. Common file types with custom suffixes for
081     * compressed versions are automatically detected and correctly mapped.
082     * For example the name "package.tar" is mapped to "package.tgz". If no
083     * custom mapping is applicable, then the default ".gz" suffix is appended
084     * to the file name.
085     *
086     * @param fileName name of a file
087     * @return name of the corresponding compressed file
088     * @since 1.25.0
089     */
090    public static String getCompressedFileName(final String fileName) {
091        return fileNameUtil.getCompressedFileName(fileName);
092    }
093
094    /**
095     * Maps the given name of a gzip-compressed file to the name that the
096     * file should have after uncompression. Commonly used file type specific
097     * suffixes like ".tgz" or ".svgz" are automatically detected and
098     * correctly mapped. For example the name "package.tgz" is mapped to
099     * "package.tar". And any file names with the generic ".gz" suffix
100     * (or any other generic gzip suffix) is mapped to a name without that
101     * suffix. If no gzip suffix is detected, then the file name is returned
102     * unmapped.
103     *
104     * @param fileName name of a file
105     * @return name of the corresponding uncompressed file
106     * @deprecated Use {@link #getUncompressedFileName(String)}.
107     */
108    @Deprecated
109    public static String getUncompressedFilename(final String fileName) {
110        return fileNameUtil.getUncompressedFileName(fileName);
111    }
112
113    /**
114     * Maps the given name of a gzip-compressed file to the name that the
115     * file should have after uncompression. Commonly used file type specific
116     * suffixes like ".tgz" or ".svgz" are automatically detected and
117     * correctly mapped. For example the name "package.tgz" is mapped to
118     * "package.tar". And any file names with the generic ".gz" suffix
119     * (or any other generic gzip suffix) is mapped to a name without that
120     * suffix. If no gzip suffix is detected, then the file name is returned
121     * unmapped.
122     *
123     * @param fileName name of a file
124     * @return name of the corresponding uncompressed file
125     * @since 1.25.0
126     */
127    public static String getUncompressedFileName(final String fileName) {
128        return fileNameUtil.getUncompressedFileName(fileName);
129    }
130
131    /**
132     * Detects common gzip suffixes in the given file name.
133     *
134     * @param fileName name of a file
135     * @return {@code true} if the file name has a common gzip suffix,
136     *         {@code false} otherwise
137     * @deprecated Use {@link #isCompressedFileName(String)}.
138     */
139    @Deprecated
140    public static boolean isCompressedFilename(final String fileName) {
141        return fileNameUtil.isCompressedFileName(fileName);
142    }
143
144    /**
145     * Detects common gzip suffixes in the given file name.
146     *
147     * @param fileName name of a file
148     * @return {@code true} if the file name has a common gzip suffix,
149     *         {@code false} otherwise
150     * @since 1.25.0
151     */
152    public static boolean isCompressedFileName(final String fileName) {
153        return fileNameUtil.isCompressedFileName(fileName);
154    }
155
156    /** Private constructor to prevent instantiation of this utility class. */
157    private GzipUtils() {
158    }
159
160}