GzipUtils.java

  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. import java.nio.charset.Charset;
  21. import java.nio.charset.StandardCharsets;
  22. import java.util.LinkedHashMap;
  23. import java.util.Map;

  24. import org.apache.commons.compress.compressors.FileNameUtil;

  25. /**
  26.  * Utility code for the gzip compression format.
  27.  *
  28.  * @ThreadSafe
  29.  */
  30. public class GzipUtils {

  31.     private static final FileNameUtil fileNameUtil;

  32.     static {
  33.         // using LinkedHashMap so .tgz is preferred over .taz as
  34.         // compressed extension of .tar as FileNameUtil will use the
  35.         // first one found
  36.         final Map<String, String> uncompressSuffix = new LinkedHashMap<>();
  37.         uncompressSuffix.put(".tgz", ".tar");
  38.         uncompressSuffix.put(".taz", ".tar");
  39.         uncompressSuffix.put(".svgz", ".svg");
  40.         uncompressSuffix.put(".cpgz", ".cpio");
  41.         uncompressSuffix.put(".wmz", ".wmf");
  42.         uncompressSuffix.put(".emz", ".emf");
  43.         uncompressSuffix.put(".gz", "");
  44.         uncompressSuffix.put(".z", "");
  45.         uncompressSuffix.put("-gz", "");
  46.         uncompressSuffix.put("-z", "");
  47.         uncompressSuffix.put("_z", "");
  48.         fileNameUtil = new FileNameUtil(uncompressSuffix, ".gz");
  49.     }

  50.     /**
  51.      * Encoding for file name and comments per the <a href="https://tools.ietf.org/html/rfc1952">GZIP File Format Specification</a>
  52.      */
  53.     static final Charset GZIP_ENCODING = StandardCharsets.ISO_8859_1;

  54.     /**
  55.      * 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
  56.      * versions are automatically detected and correctly mapped. For example the name "package.tar" is mapped to "package.tgz". If no custom mapping is
  57.      * applicable, then the default ".gz" suffix is appended to the file name.
  58.      *
  59.      * @param fileName name of a file
  60.      * @return name of the corresponding compressed file
  61.      * @deprecated Use {@link #getCompressedFileName(String)}.
  62.      */
  63.     @Deprecated
  64.     public static String getCompressedFilename(final String fileName) {
  65.         return fileNameUtil.getCompressedFileName(fileName);
  66.     }

  67.     /**
  68.      * 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
  69.      * versions are automatically detected and correctly mapped. For example the name "package.tar" is mapped to "package.tgz". If no custom mapping is
  70.      * applicable, then the default ".gz" suffix is appended to the file name.
  71.      *
  72.      * @param fileName name of a file
  73.      * @return name of the corresponding compressed file
  74.      * @since 1.25.0
  75.      */
  76.     public static String getCompressedFileName(final String fileName) {
  77.         return fileNameUtil.getCompressedFileName(fileName);
  78.     }

  79.     /**
  80.      * 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
  81.      * ".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
  82.      * 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
  83.      * returned unmapped.
  84.      *
  85.      * @param fileName name of a file
  86.      * @return name of the corresponding uncompressed file
  87.      * @deprecated Use {@link #getUncompressedFileName(String)}.
  88.      */
  89.     @Deprecated
  90.     public static String getUncompressedFilename(final String fileName) {
  91.         return fileNameUtil.getUncompressedFileName(fileName);
  92.     }

  93.     /**
  94.      * 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
  95.      * ".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
  96.      * 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
  97.      * returned unmapped.
  98.      *
  99.      * @param fileName name of a file
  100.      * @return name of the corresponding uncompressed file
  101.      * @since 1.25.0
  102.      */
  103.     public static String getUncompressedFileName(final String fileName) {
  104.         return fileNameUtil.getUncompressedFileName(fileName);
  105.     }

  106.     /**
  107.      * Detects common gzip suffixes in the given file name.
  108.      *
  109.      * @param fileName name of a file
  110.      * @return {@code true} if the file name has a common gzip suffix, {@code false} otherwise
  111.      * @deprecated Use {@link #isCompressedFileName(String)}.
  112.      */
  113.     @Deprecated
  114.     public static boolean isCompressedFilename(final String fileName) {
  115.         return fileNameUtil.isCompressedFileName(fileName);
  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.      * @since 1.25.0
  123.      */
  124.     public static boolean isCompressedFileName(final String fileName) {
  125.         return fileNameUtil.isCompressedFileName(fileName);
  126.     }

  127.     /** Private constructor to prevent instantiation of this utility class. */
  128.     private GzipUtils() {
  129.     }

  130. }