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 */
019 package org.apache.commons.compress.compressors.bzip2;
020
021 import java.util.HashMap;
022 import java.util.Map;
023
024 /**
025 * Utility code for the BZip2 compression format.
026 * @ThreadSafe
027 * @since Commons Compress 1.1
028 */
029 public abstract class BZip2Utils {
030
031 /**
032 * Map from common filename suffixes of bzip2ed files to the corresponding
033 * suffixes of uncompressed files. For example: from ".tbz2" to ".tar".
034 * <p>
035 * This map also contains bzip2-specific suffixes like ".bz2". These
036 * suffixes are mapped to the empty string, as they should simply be
037 * removed from the filename when the file is uncompressed.
038 */
039 private static final Map uncompressSuffix = new HashMap();
040
041 static {
042 uncompressSuffix.put(".tbz2", ".tar");
043 uncompressSuffix.put(".tbz", ".tar");
044 uncompressSuffix.put(".bz2", "");
045 uncompressSuffix.put(".bz", "");
046 }
047 // N.B. if any shorter or longer keys are added, ensure the for loop limits are changed
048
049 /** Private constructor to prevent instantiation of this utility class. */
050 private BZip2Utils() {
051 }
052
053 /**
054 * Detects common bzip2 suffixes in the given filename.
055 *
056 * @param filename name of a file
057 * @return <code>true</code> if the filename has a common bzip2 suffix,
058 * <code>false</code> otherwise
059 */
060 public static boolean isCompressedFilename(String filename) {
061 String lower = filename.toLowerCase();
062 int n = lower.length();
063 // Shortest suffix is three letters (.bz), longest is five (.tbz2)
064 for (int i = 3; i <= 5 && i < n; i++) {
065 if (uncompressSuffix.containsKey(lower.substring(n - i))) {
066 return true;
067 }
068 }
069 return false;
070 }
071
072 /**
073 * Maps the given name of a bzip2-compressed file to the name that the
074 * file should have after uncompression. Commonly used file type specific
075 * suffixes like ".tbz" or ".tbz2" are automatically detected and
076 * correctly mapped. For example the name "package.tbz2" is mapped to
077 * "package.tar". And any filenames with the generic ".bz2" suffix
078 * (or any other generic bzip2 suffix) is mapped to a name without that
079 * suffix. If no bzip2 suffix is detected, then the filename is returned
080 * unmapped.
081 *
082 * @param filename name of a file
083 * @return name of the corresponding uncompressed file
084 */
085 public static String getUncompressedFilename(String filename) {
086 String lower = filename.toLowerCase();
087 int n = lower.length();
088 // Shortest suffix is three letters (.bz), longest is five (.tbz2)
089 for (int i = 3; i <= 5 && i < n; i++) {
090 Object suffix = uncompressSuffix.get(lower.substring(n - i));
091 if (suffix != null) {
092 return filename.substring(0, n - i) + suffix;
093 }
094 }
095 return filename;
096 }
097
098 /**
099 * Maps the given filename to the name that the file should have after
100 * compression with bzip2. Currently this method simply appends the suffix
101 * ".bz2" to the filename based on the standard behaviour of the "bzip2"
102 * program, but a future version may implement a more complex mapping if
103 * a new widely used naming pattern emerges.
104 *
105 * @param filename name of a file
106 * @return name of the corresponding compressed file
107 */
108 public static String getCompressedFilename(String filename) {
109 return filename + ".bz2";
110 }
111
112 }