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