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     */
019    package org.apache.commons.compress.compressors.gzip;
020    
021    import java.util.HashMap;
022    import java.util.Locale;
023    import java.util.Map;
024    
025    /**
026     * Utility code for the gzip compression format.
027     * @ThreadSafe
028     */
029    public class GzipUtils {
030    
031        /**
032         * Map from common filename suffixes to the suffixes that identify gzipped
033         * versions of those file types. For example: from ".tar" to ".tgz".
034         */
035        private static final Map compressSuffix = new HashMap();
036    
037        /**
038         * Map from common filename suffixes of gzipped files to the corresponding
039         * suffixes of uncompressed files. For example: from ".tgz" to ".tar".
040         * <p>
041         * This map also contains gzip-specific suffixes like ".gz" and "-z".
042         * These suffixes are mapped to the empty string, as they should simply
043         * be removed from the filename when the file is uncompressed.
044         */
045        private static final Map uncompressSuffix = new HashMap();
046    
047        static {
048            compressSuffix.put(".tar", ".tgz");
049            compressSuffix.put(".svg", ".svgz");
050            compressSuffix.put(".cpio", ".cpgz");
051            compressSuffix.put(".wmf", ".wmz");
052            compressSuffix.put(".emf", ".emz");
053    
054            uncompressSuffix.put(".tgz", ".tar");
055            uncompressSuffix.put(".taz", ".tar");
056            uncompressSuffix.put(".svgz", ".svg");
057            uncompressSuffix.put(".cpgz", ".cpio");
058            uncompressSuffix.put(".wmz", ".wmf");
059            uncompressSuffix.put(".emz", ".emf");
060            uncompressSuffix.put(".gz", "");
061            uncompressSuffix.put(".z", "");
062            uncompressSuffix.put("-gz", "");
063            uncompressSuffix.put("-z", "");
064            uncompressSuffix.put("_z", "");
065        }
066        // N.B. if any shorter or longer keys are added, ensure the for loop limits are changed
067    
068        /** Private constructor to prevent instantiation of this utility class. */
069        private GzipUtils() {
070        }
071    
072        /**
073         * Detects common gzip suffixes in the given filename.
074         *
075         * @param filename name of a file
076         * @return <code>true</code> if the filename has a common gzip suffix,
077         *         <code>false</code> otherwise
078         */
079        public static boolean isCompressedFilename(String filename) {
080            String lower = filename.toLowerCase(Locale.ENGLISH);
081            int n = lower.length();
082            // Shortest suffix is two letters (_z), longest is five (.svgz)
083            for (int i = 2; i <= 5 && i < n; i++) {
084                if (uncompressSuffix.containsKey(lower.substring(n - i))) {
085                    return true;
086                }
087            }
088            return false;
089        }
090    
091        /**
092         * Maps the given name of a gzip-compressed file to the name that the
093         * file should have after uncompression. Commonly used file type specific
094         * suffixes like ".tgz" or ".svgz" are automatically detected and
095         * correctly mapped. For example the name "package.tgz" is mapped to
096         * "package.tar". And any filenames with the generic ".gz" suffix
097         * (or any other generic gzip suffix) is mapped to a name without that
098         * suffix. If no gzip suffix is detected, then the filename is returned
099         * unmapped.
100         *
101         * @param filename name of a file
102         * @return name of the corresponding uncompressed file
103         */
104        public static String getUncompressedFilename(String filename) {
105            String lower = filename.toLowerCase(Locale.ENGLISH);
106            int n = lower.length();
107            // Shortest suffix is two letters (_z), longest is five (.svgz)
108            for (int i = 2; i <= 5 && i < n; i++) {
109                Object suffix = uncompressSuffix.get(lower.substring(n - i));
110                if (suffix != null) {
111                    return filename.substring(0, n - i) + suffix;
112                }
113            }
114            return filename;
115        }
116    
117        /**
118         * Maps the given filename to the name that the file should have after
119         * compression with gzip. Common file types with custom suffixes for
120         * compressed versions are automatically detected and correctly mapped.
121         * For example the name "package.tar" is mapped to "package.tgz". If no
122         * custom mapping is applicable, then the default ".gz" suffix is appended
123         * to the filename.
124         *
125         * @param filename name of a file
126         * @return name of the corresponding compressed file
127         */
128        public static String getCompressedFilename(String filename) {
129            String lower = filename.toLowerCase(Locale.ENGLISH);
130            int n = lower.length();
131            // Shortest suffix is four letters (.svg), longest is five (.cpio)
132            for (int i = 4; i <= 5 && i < n; i++) {
133                Object suffix = compressSuffix.get(lower.substring(n - i));
134                if (suffix != null) {
135                    return filename.substring(0, n - i) + suffix;
136                }
137            }
138            // No custom suffix found, just append the default .gz
139            return filename + ".gz";
140        }
141    
142    }