Example usage for org.apache.commons.compress.archivers ArchiveOutputStream finish

List of usage examples for org.apache.commons.compress.archivers ArchiveOutputStream finish

Introduction

In this page you can find the example usage for org.apache.commons.compress.archivers ArchiveOutputStream finish.

Prototype

public abstract void finish() throws IOException;

Source Link

Document

Finishes the addition of entries to this stream, without closing it.

Usage

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

public static void writeDebFile(File destination, File[] inputsfiles) throws ArchiveException, IOException {
    ArchiveOutputStream archive = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.AR,
            new FileOutputStream(destination));

    for (File file : inputsfiles) {
        ArArchiveEntry entry = new ArArchiveEntry(file, file.getName());
        archive.putArchiveEntry(entry);//from w w w  .j a v  a 2s .com

        BufferedInputStream input = new BufferedInputStream(new FileInputStream(file));
        IOUtils.copy(input, archive);
        input.close();

        archive.closeArchiveEntry();
    }
    archive.finish();
    archive.close();

}

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

private static void addFilesFromFolderToTar(final File source, final File destination,
        final String archiverName) throws IOException, ArchiveException {
    OutputStream archiveStream = new FileOutputStream(destination);
    ArchiveOutputStream archive = new ArchiveStreamFactory().createArchiveOutputStream(archiverName,
            archiveStream);/*from w ww.j  a  v  a  2  s. c om*/

    if (source.isDirectory()) {
        for (File file : source.listFiles()) {

            if (file.getName().equals("DEBIAN")) {
                continue;
            } else if (file.isDirectory()) {
                addFilesFromFolderToTar(file, destination, archiverName);
            } else {
                fileEntryToDestination(file, archive, false);
            }

        }
    } else {
        fileEntryToDestination(source, archive, true);
    }

    archive.finish();
    archiveStream.close();
}

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   ww w . j a  v a2s . c o  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.mulesoft.jockey.maven.GenerateMojo.java

private void createZip(File distDir) throws MojoExecutionException {
    File output = new File(buildDirectory, distributionName + ".zip");
    try {/*from   ww w. jav  a  2  s  . co m*/
        final OutputStream out = new FileOutputStream(output);
        ArchiveOutputStream os = new ArchiveStreamFactory().createArchiveOutputStream("zip", out);

        copyArchiveFile(distDir, os, true);

        os.finish();

        os.close();
        out.close();
    } catch (IOException e) {
        throw new MojoExecutionException("Could not create zip file.", e);
    } catch (ArchiveException e) {
        throw new MojoExecutionException("Could not create zip file.", e);
    }
    projectHelper.attachArtifact(project, "zip", "", output);

}

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;/*from  w w  w.  j  a  v  a 2s . 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);

    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 w w. j  ava  2 s  . co m
        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.impetus.ankush.agent.utils.ZipFiles.java

/**
 * Zip file.//  w w  w  .  ja  v a2 s. co m
 *
 * @param filePath the file path
 * @return the string
 */
public String zipFile(String filePath) {
    try {
        /* Create Output Stream that will have final zip files */
        OutputStream zipOutput = new FileOutputStream(new File(filePath + ".zip"));
        /*
         * Create Archive Output Stream that attaches File Output Stream / and
         * specifies type of compression
         */
        ArchiveOutputStream logicalZip = new ArchiveStreamFactory()
                .createArchiveOutputStream(ArchiveStreamFactory.ZIP, zipOutput);
        /* Create Archieve entry - write header information */
        logicalZip.putArchiveEntry(new ZipArchiveEntry(FilenameUtils.getName(filePath)));
        /* Copy input file */
        IOUtils.copy(new FileInputStream(new File(filePath)), logicalZip);
        /* Close Archieve entry, write trailer information */
        logicalZip.closeArchiveEntry();

        /* Finish addition of entries to the file */
        logicalZip.finish();
        /* Close output stream, our files are zipped */
        zipOutput.close();
    } catch (Exception e) {
        System.err.println(e.getMessage());
        return null;
    }
    return filePath + ".zip";
}

From source file:com.neatresults.mgnltweaks.ui.action.ExportMultipleAction.java

/**
 * Once all items are exported, zip it all and send to client.
 *//*from w  w  w  . j  av a  2 s  .c  o m*/
@Override
protected void onPostExecute() throws Exception {
    IOUtils.closeQuietly(fileOutputStream);
    postExecCount++;

    if (getItems().size() > 1) {
        ExportCommand exportCommand = (ExportCommand) getCommand();
        String fileName = exportCommand.getFileName();
        tempFiles.put(fileName, fileOutput);

    }
    if (getItems().size() > 1 && postExecCount == getItems().size()) {
        // last run on multi-run
        String fileName = "magnoliaExport.zip";
        String mimeType = MIMEMapping.getMIMEType("zip");

        // will get deleted by stream after it is streamed through http request
        TempFileStreamResource tempFileStreamResource = new TempFileStreamResource();
        tempFileStreamResource.setTempFileName("magnoliaExport" + (Math.random() * 1000));
        tempFileStreamResource.setTempFileExtension("zip");
        ArchiveOutputStream zipOutput = new ArchiveStreamFactory().createArchiveOutputStream("zip",
                tempFileStreamResource.getTempFileOutputStream());
        try {

            for (Entry<String, File> entry : tempFiles.entrySet()) {
                zipOutput.putArchiveEntry(new ZipArchiveEntry(entry.getValue(), entry.getKey()));
                FileInputStream fis = new FileInputStream(entry.getValue());
                IOUtils.copy(fis, zipOutput);
                zipOutput.closeArchiveEntry();
                IOUtils.closeQuietly(fis);
            }
        } finally {
            zipOutput.finish();
            IOUtils.closeQuietly(zipOutput);
        }
        tempFileStreamResource.setFilename(fileName);
        tempFileStreamResource.setMIMEType(mimeType);
        // is this really necessary?
        tempFileStreamResource.getStream().setParameter("Content-Disposition",
                "attachment; filename=" + fileName + "\"");
        // Opens the resource for download
        Page.getCurrent().open(tempFileStreamResource, "", true);

    } else if (getItems().size() == 1) {
        // single item run
        final ExportCommand exportCommand = (ExportCommand) getCommand();
        tempFileStreamResource.setFilename(exportCommand.getFileName());
        tempFileStreamResource.setMIMEType(exportCommand.getMimeExtension());
        // Opens the resource for download
        Page.getCurrent().open(tempFileStreamResource, "", true);
    }
}

From source file:fr.gael.ccsds.sip.archive.TarArchiveManager.java

/**
 * Produces TAR archive/*from  www. java  2 s  . co  m*/
 */
@Override
public File copy(final File src, final File tar_file, final String dst) throws Exception {
    final ArchiveStreamFactory asf = new ArchiveStreamFactory();

    // Case of tar already exist: all the entries must be copied..
    if (tar_file.exists()) {
        final FileInputStream fis = new FileInputStream(tar_file);
        final ArchiveInputStream ais = asf.createArchiveInputStream(ArchiveStreamFactory.TAR, fis);

        final File tempFile = File.createTempFile("updateTar", "tar");
        final FileOutputStream fos = new FileOutputStream(tempFile);
        final ArchiveOutputStream aos = asf.createArchiveOutputStream(ArchiveStreamFactory.TAR, fos);

        // copy the existing entries
        ArchiveEntry nextEntry;
        while ((nextEntry = ais.getNextEntry()) != null) {
            aos.putArchiveEntry(nextEntry);
            IOUtils.copy(ais, aos);
            aos.closeArchiveEntry();
        }

        // create the new entry
        final TarArchiveEntry entry = new TarArchiveEntry(src, dst);
        entry.setSize(src.length());
        aos.putArchiveEntry(entry);
        final FileInputStream sfis = new FileInputStream(src);
        IOUtils.copy(sfis, aos);
        sfis.close();
        aos.closeArchiveEntry();

        aos.finish();
        ais.close();
        aos.close();
        fis.close();

        // copies the new file over the old
        tar_file.delete();
        tempFile.renameTo(tar_file);
        return tar_file;
    } else {
        final FileOutputStream fos = new FileOutputStream(tar_file);
        final ArchiveOutputStream aos = asf.createArchiveOutputStream(ArchiveStreamFactory.TAR, fos);

        // create the new entry
        final TarArchiveEntry entry = new TarArchiveEntry(src, dst);
        entry.setSize(src.length());
        aos.putArchiveEntry(entry);
        final FileInputStream sfis = new FileInputStream(src);
        IOUtils.copy(sfis, aos);
        sfis.close();
        aos.closeArchiveEntry();

        aos.finish();
        aos.close();
        fos.close();
    }
    return tar_file;
}

From source file:fr.gael.ccsds.sip.archive.TgzArchiveManager.java

@Override
public File copy(final File src, final File tar_file, final String dst) throws Exception {
    final ArchiveStreamFactory asf = new ArchiveStreamFactory();
    final CompressorStreamFactory csf = new CompressorStreamFactory();

    // Case of tar already exist: all the entries must be copied..
    if (tar_file.exists()) {
        final FileInputStream fis = new FileInputStream(tar_file);
        final CompressorInputStream cis = csf.createCompressorInputStream(CompressorStreamFactory.GZIP, fis);
        final ArchiveInputStream ais = asf.createArchiveInputStream(ArchiveStreamFactory.TAR, cis);

        final File tempFile = File.createTempFile("updateTar", "tar");
        final FileOutputStream fos = new FileOutputStream(tempFile);
        final CompressorOutputStream cos = csf.createCompressorOutputStream(CompressorStreamFactory.GZIP, fos);
        final ArchiveOutputStream aos = asf.createArchiveOutputStream(ArchiveStreamFactory.TAR, cos);

        // copy the existing entries
        ArchiveEntry nextEntry;/*w ww  .  j ava 2 s  .  c  o  m*/
        while ((nextEntry = ais.getNextEntry()) != null) {
            aos.putArchiveEntry(nextEntry);
            IOUtils.copy(ais, aos);
            aos.closeArchiveEntry();
        }

        // create the new entry
        final TarArchiveEntry entry = new TarArchiveEntry(src, dst);
        entry.setSize(src.length());
        aos.putArchiveEntry(entry);
        final FileInputStream sfis = new FileInputStream(src);
        IOUtils.copy(sfis, aos);
        sfis.close();
        aos.closeArchiveEntry();

        aos.finish();
        ais.close();
        aos.close();
        fis.close();

        // copies the new file over the old
        tar_file.delete();
        tempFile.renameTo(tar_file);
        return tar_file;
    } else {
        final FileOutputStream fos = new FileOutputStream(tar_file);
        final CompressorOutputStream cos = csf.createCompressorOutputStream(CompressorStreamFactory.GZIP, fos);
        final ArchiveOutputStream aos = asf.createArchiveOutputStream(ArchiveStreamFactory.TAR, cos);

        // create the new entry
        final TarArchiveEntry entry = new TarArchiveEntry(src, dst);
        entry.setSize(src.length());
        aos.putArchiveEntry(entry);
        final FileInputStream sfis = new FileInputStream(src);
        IOUtils.copy(sfis, aos);
        sfis.close();
        aos.closeArchiveEntry();

        aos.finish();
        aos.close();
        fos.close();
    }
    return tar_file;
}