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

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

Introduction

In this page you can find the example usage for org.apache.commons.compress.archivers ArchiveOutputStream 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:de.fischer.thotti.core.distbuilder.CommonsCompressTest.java

public static void main(String[] args) throws IOException, ArchiveException {

    File output = new File("C:\\projekte\\thotti.master\\test.zip");
    File file1 = new File("C:\\projekte\\thotti.master\\pom.xml");
    File file2 = new File("C:\\projekte\\thotti.master\\todo.txt");

    final OutputStream out = new FileOutputStream(output);
    ArchiveOutputStream os = new ArchiveStreamFactory().createArchiveOutputStream("zip", out);

    os.putArchiveEntry(new ZipArchiveEntry("testdata/test1.xml"));
    IOUtils.copy(new FileInputStream(file1), os);
    os.closeArchiveEntry();//from w ww.  j  a va 2s.c o  m

    os.putArchiveEntry(new ZipArchiveEntry("testdata/test2.xml"));
    IOUtils.copy(new FileInputStream(file2), os);
    os.closeArchiveEntry();
    out.flush();
    os.close();
}

From source file:gov.nih.nci.caarray.web.helper.DownloadHelper.java

/**
 * Zips the selected files and writes the result to the servlet output stream. Also sets content type and
 * disposition appropriately.//from w  ww  .j  a  v a2 s.  co  m
 * 
 * @param files the files to zip and send
 * @param baseFilename the filename w/o the suffix to use for the archive file. This filename will be set as the
 *            Content-disposition header
 * @throws IOException if there is an error writing to the stream
 */
public static void downloadFiles(Collection<CaArrayFile> files, String baseFilename) throws IOException {
    final HttpServletResponse response = ServletActionContext.getResponse();

    try {
        final PackagingInfo info = getPreferedPackageInfo(files, baseFilename);
        response.setContentType(info.getMethod().getMimeType());
        response.addHeader("Content-disposition", "filename=\"" + info.getName() + "\"");

        final List<CaArrayFile> sortedFiles = new ArrayList<CaArrayFile>(files);
        Collections.sort(sortedFiles, CAARRAYFILE_NAME_COMPARATOR_INSTANCE);
        final OutputStream sos = response.getOutputStream();
        final OutputStream closeShield = new CloseShieldOutputStream(sos);
        final ArchiveOutputStream arOut = info.getMethod().createArchiveOutputStream(closeShield);
        try {
            for (final CaArrayFile f : sortedFiles) {
                fileAccessUtils.addFileToArchive(f, arOut);
            }
        } finally {
            // note that the caller's stream is shielded from the close(),
            // but this is the only way to finish and flush the (gzip) stream.
            try {
                arOut.close();
            } catch (final Exception e) {
                LOG.error(e);
            }
        }
    } catch (final Exception e) {
        LOG.error("Error streaming download of files " + files, e);
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
    }
}

From source file:big.zip.java

/**
 * // w  ww . java 2s  .c  om
 * @param fileToCompress    The file that we want to compress
 * @param fileToOutput      The zip file containing the compressed file
 * @return  True if the file was compressed and created, false when something
 * went wrong
 */
public static boolean compress(final File fileToCompress, final File fileToOutput) {
    if (fileToOutput.exists()) {
        // do the first run to delete this file
        fileToOutput.delete();
        // did this worked?
        if (fileToOutput.exists()) {
            // something went wrong, the file is still here
            System.out.println("ZIP59 - Failed to delete output file: " + fileToOutput.getAbsolutePath());
            return false;
        }
    }
    // does our file to compress exist?
    if (fileToCompress.exists() == false) {
        // we have a problem here
        System.out.println("ZIP66 - Didn't found the file to compress: " + fileToCompress.getAbsolutePath());
        return false;
    }
    // all checks are done, now it is time to do the compressing
    try {
        final OutputStream outputStream = new FileOutputStream(fileToOutput);
        ArchiveOutputStream archive = new ArchiveStreamFactory().createArchiveOutputStream("zip", outputStream);
        archive.putArchiveEntry(new ZipArchiveEntry(fileToCompress.getName()));
        // create the input file stream and copy it over to the archive
        FileInputStream inputStream = new FileInputStream(fileToCompress);
        IOUtils.copy(inputStream, archive);
        // close the archive
        archive.closeArchiveEntry();
        archive.flush();
        archive.close();
        // now close the input file stream
        inputStream.close();
        // and close the output file stream too
        outputStream.flush();
        outputStream.close();

    } catch (FileNotFoundException ex) {
        Logger.getLogger(zip.class.getName()).log(Level.SEVERE, null, ex);
        return false;
    } catch (ArchiveException ex) {
        Logger.getLogger(zip.class.getName()).log(Level.SEVERE, null, ex);
        return false;
    } catch (IOException ex) {
        Logger.getLogger(zip.class.getName()).log(Level.SEVERE, null, ex);
        return false;
    }
    return true;
}

From source file:net.orpiske.ssps.common.archive.TarArchiveUtils.java

/**
 * Lower level pack operation/*from ww  w. j a  v  a  2  s  .c o  m*/
 * 
 * @param source
 *            source file
 * @param destination
 *            destination file
 * @return the number of bytes processed
 * @throws ArchiveException
 * @throws IOException
 */
public static long pack(String source, File destination) throws ArchiveException, IOException {

    ArchiveStreamFactory factory = new ArchiveStreamFactory();

    OutputStream outStream = new FileOutputStream(destination);

    ArchiveOutputStream outputStream;
    try {
        outputStream = factory.createArchiveOutputStream(ArchiveStreamFactory.TAR, outStream);
    } catch (ArchiveException e) {
        IOUtils.closeQuietly(outStream);

        throw e;
    }

    File startDirectory = new File(source);

    RecursiveArchiver archiver = new RecursiveArchiver(outputStream);

    try {
        archiver.archive(startDirectory);
        outputStream.flush();
        outputStream.close();

        outStream.flush();
        outStream.close();

    } catch (IOException e) {
        IOUtils.closeQuietly(outStream);
        IOUtils.closeQuietly(outputStream);

        throw e;
    }

    long ret = outputStream.getBytesWritten();

    if (logger.isDebugEnabled()) {
        logger.debug("Packed " + ret + " bytes");
    }

    return ret;
}

From source file:io.zatarox.satellite.impl.BackgroundWrapperTest.java

@BeforeClass
public static void setBefore() throws Exception {
    file = File.createTempFile("archive", ".jar");
    file.deleteOnExit();//from  w ww.  jav a2s . c  o  m
    final FileOutputStream out = new FileOutputStream(file);
    ArchiveOutputStream archive = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP,
            out);
    ZipArchiveEntry entry = new ZipArchiveEntry("META-INF/MANIFEST.MF");
    archive.putArchiveEntry(entry);
    final Manifest manifest = new Manifest();
    manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
    manifest.getMainAttributes().putValue("Background-Process-Class",
            FakeBackgroundProcessImpl.class.getName());
    manifest.write(archive);
    archive.closeArchiveEntry();
    archive.close();
}

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);/*  w w  w.j  a  va2 s  .  c om*/

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

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

}

From source file:com.chnoumis.commons.zip.utils.ZipUtils.java

public static void zip(List<ZipInfo> zipInfos, OutputStream out) throws IOException, ArchiveException {

    ArchiveOutputStream os = null;

    try {// w w  w  . ja  v a 2s.c o m
        os = new ArchiveStreamFactory().createArchiveOutputStream("zip", out);

        for (ZipInfo zipInfo : zipInfos) {
            os.putArchiveEntry(new ZipArchiveEntry(zipInfo.getFileName()));
            InputStream o = null;
            if (zipInfo.getFileContent() != null) {
                o = new ByteArrayInputStream(zipInfo.getFileContent());
            } else {
                o = zipInfo.getInputStream();
            }
            IOUtils.copy(o, os);
            os.closeArchiveEntry();
        }
    } finally {
        if (os != null) {
            os.close();
        }
    }
    out.close();
}

From source file:com.openkm.misc.ZipTest.java

public void testApache() throws IOException, ArchiveException {
    log.debug("testApache()");
    File zip = File.createTempFile("apache_", ".zip");

    // Create zip
    FileOutputStream fos = new FileOutputStream(zip);
    ArchiveOutputStream aos = new ArchiveStreamFactory().createArchiveOutputStream("zip", fos);
    aos.putArchiveEntry(new ZipArchiveEntry("coeta"));
    aos.closeArchiveEntry();//from w w  w .  j  a  va2  s.  c  om
    aos.close();

    // Read zip
    FileInputStream fis = new FileInputStream(zip);
    ArchiveInputStream ais = new ArchiveStreamFactory().createArchiveInputStream("zip", fis);
    ZipArchiveEntry zae = (ZipArchiveEntry) ais.getNextEntry();
    assertEquals(zae.getName(), "coeta");
    ais.close();
}

From source file:at.beris.virtualfile.provider.LocalArchivedFileOperationProvider.java

@Override
public void create(FileModel model) throws IOException {
    try {/*from   ww  w . j a  v  a2  s .  co m*/
        // if not exists create UrlArchive
        // insert or update ArchiveEntry
        FileOutputStream fileOutputStream = new FileOutputStream(new java.io.File(model.getUrl().toURI()));
        ArchiveStreamFactory archiveStreamFactory = new ArchiveStreamFactory();
        ArchiveOutputStream archiveOutputStream = archiveStreamFactory
                .createArchiveOutputStream(ArchiveStreamFactory.ZIP, fileOutputStream);
        archiveOutputStream.close();

    } catch (ArchiveException | URISyntaxException e) {
        throw new IOException(e);
    }
}

From source file:algorithm.ZipPackaging.java

@Override
public File encapsulate(File carrier, List<File> payloadList) throws IOException {
    String outputName = getOutputFileName(carrier);
    int dotIndex = outputName.lastIndexOf('.');
    if (dotIndex > 0) {
        outputName = outputName.substring(0, dotIndex) + ".zip";
    }/*from  www .ja v  a  2  s .c  o  m*/
    File zipFile = new File(outputName);
    try {
        FileOutputStream outputStream = new FileOutputStream(zipFile);
        ArchiveOutputStream archiveOutputStream = new ArchiveStreamFactory()
                .createArchiveOutputStream(ArchiveStreamFactory.ZIP, outputStream);
        archiveFile(archiveOutputStream, carrier);
        for (File payload : payloadList) {
            archiveFile(archiveOutputStream, payload);
        }
        archiveOutputStream.close();
        outputStream.close();
    } catch (ArchiveException e) {
    }
    return zipFile;
}