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.xz;
020
021import java.util.HashMap;
022import java.util.Map;
023import org.apache.commons.compress.compressors.FileNameUtil;
024
025/**
026 * Utility code for the xz compression format.
027 * @ThreadSafe
028 * @since 1.4
029 */
030public class XZUtils {
031
032    private static final FileNameUtil fileNameUtil;
033
034    static {
035        Map<String, String> uncompressSuffix = new HashMap<String, String>();
036        uncompressSuffix.put(".txz", ".tar");
037        uncompressSuffix.put(".xz", "");
038        uncompressSuffix.put("-xz", "");
039        fileNameUtil = new FileNameUtil(uncompressSuffix, ".xz");
040    }
041
042    /** Private constructor to prevent instantiation of this utility class. */
043    private XZUtils() {
044    }
045
046    /**
047     * Are the classes required to support XZ compression available?
048     * @since 1.5
049     */
050    public static boolean isXZCompressionAvailable() {
051        try {
052            XZCompressorInputStream.matches(null, 0);
053            return true;
054        } catch (NoClassDefFoundError error) {
055            return false;
056        }
057    }
058
059    /**
060     * Detects common xz suffixes in the given filename.
061     *
062     * @param filename name of a file
063     * @return {@code true} if the filename has a common xz suffix,
064     *         {@code false} otherwise
065     */
066    public static boolean isCompressedFilename(String filename) {
067        return fileNameUtil.isCompressedFilename(filename);
068    }
069
070    /**
071     * Maps the given name of a xz-compressed file to the name that the
072     * file should have after uncompression. Commonly used file type specific
073     * suffixes like ".txz" are automatically detected and
074     * correctly mapped. For example the name "package.txz" is mapped to
075     * "package.tar". And any filenames with the generic ".xz" suffix
076     * (or any other generic xz suffix) is mapped to a name without that
077     * suffix. If no xz suffix is detected, then the filename is returned
078     * unmapped.
079     *
080     * @param filename name of a file
081     * @return name of the corresponding uncompressed file
082     */
083    public static String getUncompressedFilename(String filename) {
084        return fileNameUtil.getUncompressedFilename(filename);
085    }
086
087    /**
088     * Maps the given filename to the name that the file should have after
089     * compression with xz. Common file types with custom suffixes for
090     * compressed versions are automatically detected and correctly mapped.
091     * For example the name "package.tar" is mapped to "package.txz". If no
092     * custom mapping is applicable, then the default ".xz" suffix is appended
093     * to the filename.
094     *
095     * @param filename name of a file
096     * @return name of the corresponding compressed file
097     */
098    public static String getCompressedFilename(String filename) {
099        return fileNameUtil.getCompressedFilename(filename);
100    }
101
102}