Example usage for org.apache.commons.compress.compressors CompressorOutputStream close

List of usage examples for org.apache.commons.compress.compressors CompressorOutputStream close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closes this output stream and releases any system resources associated with this stream.

Usage

From source file:com.github.wolfposd.jdpkg.deb.DpkgDeb.java

private static void compressArchive(File source, File destination, String compression)
        throws CompressorException, IOException {
    OutputStream archiveStream = new FileOutputStream(destination);
    CompressorOutputStream comp = new CompressorStreamFactory().createCompressorOutputStream(compression,
            archiveStream);//from   w ww  .j  a  v a2s . c  o m

    BufferedInputStream input = new BufferedInputStream(new FileInputStream(source));

    IOUtils.copy(input, comp);
    input.close();
    comp.close();

}

From source file:com.streamsets.pipeline.stage.origin.spooldir.TestSpoolDirWithCompression.java

@BeforeClass
public static void setUpClass()
        throws IOException, InterruptedException, URISyntaxException, CompressorException {
    testDir = new File("target", UUID.randomUUID().toString()).getAbsoluteFile();
    Assert.assertTrue(testDir.mkdirs());
    Files.copy(Paths.get(Resources.getResource("logArchive.zip").toURI()),
            Paths.get(testDir.getAbsolutePath(), "logArchive1.zip"));
    Files.copy(Paths.get(Resources.getResource("logArchive.zip").toURI()),
            Paths.get(testDir.getAbsolutePath(), "logArchive2.zip"));
    Files.copy(Paths.get(Resources.getResource("logArchive.tar.gz").toURI()),
            Paths.get(testDir.getAbsolutePath(), "logArchive1.tar.gz"));
    Files.copy(Paths.get(Resources.getResource("logArchive.tar.gz").toURI()),
            Paths.get(testDir.getAbsolutePath(), "logArchive2.tar.gz"));
    Files.copy(Paths.get(Resources.getResource("testAvro.tar.gz").toURI()),
            Paths.get(testDir.getAbsolutePath(), "testAvro1.tar.gz"));
    Files.copy(Paths.get(Resources.getResource("testAvro.tar.gz").toURI()),
            Paths.get(testDir.getAbsolutePath(), "testAvro2.tar.gz"));

    File bz2File = new File(testDir, "testFile1.bz2");
    CompressorOutputStream bzip2 = new CompressorStreamFactory().createCompressorOutputStream("bzip2",
            new FileOutputStream(bz2File));
    bzip2.write(IOUtils.toByteArray(Resources.getResource("testLogFile.txt").openStream()));
    bzip2.close();

    bz2File = new File(testDir, "testFile2.bz2");
    bzip2 = new CompressorStreamFactory().createCompressorOutputStream("bzip2", new FileOutputStream(bz2File));
    bzip2.write(IOUtils.toByteArray(Resources.getResource("testLogFile.txt").openStream()));
    bzip2.close();/*from  www .j  a  va 2 s.  co m*/
}

From source file:deployer.TestUtils.java

public static ByteBuffer createSampleOpenShiftWebAppTarBall() throws IOException, ArchiveException {
    ByteArrayInputStream bis = new ByteArrayInputStream(createSampleAppTarBall(ArtifactType.WebApp).array());
    CompressorInputStream cis = new GzipCompressorInputStream(bis);
    ArchiveInputStream ais = new TarArchiveInputStream(cis);

    ByteArrayOutputStream bos = new ByteArrayOutputStream(bis.available() + 2048);
    CompressorOutputStream cos = new GzipCompressorOutputStream(bos);
    ArchiveOutputStream aos = new TarArchiveOutputStream(cos);

    ArchiveEntry nextEntry;/*from  w ww. ja v a 2 s . co  m*/
    while ((nextEntry = ais.getNextEntry()) != null) {
        aos.putArchiveEntry(nextEntry);
        IOUtils.copy(ais, aos);
        aos.closeArchiveEntry();
    }
    ais.close();
    cis.close();
    bis.close();

    TarArchiveEntry entry = new TarArchiveEntry(
            Paths.get(".openshift", CONFIG_DIRECTORY, "/standalone.xml").toFile());
    byte[] xmlData = SAMPLE_STANDALONE_DATA.getBytes();
    entry.setSize(xmlData.length);
    aos.putArchiveEntry(entry);
    IOUtils.write(xmlData, aos);
    aos.closeArchiveEntry();

    aos.finish();
    cos.close();
    bos.flush();
    return ByteBuffer.wrap(bos.toByteArray());
}

From source file:com.surevine.gateway.scm.gatewayclient.GatewayPackage.java

void writeGZ(final Path gzPath, final Path tarPath) throws IOException, CompressorException {
    CompressorOutputStream cos = new CompressorStreamFactory().createCompressorOutputStream("gz",
            Files.newOutputStream(gzPath));

    try {/*from   w  w  w.j a  v  a 2  s  . co  m*/
        Files.copy(tarPath, cos);
    } finally {
        cos.close();
    }
}

From source file:com.streamsets.pipeline.lib.parser.TestCompressionInputBuilder.java

private void testCompressedFile(String compressionType) throws Exception {

    //write data into the stream using the specified compression
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    CompressorOutputStream cOut = new CompressorStreamFactory().createCompressorOutputStream(compressionType,
            bOut);//w  ww  .  jav a 2  s . co  m
    cOut.write("StreamSets".getBytes());
    cOut.close();

    //create compression input
    CompressionDataParser.CompressionInputBuilder compressionInputBuilder = new CompressionDataParser.CompressionInputBuilder(
            Compression.COMPRESSED_FILE, null, new ByteArrayInputStream(bOut.toByteArray()), "0");
    CompressionDataParser.CompressionInput input = compressionInputBuilder.build();

    //verify
    Assert.assertNotNull(input);
    Assert.assertEquals("myFile::4567", input.wrapOffset("myFile::4567"));
    Assert.assertEquals("myFile::4567", input.wrapRecordId("myFile::4567"));
    InputStream myFile = input.getNextInputStream();
    BufferedReader reader = new BufferedReader(new InputStreamReader(myFile));
    Assert.assertEquals("StreamSets", reader.readLine());
}

From source file:com.streamsets.pipeline.lib.parser.TestCompressionInputBuilder.java

private void testConcatenatedCompressedFile(String compressionType) throws Exception {
    ByteArrayOutputStream bytes1 = new ByteArrayOutputStream();
    ByteArrayOutputStream bytes2 = new ByteArrayOutputStream();
    CompressorOutputStream compressed1 = new CompressorStreamFactory()
            .createCompressorOutputStream(compressionType, bytes1);

    CompressorOutputStream compressed2 = new CompressorStreamFactory()
            .createCompressorOutputStream(compressionType, bytes2);

    compressed1.write("line1\n".getBytes());
    compressed1.close();

    compressed2.write("line2".getBytes());
    compressed2.close();//  w ww .ja  va2 s  . c  o  m

    CompressionDataParser.CompressionInputBuilder compressionInputBuilder = new CompressionDataParser.CompressionInputBuilder(
            Compression.COMPRESSED_FILE, null,
            new SequenceInputStream(new ByteArrayInputStream(bytes1.toByteArray()),
                    new ByteArrayInputStream(bytes2.toByteArray())),
            "0");
    CompressionDataParser.CompressionInput input = compressionInputBuilder.build();

    //verify
    Assert.assertNotNull(input);
    Assert.assertEquals("myFile::4567", input.wrapOffset("myFile::4567"));
    Assert.assertEquals("myFile::4567", input.wrapRecordId("myFile::4567"));
    InputStream myFile = input.getNextInputStream();
    BufferedReader reader = new BufferedReader(new InputStreamReader(myFile));
    Assert.assertEquals("line1", reader.readLine());
    Assert.assertEquals("line2", reader.readLine());
}

From source file:deployer.TestUtils.java

public static ByteBuffer createSampleOpenShiftWebAppTarBallWithEmptyFiles(String[] filepaths)
        throws IOException, ArchiveException {
    ByteArrayInputStream bis = new ByteArrayInputStream(createSampleAppTarBall(ArtifactType.WebApp).array());
    CompressorInputStream cis = new GzipCompressorInputStream(bis);
    ArchiveInputStream ais = new TarArchiveInputStream(cis);

    ByteArrayOutputStream bos = new ByteArrayOutputStream(bis.available() + 2048);
    CompressorOutputStream cos = new GzipCompressorOutputStream(bos);
    ArchiveOutputStream aos = new TarArchiveOutputStream(cos);

    ArchiveEntry nextEntry;//  w  w w. j a v a  2 s .  com
    while ((nextEntry = ais.getNextEntry()) != null) {
        aos.putArchiveEntry(nextEntry);
        IOUtils.copy(ais, aos);
        aos.closeArchiveEntry();
    }
    ais.close();
    cis.close();
    bis.close();

    TarArchiveEntry entry = new TarArchiveEntry(
            Paths.get(".openshift", CONFIG_DIRECTORY, "/standalone.xml").toFile());
    byte[] xmlData = SAMPLE_STANDALONE_DATA.getBytes();
    entry.setSize(xmlData.length);
    aos.putArchiveEntry(entry);
    IOUtils.write(xmlData, aos);

    for (int i = 0; i < filepaths.length; i++) {
        String filepath = filepaths[i];
        TarArchiveEntry emptyEntry = new TarArchiveEntry(Paths.get(filepath).toFile());
        byte[] emptyData = "".getBytes();
        emptyEntry.setSize(emptyData.length);
        aos.putArchiveEntry(emptyEntry);
        IOUtils.write(emptyData, aos);
        aos.closeArchiveEntry();
    }

    aos.finish();
    cos.close();
    bos.flush();
    return ByteBuffer.wrap(bos.toByteArray());
}

From source file:deployer.TestUtils.java

public static ByteBuffer createSampleAppTarBall(ArtifactType type) throws IOException, ArchiveException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream(8096);
    CompressorOutputStream gzs = new GzipCompressorOutputStream(bos);
    ArchiveOutputStream aos = new TarArchiveOutputStream(gzs);
    {//from   w ww  .java  2  s .  c  om
        TarArchiveEntry nextEntry = new TarArchiveEntry(CONFIG_DIRECTORY + "/app.conf");
        byte[] sampleConf = SAMPLE_CONF_DATA.getBytes();
        nextEntry.setSize(sampleConf.length);
        aos.putArchiveEntry(nextEntry);
        IOUtils.write(sampleConf, aos);
        aos.closeArchiveEntry();
    }
    if (type != ArtifactType.WebApp) {
        TarArchiveEntry nextEntry = new TarArchiveEntry("bin/myApplication.jar");
        byte[] jarData = SAMPLE_JAR_DATA.getBytes();
        nextEntry.setSize(jarData.length);
        aos.putArchiveEntry(nextEntry);
        IOUtils.write(jarData, aos);
        aos.closeArchiveEntry();
    } else {
        TarArchiveEntry nextEntry = new TarArchiveEntry("deployments/ROOT.war");
        byte[] jarData = SAMPLE_JAR_DATA.getBytes();
        nextEntry.setSize(jarData.length);
        aos.putArchiveEntry(nextEntry);
        IOUtils.write(jarData, aos);
        aos.closeArchiveEntry();
    }
    aos.finish();
    gzs.close();
    bos.flush();
    return ByteBuffer.wrap(bos.toByteArray());
}

From source file:com.gitblit.servlet.PtServlet.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    try {//from  w  w  w. java 2s .co  m
        response.setContentType("application/octet-stream");
        response.setDateHeader("Last-Modified", lastModified);
        response.setHeader("Cache-Control", "none");
        response.setHeader("Pragma", "no-cache");
        response.setDateHeader("Expires", 0);

        boolean windows = false;
        try {
            String useragent = request.getHeader("user-agent").toString();
            windows = useragent.toLowerCase().contains("windows");
        } catch (Exception e) {
        }

        byte[] pyBytes;
        File file = runtimeManager.getFileOrFolder("tickets.pt", "${baseFolder}/pt.py");
        if (file.exists()) {
            // custom script
            pyBytes = readAll(new FileInputStream(file));
        } else {
            // default script
            pyBytes = readAll(getClass().getResourceAsStream("/pt.py"));
        }

        if (windows) {
            // windows: download zip file with pt.py and pt.cmd
            response.setHeader("Content-Disposition", "attachment; filename=\"pt.zip\"");

            OutputStream os = response.getOutputStream();
            ZipArchiveOutputStream zos = new ZipArchiveOutputStream(os);

            // add the Python script
            ZipArchiveEntry pyEntry = new ZipArchiveEntry("pt.py");
            pyEntry.setSize(pyBytes.length);
            pyEntry.setUnixMode(FileMode.EXECUTABLE_FILE.getBits());
            pyEntry.setTime(lastModified);
            zos.putArchiveEntry(pyEntry);
            zos.write(pyBytes);
            zos.closeArchiveEntry();

            // add a Python launch cmd file
            byte[] cmdBytes = readAll(getClass().getResourceAsStream("/pt.cmd"));
            ZipArchiveEntry cmdEntry = new ZipArchiveEntry("pt.cmd");
            cmdEntry.setSize(cmdBytes.length);
            cmdEntry.setUnixMode(FileMode.REGULAR_FILE.getBits());
            cmdEntry.setTime(lastModified);
            zos.putArchiveEntry(cmdEntry);
            zos.write(cmdBytes);
            zos.closeArchiveEntry();

            // add a brief readme
            byte[] txtBytes = readAll(getClass().getResourceAsStream("/pt.txt"));
            ZipArchiveEntry txtEntry = new ZipArchiveEntry("readme.txt");
            txtEntry.setSize(txtBytes.length);
            txtEntry.setUnixMode(FileMode.REGULAR_FILE.getBits());
            txtEntry.setTime(lastModified);
            zos.putArchiveEntry(txtEntry);
            zos.write(txtBytes);
            zos.closeArchiveEntry();

            // cleanup
            zos.finish();
            zos.close();
            os.flush();
        } else {
            // unix: download a tar.gz file with pt.py set with execute permissions
            response.setHeader("Content-Disposition", "attachment; filename=\"pt.tar.gz\"");

            OutputStream os = response.getOutputStream();
            CompressorOutputStream cos = new CompressorStreamFactory()
                    .createCompressorOutputStream(CompressorStreamFactory.GZIP, os);
            TarArchiveOutputStream tos = new TarArchiveOutputStream(cos);
            tos.setAddPaxHeadersForNonAsciiNames(true);
            tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);

            // add the Python script
            TarArchiveEntry pyEntry = new TarArchiveEntry("pt");
            pyEntry.setMode(FileMode.EXECUTABLE_FILE.getBits());
            pyEntry.setModTime(lastModified);
            pyEntry.setSize(pyBytes.length);
            tos.putArchiveEntry(pyEntry);
            tos.write(pyBytes);
            tos.closeArchiveEntry();

            // add a brief readme
            byte[] txtBytes = readAll(getClass().getResourceAsStream("/pt.txt"));
            TarArchiveEntry txtEntry = new TarArchiveEntry("README");
            txtEntry.setMode(FileMode.REGULAR_FILE.getBits());
            txtEntry.setModTime(lastModified);
            txtEntry.setSize(txtBytes.length);
            tos.putArchiveEntry(txtEntry);
            tos.write(txtBytes);
            tos.closeArchiveEntry();

            // cleanup
            tos.finish();
            tos.close();
            cos.close();
            os.flush();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:no.finntech.shootout.streams.CompressedStreams.java

@Override
protected void writeTo(OutputStream out) throws IOException, CompressorException {
    String json = JsonUtil.objectToJson(getObject());
    CompressorOutputStream outputStream = FACTORY.createCompressorOutputStream(getCompressor(), out);
    outputStream.write(json.getBytes());
    outputStream.flush();//from   w w w  . j ava  2s  .c  om
    outputStream.close();
}