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     */
019    package org.apache.commons.compress.compressors.xz;
020    
021    import java.util.HashMap;
022    import java.util.Map;
023    import org.apache.commons.compress.compressors.FileNameUtil;
024    
025    /**
026     * Utility code for the xz compression format.
027     * @ThreadSafe
028     * @since Commons Compress 1.4
029     */
030    public 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         * Detects common xz suffixes in the given filename.
048         *
049         * @param filename name of a file
050         * @return {@code true} if the filename has a common xz suffix,
051         *         {@code false} otherwise
052         */
053        public static boolean isCompressedFilename(String filename) {
054            return fileNameUtil.isCompressedFilename(filename);
055        }
056    
057        /**
058         * Maps the given name of a xz-compressed file to the name that the
059         * file should have after uncompression. Commonly used file type specific
060         * suffixes like ".txz" are automatically detected and
061         * correctly mapped. For example the name "package.txz" is mapped to
062         * "package.tar". And any filenames with the generic ".xz" suffix
063         * (or any other generic xz suffix) is mapped to a name without that
064         * suffix. If no xz suffix is detected, then the filename is returned
065         * unmapped.
066         *
067         * @param filename name of a file
068         * @return name of the corresponding uncompressed file
069         */
070        public static String getUncompressedFilename(String filename) {
071            return fileNameUtil.getUncompressedFilename(filename);
072        }
073    
074        /**
075         * Maps the given filename to the name that the file should have after
076         * compression with xz. Common file types with custom suffixes for
077         * compressed versions are automatically detected and correctly mapped.
078         * For example the name "package.tar" is mapped to "package.txz". If no
079         * custom mapping is applicable, then the default ".xz" suffix is appended
080         * to the filename.
081         *
082         * @param filename name of a file
083         * @return name of the corresponding compressed file
084         */
085        public static String getCompressedFilename(String filename) {
086            return fileNameUtil.getCompressedFilename(filename);
087        }
088    
089    }