    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 */
    019package org.apache.commons.compress.compressors.bzip2;
    020
    021import java.util.LinkedHashMap;
    022import java.util.Map;
    023import org.apache.commons.compress.compressors.FileNameUtil;
    024
    025/**
    026 * Utility code for the BZip2 compression format.
    027 * @ThreadSafe
    028 * @since 1.1
    029 */
    030public abstract class BZip2Utils {
    031
    032    private static final FileNameUtil fileNameUtil;
    033
    034    static {
    035        final Map<String, String> uncompressSuffix =
    036            new LinkedHashMap<>();
    037        // backwards compatibilty: BZip2Utils never created the short
    038        // tbz form, so .tar.bz2 has to be added explicitly
    039        uncompressSuffix.put(".tar.bz2", ".tar");
    040        uncompressSuffix.put(".tbz2", ".tar");
    041        uncompressSuffix.put(".tbz", ".tar");
    042        uncompressSuffix.put(".bz2", "");
    043        uncompressSuffix.put(".bz", "");
    044        fileNameUtil = new FileNameUtil(uncompressSuffix, ".bz2");
    045    }
    046
    047    /** Private constructor to prevent instantiation of this utility class. */
    048    private BZip2Utils() {
    049    }
    050
    051    /**
    052     * Detects common bzip2 suffixes in the given filename.
    053     *
    054     * @param filename name of a file
    055     * @return {@code true} if the filename has a common bzip2 suffix,
    056     *         {@code false} otherwise
    057     */
    058    public static boolean isCompressedFilename(final String filename) {
    059        return fileNameUtil.isCompressedFilename(filename);
    060    }
    061
    062    /**
    063     * Maps the given name of a bzip2-compressed file to the name that the
    064     * file should have after uncompression. Commonly used file type specific
    065     * suffixes like ".tbz" or ".tbz2" are automatically detected and
    066     * correctly mapped. For example the name "package.tbz2" is mapped to
    067     * "package.tar". And any filenames with the generic ".bz2" suffix
    068     * (or any other generic bzip2 suffix) is mapped to a name without that
    069     * suffix. If no bzip2 suffix is detected, then the filename is returned
    070     * unmapped.
    071     *
    072     * @param filename name of a file
    073     * @return name of the corresponding uncompressed file
    074     */
    075    public static String getUncompressedFilename(final String filename) {
    076        return fileNameUtil.getUncompressedFilename(filename);
    077    }
    078
    079    /**
    080     * Maps the given filename to the name that the file should have after
    081     * compression with bzip2. Currently this method simply appends the suffix
    082     * ".bz2" to the filename based on the standard behaviour of the "bzip2"
    083     * program, but a future version may implement a more complex mapping if
    084     * a new widely used naming pattern emerges.
    085     *
    086     * @param filename name of a file
    087     * @return name of the corresponding compressed file
    088     */
    089    public static String getCompressedFilename(final String filename) {
    090        return fileNameUtil.getCompressedFilename(filename);
    091    }
    092
    093}