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