下面列出了org.apache.commons.io.input.CloseShieldInputStream#org.apache.commons.compress.compressors.xz.XZCompressorInputStream 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Decodes a byte array as an InputStream. The byte array may be compressed using some codec.
* Reads from the returned stream will result in decompressed bytes.
*
* <p>This supports the same codecs as Avro's {@link CodecFactory}, namely those defined in
* {@link DataFileConstants}.
*
* <ul>
* <li>"snappy" : Google's Snappy compression
* <li>"deflate" : deflate compression
* <li>"bzip2" : Bzip2 compression
* <li>"xz" : xz compression
* <li>"null" (the string, not the value): Uncompressed data
* </ul>
*/
private static InputStream decodeAsInputStream(byte[] data, String codec) throws IOException {
ByteArrayInputStream byteStream = new ByteArrayInputStream(data);
switch (codec) {
case DataFileConstants.SNAPPY_CODEC:
return new SnappyCompressorInputStream(byteStream, 1 << 16 /* Avro uses 64KB blocks */);
case DataFileConstants.DEFLATE_CODEC:
// nowrap == true: Do not expect ZLIB header or checksum, as Avro does not write them.
Inflater inflater = new Inflater(true);
return new InflaterInputStream(byteStream, inflater);
case DataFileConstants.XZ_CODEC:
return new XZCompressorInputStream(byteStream);
case DataFileConstants.BZIP2_CODEC:
return new BZip2CompressorInputStream(byteStream);
case DataFileConstants.NULL_CODEC:
return byteStream;
default:
throw new IllegalArgumentException("Unsupported codec: " + codec);
}
}
@Test
public void removeAbsoluteUrlFromXz() throws Exception {
when(artifactsXml.get()).thenReturn(getClass().getResourceAsStream(ARTIFACTS_XML_XZ));
TempBlob modified = underTest.removeMirrorUrlFromArtifactsXml(artifactsXml, repository, "xml.xz");
try (XZCompressorInputStream xz = new XZCompressorInputStream(modified.get())) {
assertXmlMatches(xz, ARTIFACTS_XML_WITHOUT_ABSOLUTE);
}
catch (IOException e) {
fail("Exception not expected. Is this file XZ compressed? " + e.getMessage());
}
}
List<File> uncompressTarXzFile(File inputFile, File outputDir, Consumer<ProgressEntity> stateCallback) {
try (CountingInputStream countingInputStream = new CountingInputStream(new FileInputStream(inputFile));
InputStream inputStream = new XZCompressorInputStream(countingInputStream)) {
final long finalSize = FileUtils.sizeOf(inputFile);
return uncompress(inputStream, countingInputStream, outputDir, finalSize, stateCallback);
} catch (IOException e) {
throw new ArchiveException(TAR_ERROR_MESSAGE, e);
}
}
public static void extractResource(@Nullable ClassLoader cl, @Nullable Object parentObject,
@Nonnull String resourceLocation, @Nonnull File outputFile, boolean uncompress)
throws IOException {
// if (outputFile.exists()) {
// throw new IOException("Cannot overwrite existing file: " + outputFile);
// }
File parent = outputFile.getParentFile();
if (parent != null && !parent.exists()) {
if (!parent.mkdirs()) {
throw new IOException("Could not create directory " + parent);
}
}
InputStream inp = new BufferedInputStream(getStream(cl, parentObject, resourceLocation));
if (uncompress) {
if (resourceLocation.endsWith(".tar.bz2")) {
extractArchive(new TarArchiveInputStream(new BZip2CompressorInputStream(inp)), outputFile);
return;
}
if (resourceLocation.endsWith(".tar.xz")) {
extractArchive(new TarArchiveInputStream(new XZCompressorInputStream(inp)), outputFile);
return;
}
if (resourceLocation.endsWith(".tar.gz") || resourceLocation.endsWith(".tgz")) {
extractArchive(new TarArchiveInputStream(new GzipCompressorInputStream(inp)), outputFile);
return;
}
if (resourceLocation.endsWith(".tar")) {
extractArchive(new TarArchiveInputStream(inp), outputFile);
return;
}
if (resourceLocation.endsWith(".zip")) {
extractArchive(new ZipArchiveInputStream(inp), outputFile);
return;
}
}
extractFile(inp, outputFile);
}
@Override
public XZCompressorInputStream create(InputStream in) throws IOException {
return new XZCompressorInputStream(in, true);
}
@Override
public XZCompressorInputStream create(InputStream in) throws IOException {
return new XZCompressorInputStream(in, true);
}
@Override
public InputStream createInputStream ( final InputStream in ) throws IOException
{
return new XZCompressorInputStream ( in );
}
@Override
public XZCompressorInputStream create(InputStream in) throws IOException {
return new XZCompressorInputStream(in, true);
}