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.bzip2;
20
21 import java.util.LinkedHashMap;
22 import java.util.Map;
23 import org.apache.commons.compress.compressors.FileNameUtil;
24
25 /**
26 * Utility code for the BZip2 compression format.
27 * @ThreadSafe
28 * @since 1.1
29 */
30 public abstract class BZip2Utils {
31
32 private static final FileNameUtil fileNameUtil;
33
34 static {
35 Map<String, String> uncompressSuffix =
36 new LinkedHashMap<String, String>();
37 // backwards compatibilty: BZip2Utils never created the short
38 // tbz form, so .tar.bz2 has to be added explicitly
39 uncompressSuffix.put(".tar.bz2", ".tar");
40 uncompressSuffix.put(".tbz2", ".tar");
41 uncompressSuffix.put(".tbz", ".tar");
42 uncompressSuffix.put(".bz2", "");
43 uncompressSuffix.put(".bz", "");
44 fileNameUtil = new FileNameUtil(uncompressSuffix, ".bz2");
45 }
46
47 /** Private constructor to prevent instantiation of this utility class. */
48 private BZip2Utils() {
49 }
50
51 /**
52 * Detects common bzip2 suffixes in the given filename.
53 *
54 * @param filename name of a file
55 * @return {@code true} if the filename has a common bzip2 suffix,
56 * {@code false} otherwise
57 */
58 public static boolean isCompressedFilename(String filename) {
59 return fileNameUtil.isCompressedFilename(filename);
60 }
61
62 /**
63 * Maps the given name of a bzip2-compressed file to the name that the
64 * file should have after uncompression. Commonly used file type specific
65 * suffixes like ".tbz" or ".tbz2" are automatically detected and
66 * correctly mapped. For example the name "package.tbz2" is mapped to
67 * "package.tar". And any filenames with the generic ".bz2" suffix
68 * (or any other generic bzip2 suffix) is mapped to a name without that
69 * suffix. If no bzip2 suffix is detected, then the filename is returned
70 * unmapped.
71 *
72 * @param filename name of a file
73 * @return name of the corresponding uncompressed file
74 */
75 public static String getUncompressedFilename(String filename) {
76 return fileNameUtil.getUncompressedFilename(filename);
77 }
78
79 /**
80 * Maps the given filename to the name that the file should have after
81 * compression with bzip2. Currently this method simply appends the suffix
82 * ".bz2" to the filename based on the standard behaviour of the "bzip2"
83 * program, but a future version may implement a more complex mapping if
84 * a new widely used naming pattern emerges.
85 *
86 * @param filename name of a file
87 * @return name of the corresponding compressed file
88 */
89 public static String getCompressedFilename(String filename) {
90 return fileNameUtil.getCompressedFilename(filename);
91 }
92
93 }