Example usage for org.apache.commons.compress.compressors.zstandard ZstdCompressorOutputStream ZstdCompressorOutputStream

List of usage examples for org.apache.commons.compress.compressors.zstandard ZstdCompressorOutputStream ZstdCompressorOutputStream

Introduction

In this page you can find the example usage for org.apache.commons.compress.compressors.zstandard ZstdCompressorOutputStream ZstdCompressorOutputStream.

Prototype

public ZstdCompressorOutputStream(final OutputStream outStream) throws IOException 

Source Link

Document

Wraps the given stream into a zstd-jni ZstdOutputStream using the default values for level , closeFrameOnFlush and useChecksum .

Usage

From source file:com.facebook.buck.artifact_cache.ArtifactUploader.java

/** Archive and compress 'pathsToIncludeInArchive' into 'out', using tar+zstandard. */
@VisibleForTesting/*  w  ww.  j a  v  a 2 s  . c om*/
static void compress(ProjectFilesystem projectFilesystem, Collection<Path> pathsToIncludeInArchive, Path out)
        throws IOException {
    try (OutputStream o = new BufferedOutputStream(Files.newOutputStream(out));
            OutputStream z = new ZstdCompressorOutputStream(o);
            TarArchiveOutputStream archive = new TarArchiveOutputStream(z)) {
        archive.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
        for (Path path : pathsToIncludeInArchive) {
            boolean isRegularFile = !projectFilesystem.isDirectory(path);

            // Add a file entry.
            TarArchiveEntry e = new TarArchiveEntry(path.toString() + (isRegularFile ? "" : "/"));
            e.setMode((int) projectFilesystem.getPosixFileMode(path));
            e.setModTime(ZipConstants.getFakeTime());

            if (isRegularFile) {
                e.setSize(projectFilesystem.getFileSize(path));
                archive.putArchiveEntry(e);
                try (InputStream input = projectFilesystem.newFileInputStream(path)) {
                    ByteStreams.copy(input, archive);
                }
            } else {
                archive.putArchiveEntry(e);
            }
            archive.closeArchiveEntry();
        }
        archive.finish();
    }
}

From source file:com.facebook.buck.jvm.java.DefaultJavaLibraryIntegrationTest.java

/**
 * writeTarZst writes a .tar.zst file to 'file'.
 *
 * <p>For each key:value in archiveContents, a file named 'key' with contents 'value' will be
 * created in the archive. File names ending with "/" are considered directories.
 *///from w w w  .j  a v  a 2 s.c o m
private void writeTarZst(Path file, Map<String, byte[]> archiveContents) throws IOException {
    try (OutputStream o = new BufferedOutputStream(Files.newOutputStream(file));
            OutputStream z = new ZstdCompressorOutputStream(o);
            TarArchiveOutputStream archive = new TarArchiveOutputStream(z)) {
        archive.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
        for (Entry<String, byte[]> mapEntry : archiveContents.entrySet()) {
            String fileName = mapEntry.getKey();
            byte[] fileContents = mapEntry.getValue();
            boolean isRegularFile = !fileName.endsWith("/");

            TarArchiveEntry e = new TarArchiveEntry(fileName);
            if (isRegularFile) {
                e.setSize(fileContents.length);
                archive.putArchiveEntry(e);
                archive.write(fileContents);
            } else {
                archive.putArchiveEntry(e);
            }
            archive.closeArchiveEntry();
        }
        archive.finish();
    }
}

From source file:com.facebook.buck.core.build.engine.impl.CachingBuildEngineTest.java

private static void writeEntriesToArchive(Path file, ImmutableMap<Path, String> entries,
        ImmutableList<Path> directories) throws IOException {
    try (OutputStream o = new BufferedOutputStream(Files.newOutputStream(file));
            OutputStream z = new ZstdCompressorOutputStream(o);
            TarArchiveOutputStream archive = new TarArchiveOutputStream(z)) {
        archive.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
        for (Map.Entry<Path, String> mapEntry : entries.entrySet()) {
            TarArchiveEntry e = new TarArchiveEntry(mapEntry.getKey().toString());
            e.setModTime(ZipConstants.getFakeTime());
            byte[] bytes = mapEntry.getValue().getBytes(UTF_8);
            e.setSize(bytes.length);//w  w  w. jav  a 2  s  .  co  m
            archive.putArchiveEntry(e);
            archive.write(bytes);
            archive.closeArchiveEntry();
        }
        for (Path dir : directories) {
            TarArchiveEntry e = new TarArchiveEntry(dir.toString() + "/");
            e.setModTime(ZipConstants.getFakeTime());
        }
        archive.finish();
    }
}

From source file:org.apache.avro.file.ZstandardCodec.java

@Override
public ByteBuffer compress(ByteBuffer uncompressedData) throws IOException {
    ByteArrayOutputStream baos = getOutputBuffer(uncompressedData.remaining());
    OutputStream outputStream = new ZstdCompressorOutputStream(baos);
    writeAndClose(uncompressedData, outputStream);
    return ByteBuffer.wrap(baos.toByteArray());
}

From source file:org.apache.druid.java.util.common.CompressionUtilsTest.java

@Test
public void testDecompressZstd() throws IOException {
    final File tmpDir = temporaryFolder.newFolder("testDecompressZstd");
    final File zstdFile = new File(tmpDir, testFile.getName() + ".zst");
    Assert.assertFalse(zstdFile.exists());
    try (final OutputStream out = new ZstdCompressorOutputStream(new FileOutputStream(zstdFile))) {
        ByteStreams.copy(new FileInputStream(testFile), out);
    }/*from   w w w  .j  a v a  2 s  . c  om*/
    try (final InputStream inputStream = CompressionUtils.decompress(new FileInputStream(zstdFile),
            zstdFile.getName())) {
        assertGoodDataStream(inputStream);
    }
}