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.xz; 20 21 import java.util.HashMap; 22 import java.util.Map; 23 import org.apache.commons.compress.compressors.FileNameUtil; 24 25 /** 26 * Utility code for the xz compression format. 27 * @ThreadSafe 28 * @since 1.4 29 */ 30 public class XZUtils { 31 32 private static final FileNameUtil fileNameUtil; 33 34 /** 35 * XZ Header Magic Bytes begin a XZ file. 36 * 37 * <p>This is a copy of {@code org.tukaani.xz.XZ.HEADER_MAGIC} in 38 * XZ for Java version 1.5.</p> 39 */ 40 private static final byte[] HEADER_MAGIC = { 41 (byte) 0xFD, '7', 'z', 'X', 'Z', '\0' 42 }; 43 44 enum CachedAvailability { 45 DONT_CACHE, CACHED_AVAILABLE, CACHED_UNAVAILABLE 46 } 47 48 private static volatile CachedAvailability cachedXZAvailability; 49 50 static { 51 final Map<String, String> uncompressSuffix = new HashMap<>(); 52 uncompressSuffix.put(".txz", ".tar"); 53 uncompressSuffix.put(".xz", ""); 54 uncompressSuffix.put("-xz", ""); 55 fileNameUtil = new FileNameUtil(uncompressSuffix, ".xz"); 56 cachedXZAvailability = CachedAvailability.DONT_CACHE; 57 try { 58 Class.forName("org.osgi.framework.BundleEvent"); 59 } catch (final Exception ex) { 60 setCacheXZAvailablity(true); 61 } 62 } 63 64 /** Private constructor to prevent instantiation of this utility class. */ XZUtils()65 private XZUtils() { 66 } 67 68 /** 69 * Checks if the signature matches what is expected for a .xz file. 70 * 71 * <p>This is more or less a copy of the version found in {@link 72 * XZCompressorInputStream} but doesn't depend on the presence of 73 * XZ for Java.</p> 74 * 75 * @param signature the bytes to check 76 * @param length the number of bytes to check 77 * @return true if signature matches the .xz magic bytes, false otherwise 78 * @since 1.9 79 */ matches(final byte[] signature, final int length)80 public static boolean matches(final byte[] signature, final int length) { 81 if (length < HEADER_MAGIC.length) { 82 return false; 83 } 84 85 for (int i = 0; i < HEADER_MAGIC.length; ++i) { 86 if (signature[i] != HEADER_MAGIC[i]) { 87 return false; 88 } 89 } 90 91 return true; 92 } 93 94 /** 95 * Are the classes required to support XZ compression available? 96 * @since 1.5 97 * @return true if the classes required to support XZ compression are available 98 */ isXZCompressionAvailable()99 public static boolean isXZCompressionAvailable() { 100 final CachedAvailability cachedResult = cachedXZAvailability; 101 if (cachedResult != CachedAvailability.DONT_CACHE) { 102 return cachedResult == CachedAvailability.CACHED_AVAILABLE; 103 } 104 return internalIsXZCompressionAvailable(); 105 } 106 internalIsXZCompressionAvailable()107 private static boolean internalIsXZCompressionAvailable() { 108 try { 109 XZCompressorInputStream.matches(null, 0); 110 return true; 111 } catch (final NoClassDefFoundError error) { 112 return false; 113 } 114 } 115 116 /** 117 * Detects common xz suffixes in the given filename. 118 * 119 * @param filename name of a file 120 * @return {@code true} if the filename has a common xz suffix, 121 * {@code false} otherwise 122 */ isCompressedFilename(final String filename)123 public static boolean isCompressedFilename(final String filename) { 124 return fileNameUtil.isCompressedFilename(filename); 125 } 126 127 /** 128 * Maps the given name of a xz-compressed file to the name that the 129 * file should have after uncompression. Commonly used file type specific 130 * suffixes like ".txz" are automatically detected and 131 * correctly mapped. For example the name "package.txz" is mapped to 132 * "package.tar". And any filenames with the generic ".xz" suffix 133 * (or any other generic xz suffix) is mapped to a name without that 134 * suffix. If no xz suffix is detected, then the filename is returned 135 * unmapped. 136 * 137 * @param filename name of a file 138 * @return name of the corresponding uncompressed file 139 */ getUncompressedFilename(final String filename)140 public static String getUncompressedFilename(final String filename) { 141 return fileNameUtil.getUncompressedFilename(filename); 142 } 143 144 /** 145 * Maps the given filename to the name that the file should have after 146 * compression with xz. Common file types with custom suffixes for 147 * compressed versions are automatically detected and correctly mapped. 148 * For example the name "package.tar" is mapped to "package.txz". If no 149 * custom mapping is applicable, then the default ".xz" suffix is appended 150 * to the filename. 151 * 152 * @param filename name of a file 153 * @return name of the corresponding compressed file 154 */ getCompressedFilename(final String filename)155 public static String getCompressedFilename(final String filename) { 156 return fileNameUtil.getCompressedFilename(filename); 157 } 158 159 /** 160 * Whether to cache the result of the XZ for Java check. 161 * 162 * <p>This defaults to {@code false} in an OSGi environment and {@code true} otherwise.</p> 163 * @param doCache whether to cache the result 164 * @since 1.9 165 */ setCacheXZAvailablity(final boolean doCache)166 public static void setCacheXZAvailablity(final boolean doCache) { 167 if (!doCache) { 168 cachedXZAvailability = CachedAvailability.DONT_CACHE; 169 } else if (cachedXZAvailability == CachedAvailability.DONT_CACHE) { 170 final boolean hasXz = internalIsXZCompressionAvailable(); 171 cachedXZAvailability = hasXz ? CachedAvailability.CACHED_AVAILABLE // NOSONAR 172 : CachedAvailability.CACHED_UNAVAILABLE; 173 } 174 } 175 176 // only exists to support unit tests getCachedXZAvailability()177 static CachedAvailability getCachedXZAvailability() { 178 return cachedXZAvailability; 179 } 180 } 181