BZip2Utils.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one
  3.  * or more contributor license agreements.  See the NOTICE file
  4.  * distributed with this work for additional information
  5.  * regarding copyright ownership.  The ASF licenses this file
  6.  * to you under the Apache License, Version 2.0 (the
  7.  * "License"); you may not use this file except in compliance
  8.  * with the License.  You may obtain a copy of the License at
  9.  *
  10.  * http://www.apache.org/licenses/LICENSE-2.0
  11.  *
  12.  * Unless required by applicable law or agreed to in writing,
  13.  * software distributed under the License is distributed on an
  14.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15.  * KIND, either express or implied.  See the License for the
  16.  * specific language governing permissions and limitations
  17.  * under the License.
  18.  */
  19. package org.apache.commons.compress.compressors.bzip2;

  20. import java.util.LinkedHashMap;
  21. import java.util.Map;

  22. import org.apache.commons.compress.compressors.FileNameUtil;

  23. /**
  24.  * Utility code for the BZip2 compression format.
  25.  *
  26.  * @ThreadSafe
  27.  * @since 1.1
  28.  */
  29. public abstract class BZip2Utils {

  30.     private static final FileNameUtil fileNameUtil;

  31.     static {
  32.         final Map<String, String> uncompressSuffix = new LinkedHashMap<>();
  33.         // backwards compatibility: BZip2Utils never created the short
  34.         // tbz form, so .tar.bz2 has to be added explicitly
  35.         uncompressSuffix.put(".tar.bz2", ".tar");
  36.         uncompressSuffix.put(".tbz2", ".tar");
  37.         uncompressSuffix.put(".tbz", ".tar");
  38.         uncompressSuffix.put(".bz2", "");
  39.         uncompressSuffix.put(".bz", "");
  40.         fileNameUtil = new FileNameUtil(uncompressSuffix, ".bz2");
  41.     }

  42.     /**
  43.      * 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
  44.      * 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
  45.      * naming pattern emerges.
  46.      *
  47.      * @param fileName name of a file
  48.      * @return name of the corresponding compressed file
  49.      * @deprecated Use {@link #getCompressedFileName(String)}.
  50.      */
  51.     @Deprecated
  52.     public static String getCompressedFilename(final String fileName) {
  53.         return fileNameUtil.getCompressedFileName(fileName);
  54.     }

  55.     /**
  56.      * 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
  57.      * 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
  58.      * naming pattern emerges.
  59.      *
  60.      * @param fileName name of a file
  61.      * @return name of the corresponding compressed file
  62.      * @since 1.25.0
  63.      */
  64.     public static String getCompressedFileName(final String fileName) {
  65.         return fileNameUtil.getCompressedFileName(fileName);
  66.     }

  67.     /**
  68.      * 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
  69.      * ".tbz" or ".tbz2" are automatically detected and correctly mapped. For example the name "package.tbz2" is mapped to "package.tar". And any file names
  70.      * 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
  71.      * name is returned unmapped.
  72.      *
  73.      * @param fileName name of a file
  74.      * @return name of the corresponding uncompressed file
  75.      * @deprecated Use {@link #getUncompressedFileName(String)}.
  76.      */
  77.     @Deprecated
  78.     public static String getUncompressedFilename(final String fileName) {
  79.         return fileNameUtil.getUncompressedFileName(fileName);
  80.     }

  81.     /**
  82.      * 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
  83.      * ".tbz" or ".tbz2" are automatically detected and correctly mapped. For example the name "package.tbz2" is mapped to "package.tar". And any file names
  84.      * 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
  85.      * name is returned unmapped.
  86.      *
  87.      * @param fileName name of a file
  88.      * @return name of the corresponding uncompressed file
  89.      * @since 1.25.0
  90.      */
  91.     public static String getUncompressedFileName(final String fileName) {
  92.         return fileNameUtil.getUncompressedFileName(fileName);
  93.     }

  94.     /**
  95.      * Detects common bzip2 suffixes in the given file name.
  96.      *
  97.      * @param fileName name of a file
  98.      * @return {@code true} if the file name has a common bzip2 suffix, {@code false} otherwise
  99.      * @deprecated Use {@link #isCompressedFileName(String)}.
  100.      */
  101.     @Deprecated
  102.     public static boolean isCompressedFilename(final String fileName) {
  103.         return fileNameUtil.isCompressedFileName(fileName);
  104.     }

  105.     /**
  106.      * Detects common bzip2 suffixes in the given file name.
  107.      *
  108.      * @param fileName name of a file
  109.      * @return {@code true} if the file name has a common bzip2 suffix, {@code false} otherwise
  110.      * @since 1.25.0
  111.      */
  112.     public static boolean isCompressedFileName(final String fileName) {
  113.         return fileNameUtil.isCompressedFileName(fileName);
  114.     }

  115.     /** Private constructor to prevent instantiation of this utility class. */
  116.     private BZip2Utils() {
  117.     }

  118. }