View Javadoc
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 edu.internet2.middleware.grouperInstallerExt.org.apache.commons.compress.compressors.gzip;
20  
21  import java.util.LinkedHashMap;
22  import java.util.Map;
23  import edu.internet2.middleware.grouperInstallerExt.org.apache.commons.compress.compressors.FileNameUtil;
24  
25  /**
26   * Utility code for the gzip compression format.
27   * @ThreadSafe
28   */
29  public class GzipUtils {
30  
31      private static final FileNameUtil fileNameUtil;
32  
33      static {
34          // using LinkedHashMap so .tgz is preferred over .taz as
35          // compressed extension of .tar as FileNameUtil will use the
36          // first one found
37          Map<String, String> uncompressSuffix =
38              new LinkedHashMap<String, String>();
39          uncompressSuffix.put(".tgz", ".tar");
40          uncompressSuffix.put(".taz", ".tar");
41          uncompressSuffix.put(".svgz", ".svg");
42          uncompressSuffix.put(".cpgz", ".cpio");
43          uncompressSuffix.put(".wmz", ".wmf");
44          uncompressSuffix.put(".emz", ".emf");
45          uncompressSuffix.put(".gz", "");
46          uncompressSuffix.put(".z", "");
47          uncompressSuffix.put("-gz", "");
48          uncompressSuffix.put("-z", "");
49          uncompressSuffix.put("_z", "");
50          fileNameUtil = new FileNameUtil(uncompressSuffix, ".gz");
51      }
52  
53      /** Private constructor to prevent instantiation of this utility class. */
54      private GzipUtils() {
55      }
56  
57      /**
58       * Detects common gzip suffixes in the given filename.
59       *
60       * @param filename name of a file
61       * @return {@code true} if the filename has a common gzip suffix,
62       *         {@code false} otherwise
63       */
64      public static boolean isCompressedFilename(String filename) {
65          return fileNameUtil.isCompressedFilename(filename);
66      }
67  
68      /**
69       * Maps the given name of a gzip-compressed file to the name that the
70       * file should have after uncompression. Commonly used file type specific
71       * suffixes like ".tgz" or ".svgz" are automatically detected and
72       * correctly mapped. For example the name "package.tgz" is mapped to
73       * "package.tar". And any filenames with the generic ".gz" suffix
74       * (or any other generic gzip suffix) is mapped to a name without that
75       * suffix. If no gzip suffix is detected, then the filename is returned
76       * unmapped.
77       *
78       * @param filename name of a file
79       * @return name of the corresponding uncompressed file
80       */
81      public static String getUncompressedFilename(String filename) {
82          return fileNameUtil.getUncompressedFilename(filename);
83      }
84  
85      /**
86       * Maps the given filename to the name that the file should have after
87       * compression with gzip. Common file types with custom suffixes for
88       * compressed versions are automatically detected and correctly mapped.
89       * For example the name "package.tar" is mapped to "package.tgz". If no
90       * custom mapping is applicable, then the default ".gz" suffix is appended
91       * to the filename.
92       *
93       * @param filename name of a file
94       * @return name of the corresponding compressed file
95       */
96      public static String getCompressedFilename(String filename) {
97          return fileNameUtil.getCompressedFilename(filename);
98      }
99  
100 }