    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;
    023
    024import org.apache.commons.compress.compressors.FileNameUtil;
    025
    026/**
    027 * Utility code for the BZip2 compression format.
    028 *
    029 * @ThreadSafe
    030 * @since 1.1
    031 */
    032public abstract class BZip2Utils {
    033
    034    private static final FileNameUtil fileNameUtil;
    035
    036    static {
    037        final Map<String, String> uncompressSuffix = new LinkedHashMap<>();
    038        // backwards compatibility: BZip2Utils never created the short
    039        // tbz form, so .tar.bz2 has to be added explicitly
    040        uncompressSuffix.put(".tar.bz2", ".tar");
    041        uncompressSuffix.put(".tbz2", ".tar");
    042        uncompressSuffix.put(".tbz", ".tar");
    043        uncompressSuffix.put(".bz2", "");
    044        uncompressSuffix.put(".bz", "");
    045        fileNameUtil = new FileNameUtil(uncompressSuffix, ".bz2");
    046    }
    047
    048    /**
    049     * Maps the given file name to the name that the file should have after compression with bzip2. Currently this method simply appends the suffix ".bz2" to
    050     * the file name based on the standard behavior of the "bzip2" program, but a future version may implement a more complex mapping if a new widely used
    051     * naming pattern emerges.
    052     *
    053     * @param fileName name of a file
    054     * @return name of the corresponding compressed file
    055     * @deprecated Use {@link #getCompressedFileName(String)}.
    056     */
    057    @Deprecated
    058    public static String getCompressedFilename(final String fileName) {
    059        return fileNameUtil.getCompressedFileName(fileName);
    060    }
    061
    062    /**
    063     * Maps the given file name to the name that the file should have after compression with bzip2. Currently this method simply appends the suffix ".bz2" to
    064     * the file name based on the standard behavior of the "bzip2" program, but a future version may implement a more complex mapping if a new widely used
    065     * naming pattern emerges.
    066     *
    067     * @param fileName name of a file
    068     * @return name of the corresponding compressed file
    069     * @since 1.25.0
    070     */
    071    public static String getCompressedFileName(final String fileName) {
    072        return fileNameUtil.getCompressedFileName(fileName);
    073    }
    074
    075    /**
    076     * Maps the given name of a bzip2-compressed file to the name that the file should have after uncompression. Commonly used file type specific suffixes like
    077     * ".tbz" or ".tbz2" are automatically detected and correctly mapped. For example the name "package.tbz2" is mapped to "package.tar". And any file names
    078     * with the generic ".bz2" suffix (or any other generic bzip2 suffix) is mapped to a name without that suffix. If no bzip2 suffix is detected, then the file
    079     * name is returned unmapped.
    080     *
    081     * @param fileName name of a file
    082     * @return name of the corresponding uncompressed file
    083     * @deprecated Use {@link #getUncompressedFileName(String)}.
    084     */
    085    @Deprecated
    086    public static String getUncompressedFilename(final String fileName) {
    087        return fileNameUtil.getUncompressedFileName(fileName);
    088    }
    089
    090    /**
    091     * Maps the given name of a bzip2-compressed file to the name that the file should have after uncompression. Commonly used file type specific suffixes like
    092     * ".tbz" or ".tbz2" are automatically detected and correctly mapped. For example the name "package.tbz2" is mapped to "package.tar". And any file names
    093     * with the generic ".bz2" suffix (or any other generic bzip2 suffix) is mapped to a name without that suffix. If no bzip2 suffix is detected, then the file
    094     * name is returned unmapped.
    095     *
    096     * @param fileName name of a file
    097     * @return name of the corresponding uncompressed file
    098     * @since 1.25.0
    099     */
    100    public static String getUncompressedFileName(final String fileName) {
    101        return fileNameUtil.getUncompressedFileName(fileName);
    102    }
    103
    104    /**
    105     * Detects common bzip2 suffixes in the given file name.
    106     *
    107     * @param fileName name of a file
    108     * @return {@code true} if the file name has a common bzip2 suffix, {@code false} otherwise
    109     * @deprecated Use {@link #isCompressedFileName(String)}.
    110     */
    111    @Deprecated
    112    public static boolean isCompressedFilename(final String fileName) {
    113        return fileNameUtil.isCompressedFileName(fileName);
    114    }
    115
    116    /**
    117     * Detects common bzip2 suffixes in the given file name.
    118     *
    119     * @param fileName name of a file
    120     * @return {@code true} if the file name has a common bzip2 suffix, {@code false} otherwise
    121     * @since 1.25.0
    122     */
    123    public static boolean isCompressedFileName(final String fileName) {
    124        return fileNameUtil.isCompressedFileName(fileName);
    125    }
    126
    127    /** Private constructor to prevent instantiation of this utility class. */
    128    private BZip2Utils() {
    129    }
    130
    131}