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