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.util.LinkedHashMap;
022import java.util.Map;
023
024import org.apache.commons.compress.compressors.FileNameUtil;
025
026/**
027 * Utility code for the gzip compression format.
028 * @ThreadSafe
029 */
030public class GzipUtils {
031
032    private static final FileNameUtil fileNameUtil;
033
034    static {
035        // using LinkedHashMap so .tgz is preferred over .taz as
036        // compressed extension of .tar as FileNameUtil will use the
037        // first one found
038        final Map<String, String> uncompressSuffix =
039            new LinkedHashMap<>();
040        uncompressSuffix.put(".tgz", ".tar");
041        uncompressSuffix.put(".taz", ".tar");
042        uncompressSuffix.put(".svgz", ".svg");
043        uncompressSuffix.put(".cpgz", ".cpio");
044        uncompressSuffix.put(".wmz", ".wmf");
045        uncompressSuffix.put(".emz", ".emf");
046        uncompressSuffix.put(".gz", "");
047        uncompressSuffix.put(".z", "");
048        uncompressSuffix.put("-gz", "");
049        uncompressSuffix.put("-z", "");
050        uncompressSuffix.put("_z", "");
051        fileNameUtil = new FileNameUtil(uncompressSuffix, ".gz");
052    }
053
054    /** Private constructor to prevent instantiation of this utility class. */
055    private GzipUtils() {
056    }
057
058    /**
059     * Detects common gzip suffixes in the given file name.
060     *
061     * @param fileName name of a file
062     * @return {@code true} if the file name has a common gzip suffix,
063     *         {@code false} otherwise
064     */
065    public static boolean isCompressedFilename(final String fileName) {
066        return fileNameUtil.isCompressedFilename(fileName);
067    }
068
069    /**
070     * Maps the given name of a gzip-compressed file to the name that the
071     * file should have after uncompression. Commonly used file type specific
072     * suffixes like ".tgz" or ".svgz" are automatically detected and
073     * correctly mapped. For example the name "package.tgz" is mapped to
074     * "package.tar". And any file names with the generic ".gz" suffix
075     * (or any other generic gzip suffix) is mapped to a name without that
076     * suffix. If no gzip suffix is detected, then the file name is returned
077     * unmapped.
078     *
079     * @param fileName name of a file
080     * @return name of the corresponding uncompressed file
081     */
082    public static String getUncompressedFilename(final String fileName) {
083        return fileNameUtil.getUncompressedFilename(fileName);
084    }
085
086    /**
087     * Maps the given file name to the name that the file should have after
088     * compression with gzip. Common file types with custom suffixes for
089     * compressed versions are automatically detected and correctly mapped.
090     * For example the name "package.tar" is mapped to "package.tgz". If no
091     * custom mapping is applicable, then the default ".gz" suffix is appended
092     * to the file name.
093     *
094     * @param fileName name of a file
095     * @return name of the corresponding compressed file
096     */
097    public static String getCompressedFilename(final String fileName) {
098        return fileNameUtil.getCompressedFilename(fileName);
099    }
100
101}