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