1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 package org.apache.commons.compress.compressors.gzip; 20 21 import java.nio.charset.Charset; 22 import java.nio.charset.StandardCharsets; 23 import java.util.LinkedHashMap; 24 import java.util.Map; 25 26 import org.apache.commons.compress.compressors.FileNameUtil; 27 28 /** 29 * Utility code for the gzip compression format. 30 * 31 * @ThreadSafe 32 */ 33 public class GzipUtils { 34 35 private static final FileNameUtil fileNameUtil; 36 37 static { 38 // using LinkedHashMap so .tgz is preferred over .taz as 39 // compressed extension of .tar as FileNameUtil will use the 40 // first one found 41 final Map<String, String> uncompressSuffix = new LinkedHashMap<>(); 42 uncompressSuffix.put(".tgz", ".tar"); 43 uncompressSuffix.put(".taz", ".tar"); 44 uncompressSuffix.put(".svgz", ".svg"); 45 uncompressSuffix.put(".cpgz", ".cpio"); 46 uncompressSuffix.put(".wmz", ".wmf"); 47 uncompressSuffix.put(".emz", ".emf"); 48 uncompressSuffix.put(".gz", ""); 49 uncompressSuffix.put(".z", ""); 50 uncompressSuffix.put("-gz", ""); 51 uncompressSuffix.put("-z", ""); 52 uncompressSuffix.put("_z", ""); 53 fileNameUtil = new FileNameUtil(uncompressSuffix, ".gz"); 54 } 55 56 /** 57 * Encoding for file name and comments per the <a href="https://tools.ietf.org/html/rfc1952">GZIP File Format Specification</a> 58 */ 59 static final Charset GZIP_ENCODING = StandardCharsets.ISO_8859_1; 60 61 /** 62 * Maps the given file name to the name that the file should have after compression with gzip. Common file types with custom suffixes for compressed 63 * versions are automatically detected and correctly mapped. For example the name "package.tar" is mapped to "package.tgz". If no custom mapping is 64 * applicable, then the default ".gz" suffix is appended to the file name. 65 * 66 * @param fileName name of a file 67 * @return name of the corresponding compressed file 68 * @deprecated Use {@link #getCompressedFileName(String)}. 69 */ 70 @Deprecated 71 public static String getCompressedFilename(final String fileName) { 72 return fileNameUtil.getCompressedFileName(fileName); 73 } 74 75 /** 76 * Maps the given file name to the name that the file should have after compression with gzip. Common file types with custom suffixes for compressed 77 * versions are automatically detected and correctly mapped. For example the name "package.tar" is mapped to "package.tgz". If no custom mapping is 78 * applicable, then the default ".gz" suffix is appended to the file name. 79 * 80 * @param fileName name of a file 81 * @return name of the corresponding compressed file 82 * @since 1.25.0 83 */ 84 public static String getCompressedFileName(final String fileName) { 85 return fileNameUtil.getCompressedFileName(fileName); 86 } 87 88 /** 89 * Maps the given name of a gzip-compressed file to the name that the file should have after uncompression. Commonly used file type specific suffixes like 90 * ".tgz" or ".svgz" are automatically detected and correctly mapped. For example the name "package.tgz" is mapped to "package.tar". And any file names with 91 * the generic ".gz" suffix (or any other generic gzip suffix) is mapped to a name without that suffix. If no gzip suffix is detected, then the file name is 92 * returned unmapped. 93 * 94 * @param fileName name of a file 95 * @return name of the corresponding uncompressed file 96 * @deprecated Use {@link #getUncompressedFileName(String)}. 97 */ 98 @Deprecated 99 public static String getUncompressedFilename(final String fileName) { 100 return fileNameUtil.getUncompressedFileName(fileName); 101 } 102 103 /** 104 * Maps the given name of a gzip-compressed file to the name that the file should have after uncompression. Commonly used file type specific suffixes like 105 * ".tgz" or ".svgz" are automatically detected and correctly mapped. For example the name "package.tgz" is mapped to "package.tar". And any file names with 106 * the generic ".gz" suffix (or any other generic gzip suffix) is mapped to a name without that suffix. If no gzip suffix is detected, then the file name is 107 * returned unmapped. 108 * 109 * @param fileName name of a file 110 * @return name of the corresponding uncompressed file 111 * @since 1.25.0 112 */ 113 public static String getUncompressedFileName(final String fileName) { 114 return fileNameUtil.getUncompressedFileName(fileName); 115 } 116 117 /** 118 * Detects common gzip suffixes in the given file name. 119 * 120 * @param fileName name of a file 121 * @return {@code true} if the file name has a common gzip suffix, {@code false} otherwise 122 * @deprecated Use {@link #isCompressedFileName(String)}. 123 */ 124 @Deprecated 125 public static boolean isCompressedFilename(final String fileName) { 126 return fileNameUtil.isCompressedFileName(fileName); 127 } 128 129 /** 130 * Detects common gzip suffixes in the given file name. 131 * 132 * @param fileName name of a file 133 * @return {@code true} if the file name has a common gzip suffix, {@code false} otherwise 134 * @since 1.25.0 135 */ 136 public static boolean isCompressedFileName(final String fileName) { 137 return fileNameUtil.isCompressedFileName(fileName); 138 } 139 140 /** Private constructor to prevent instantiation of this utility class. */ 141 private GzipUtils() { 142 } 143 144 }