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