    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 * @ThreadSafe
    029 * @since 1.1
    030 */
    031public abstract class BZip2Utils {
    032
    033    private static final FileNameUtil fileNameUtil;
    034
    035    static {
    036        final Map<String, String> uncompressSuffix =
    037            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
    050     * compression with bzip2. Currently this method simply appends the suffix
    051     * ".bz2" to the file name based on the standard behavior of the "bzip2"
    052     * program, but a future version may implement a more complex mapping if
    053     * a new widely used naming pattern emerges.
    054     *
    055     * @param fileName name of a file
    056     * @return name of the corresponding compressed file
    057     * @deprecated Use {@link #getCompressedFileName(String)}.
    058     */
    059    @Deprecated
    060    public static String getCompressedFilename(final String fileName) {
    061        return fileNameUtil.getCompressedFileName(fileName);
    062    }
    063
    064    /**
    065     * Maps the given file name to the name that the file should have after
    066     * compression with bzip2. Currently this method simply appends the suffix
    067     * ".bz2" to the file name based on the standard behavior of the "bzip2"
    068     * program, but a future version may implement a more complex mapping if
    069     * a new widely used naming pattern emerges.
    070     *
    071     * @param fileName name of a file
    072     * @return name of the corresponding compressed file
    073     * @since 1.25.0
    074     */
    075    public static String getCompressedFileName(final String fileName) {
    076        return fileNameUtil.getCompressedFileName(fileName);
    077    }
    078
    079    /**
    080     * Maps the given name of a bzip2-compressed file to the name that the
    081     * file should have after uncompression. Commonly used file type specific
    082     * suffixes like ".tbz" or ".tbz2" are automatically detected and
    083     * correctly mapped. For example the name "package.tbz2" is mapped to
    084     * "package.tar". And any file names with the generic ".bz2" suffix
    085     * (or any other generic bzip2 suffix) is mapped to a name without that
    086     * suffix. If no bzip2 suffix is detected, then the file name is returned
    087     * unmapped.
    088     *
    089     * @param fileName name of a file
    090     * @return name of the corresponding uncompressed file
    091     * @deprecated Use {@link #getUncompressedFileName(String)}.
    092     */
    093    @Deprecated
    094    public static String getUncompressedFilename(final String fileName) {
    095        return fileNameUtil.getUncompressedFileName(fileName);
    096    }
    097
    098    /**
    099     * Maps the given name of a bzip2-compressed file to the name that the
    100     * file should have after uncompression. Commonly used file type specific
    101     * suffixes like ".tbz" or ".tbz2" are automatically detected and
    102     * correctly mapped. For example the name "package.tbz2" is mapped to
    103     * "package.tar". And any file names with the generic ".bz2" suffix
    104     * (or any other generic bzip2 suffix) is mapped to a name without that
    105     * suffix. If no bzip2 suffix is detected, then the file name is returned
    106     * unmapped.
    107     *
    108     * @param fileName name of a file
    109     * @return name of the corresponding uncompressed file
    110     * @since 1.25.0
    111     */
    112    public static String getUncompressedFileName(final String fileName) {
    113        return fileNameUtil.getUncompressedFileName(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,
    121     *         {@code false} otherwise
    122     * @deprecated Use {@link #isCompressedFileName(String)}.
    123     */
    124    @Deprecated
    125    public static boolean isCompressedFilename(final String fileName) {
    126        return fileNameUtil.isCompressedFileName(fileName);
    127    }
    128
    129    /**
    130     * Detects common bzip2 suffixes in the given file name.
    131     *
    132     * @param fileName name of a file
    133     * @return {@code true} if the file name has a common bzip2 suffix,
    134     *         {@code false} otherwise
    135     * @since 1.25.0
    136     */
    137    public static boolean isCompressedFileName(final String fileName) {
    138        return fileNameUtil.isCompressedFileName(fileName);
    139    }
    140
    141    /** Private constructor to prevent instantiation of this utility class. */
    142    private BZip2Utils() {
    143    }
    144
    145}