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