Example usage for org.apache.commons.compress.archivers.zip ZipArchiveOutputStream ZipArchiveOutputStream

List of usage examples for org.apache.commons.compress.archivers.zip ZipArchiveOutputStream ZipArchiveOutputStream

Introduction

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

Prototype

public ZipArchiveOutputStream(File file) throws IOException 

Source Link

Document

Creates a new ZIP OutputStream writing to a File.

Usage

From source file:org.trustedanalytics.servicebroker.gearpump.service.file.FileHelper.java

public static byte[] prepareZipFile(byte[] zipFileTestContent) throws IOException {
    ByteArrayOutputStream byteOutput = null;
    ZipArchiveOutputStream zipOutput = null;
    try {//from   w  w w.jav  a  2  s . c  o m
        byteOutput = new ByteArrayOutputStream();
        zipOutput = new ZipArchiveOutputStream(byteOutput);
        ZipArchiveEntry entry = new ZipArchiveEntry(FILE_NAME);
        entry.setSize(zipFileTestContent.length);
        addArchiveEntry(zipOutput, entry, zipFileTestContent);
    } finally {
        zipOutput.close();
        byteOutput.close();
    }

    return byteOutput.toByteArray();
}

From source file:org.waarp.common.tar.ZipUtility.java

/**
 * Create a new Zip from a root directory
 * // w w w . ja  va2  s . c o  m
 * @param directory
 *            the base directory
 * @param filename
 *            the output filename
 * @param absolute
 *            store absolute filepath (from directory) or only filename
 * @return True if OK
 */
public static boolean createZipFromDirectory(String directory, String filename, boolean absolute) {
    File rootDir = new File(directory);
    File saveFile = new File(filename);
    // recursive call
    ZipArchiveOutputStream zaos;
    try {
        zaos = new ZipArchiveOutputStream(new FileOutputStream(saveFile));
    } catch (FileNotFoundException e) {
        return false;
    }
    try {
        recurseFiles(rootDir, rootDir, zaos, absolute);
    } catch (IOException e2) {
        try {
            zaos.close();
        } catch (IOException e) {
            // ignore
        }
        return false;
    }
    try {
        zaos.finish();
    } catch (IOException e1) {
        // ignore
    }
    try {
        zaos.flush();
    } catch (IOException e) {
        // ignore
    }
    try {
        zaos.close();
    } catch (IOException e) {
        // ignore
    }
    return true;
}

From source file:org.waarp.common.tar.ZipUtility.java

/**
 * Create a new Zip from an array of Files (only name of files will be used)
 * //from  ww w  .  j  a  v a2 s. c  o  m
 * @param files
 *            array of files to add
 * @param filename
 *            the output filename
 * @return True if OK
 */
public static boolean createZipFromFiles(File[] files, String filename) {
    File saveFile = new File(filename);
    ZipArchiveOutputStream zaos;
    try {
        zaos = new ZipArchiveOutputStream(new FileOutputStream(saveFile));
    } catch (FileNotFoundException e) {
        return false;
    }
    for (File file : files) {
        try {
            addFile(file, zaos);
        } catch (IOException e) {
            try {
                zaos.close();
            } catch (IOException e1) {
                // ignore
            }
            return false;
        }
    }
    try {
        zaos.finish();
    } catch (IOException e1) {
        // ignore
    }
    try {
        zaos.flush();
    } catch (IOException e) {
        // ignore
    }
    try {
        zaos.close();
    } catch (IOException e) {
        // ignore
    }
    return true;
}

From source file:org.wso2.carbon.connector.util.FileCompressUtil.java

/**
 * Compress the files based on the archive type
 * //from   w  w  w  .  j  a  v  a2 s  .c  o m
 * 
 * @param files
 * @param file
 * @param archiveType
 * @throws IOException
 */
public void compressFiles(Collection files, File file, ArchiveType archiveType) throws IOException {
    log.info("Compressing " + files.size() + " to " + file.getAbsoluteFile());
    // Create the output stream for the output file
    FileOutputStream fos;
    switch (archiveType) {
    case TAR_GZIP:
        fos = new FileOutputStream(new File(file.getCanonicalPath() + ".tar" + ".gz"));
        // Wrap the output file stream in streams that will tar and gzip
        // everything
        TarArchiveOutputStream taos = new TarArchiveOutputStream(
                new GZIPOutputStream(new BufferedOutputStream(fos)));
        // TAR has an 8 gig file limit by default, this gets around that
        taos.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_STAR);
        // to get past the 8 gig limit; TAR originally didn't support
        // long file names, so enable the
        // support for it
        taos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
        // Get to putting all the files in the compressed output file
        Iterator iterator = files.iterator();
        while (iterator.hasNext()) {
            File f = (File) iterator.next();
            addFilesToCompression(taos, f, ".", ArchiveType.TAR_GZIP);
            // do something to object here...
        }

        // Close everything up
        taos.close();
        fos.close();
        break;
    case ZIP:
        fos = new FileOutputStream(new File(file.getCanonicalPath() + ".zip"));
        // Wrap the output file stream in streams that will tar and zip
        // everything
        ZipArchiveOutputStream zaos = new ZipArchiveOutputStream(new BufferedOutputStream(fos));
        zaos.setEncoding("UTF-8");
        zaos.setCreateUnicodeExtraFields(ZipArchiveOutputStream.UnicodeExtraFieldPolicy.ALWAYS);

        // Get to putting all the files in the compressed output file
        Iterator iterator1 = files.iterator();
        while (iterator1.hasNext()) {
            File f = (File) iterator1.next();
            addFilesToCompression(zaos, f, ".", ArchiveType.ZIP);
            // do something to object here...
        }

        // Close everything up
        zaos.close();
        fos.close();
        break;
    }
}

From source file:org.xwiki.filemanager.internal.job.PackJob.java

@Override
protected void runInternal() throws Exception {
    Collection<Path> paths = getRequest().getPaths();
    if (paths == null) {
        return;/*from  w  w w  .  j ava 2 s  .  co m*/
    }

    File outputFile = getTemporaryFile(getRequest().getOutputFileReference());
    // TODO: Use java.util.zip.ZipOutputStream when moving to Java 7.
    // http://bugs.java.com/bugdatabase/view_bug.do?bug_id=4244499
    ZipArchiveOutputStream zip = new ZipArchiveOutputStream(outputFile);
    String pathPrefix = "";

    notifyPushLevelProgress(paths.size());

    try {
        for (Path path : paths) {
            pack(path, zip, pathPrefix);
            notifyStepPropress();
        }
    } finally {
        IOUtils.closeQuietly(zip);
        getStatus().setOutputFileSize(outputFile.length());
        notifyPopLevelProgress();
    }
}

From source file:org.xwiki.filter.xar.internal.output.XARWikiWriter.java

public XARWikiWriter(String name, XAROutputProperties xarProperties) throws FilterException {
    this.name = name;
    this.xarProperties = xarProperties;

    this.xarPackage = new XarPackage();

    this.xarPackage.setPackageName(this.name);
    this.xarPackage.setPackageDescription(xarProperties.getPackageDescription());
    this.xarPackage.setPackageLicense(xarProperties.getPackageLicense());
    this.xarPackage.setPackageAuthor(xarProperties.getPackageAuthor());
    this.xarPackage.setPackageVersion(xarProperties.getPackageVersion());
    this.xarPackage.setPackageBackupPack(xarProperties.isPackageBackupPack());
    this.xarPackage.setPackagePreserveVersion(xarProperties.isPreserveVersion());
    this.xarPackage.setPackageExtensionId(xarProperties.getPackageExtensionId());

    OutputTarget target = this.xarProperties.getTarget();

    try {/*from  w  w w. j  a  va  2  s  .  co m*/
        if (target instanceof FileOutputTarget && ((FileOutputTarget) target).getFile().isDirectory()) {
            this.zipStream = new ZipArchiveOutputStream(
                    new File(((FileOutputTarget) target).getFile(), name + ".xar"));
        } else if (target instanceof OutputStreamOutputTarget) {
            this.zipStream = new ZipArchiveOutputStream(
                    new CloseShieldOutputStream(((OutputStreamOutputTarget) target).getOutputStream()));
        } else {
            throw new FilterException(String.format("Unsupported output target [%s]. Only [%s] is supported",
                    target, OutputStreamOutputTarget.class));
        }
    } catch (IOException e) {
        throw new FilterException("Failed to create zip output stream", e);
    }

    this.zipStream.setEncoding("UTF8");

    // By including the unicode extra fields, it is possible to extract XAR-files containing documents with
    // non-ascii characters in the document name using InfoZIP, and the filenames will be correctly
    // converted to the character set of the local file system.
    this.zipStream.setCreateUnicodeExtraFields(ZipArchiveOutputStream.UnicodeExtraFieldPolicy.ALWAYS);
}

From source file:org.xwiki.wikistream.xar.internal.output.XARWikiWriter.java

public XARWikiWriter(String name, XAROutputProperties xarProperties) throws WikiStreamException {
    this.name = name;
    this.xarProperties = xarProperties;

    this.xarPackage = new XarPackage();

    this.xarPackage.setPackageName(this.name);
    this.xarPackage.setPackageDescription(xarProperties.getPackageDescription());
    this.xarPackage.setPackageLicense(xarProperties.getPackageLicense());
    this.xarPackage.setPackageAuthor(xarProperties.getPackageAuthor());
    this.xarPackage.setPackageVersion(xarProperties.getPackageVersion());
    this.xarPackage.setPackageBackupPack(xarProperties.isPackageBackupPack());
    this.xarPackage.setPreserveVersion(xarProperties.isPreserveVersion());
    this.xarPackage.setPackageExtensionId(xarProperties.getPackageExtensionId());

    OutputTarget target = this.xarProperties.getTarget();

    try {//from  w w w  . j  av a  2  s.  c om
        if (target instanceof FileOutputTarget && ((FileOutputTarget) target).getFile().isDirectory()) {
            this.zipStream = new ZipArchiveOutputStream(
                    new File(((FileOutputTarget) target).getFile(), name + ".xar"));
        } else if (target instanceof OutputStreamOutputTarget) {
            this.zipStream = new ZipArchiveOutputStream(
                    new CloseShieldOutputStream(((OutputStreamOutputTarget) target).getOutputStream()));
        } else {
            throw new WikiStreamException(
                    String.format("Unsupported output target [%s]. Only [%s] is supported", target,
                            OutputStreamOutputTarget.class));
        }
    } catch (IOException e) {
        throw new WikiStreamException("Failed to create zip output stream", e);
    }

    this.zipStream.setEncoding("UTF8");

    // By including the unicode extra fields, it is possible to extract XAR-files containing documents with
    // non-ascii characters in the document name using InfoZIP, and the filenames will be correctly
    // converted to the character set of the local file system.
    this.zipStream.setCreateUnicodeExtraFields(ZipArchiveOutputStream.UnicodeExtraFieldPolicy.ALWAYS);
}

From source file:org.zuinnote.hadoop.office.format.common.writer.msexcel.internal.EncryptedZipEntrySource.java

public void setInputStream(InputStream is) throws IOException {
    this.tmpFile = TempFile.createTempFile("hadoopoffice-protected", ".zip");

    ZipArchiveInputStream zis = new ZipArchiveInputStream(is);
    FileOutputStream fos = new FileOutputStream(tmpFile);
    ZipArchiveOutputStream zos = new ZipArchiveOutputStream(fos);
    ZipArchiveEntry ze;/*from   w w w .  j  ava2s. c om*/
    while ((ze = (ZipArchiveEntry) zis.getNextEntry()) != null) {
        // rewrite zip entries to match the size of the encrypted data (with padding)
        ZipArchiveEntry zeNew = new ZipArchiveEntry(ze.getName());
        zeNew.setComment(ze.getComment());
        zeNew.setExtra(ze.getExtra());
        zeNew.setTime(ze.getTime());
        zos.putArchiveEntry(zeNew);
        FilterOutputStream fos2 = new FilterOutputStream(zos) {
            // do not close underlyzing ZipOutputStream
            @Override
            public void close() {
            }
        };
        OutputStream nos;
        if (this.ciEncoder != null) { // encrypt if needed
            nos = new CipherOutputStream(fos2, this.ciEncoder);
        } else { // do not encrypt
            nos = fos2;
        }
        IOUtils.copy(zis, nos);
        nos.close();
        if (fos2 != null) {
            fos2.close();
        }
        zos.closeArchiveEntry();

    }
    zos.close();
    fos.close();
    zis.close();
    IOUtils.closeQuietly(is);
    this.zipFile = new ZipFile(this.tmpFile);

}

From source file:ThirdParty.zip.java

/**
 * Creates a zip file at the specified path with the contents of the specified directory.
 * @param directoryPath The path of the directory where the archive will be created. eg. c:/temp
 * @param zipPath The full path of the archive to create. eg. c:/temp/archive.zip
 * @throws IOException If anything goes wrong
 * @origin http://developer-tips.hubpages.com/hub/Zipping-and-Unzipping-Nested-Directories-in-Java-using-Apache-Commons-Compress
 * @date 2012-09-05/* ww w  .  j  a v  a  2 s . c  o  m*/
 * @author Robin Spark
 */
public static void createZip(File directoryPath, File zipPath) throws IOException {
    FileOutputStream fOut = null;
    BufferedOutputStream bOut = null;
    ZipArchiveOutputStream tOut = null;

    try {
        fOut = new FileOutputStream(zipPath);
        bOut = new BufferedOutputStream(fOut);
        tOut = new ZipArchiveOutputStream(bOut);
        addFileToZip(tOut, directoryPath, "");
    } finally {
        tOut.finish();
        tOut.close();
        bOut.close();
        fOut.close();
    }

}

From source file:util.OutstreamZipFile.java

/**
 * Creates a new temporary ZIP file in the target folder.
 *
 * @param parentDirName/*from   w w w. j a  v a2  s  .c o m*/
 *            Parent directory of new zip file
 * @param zipFileName
 *            New zip file to be created
 *
 * @throws IOException
 */
public OutstreamZipFile(String parentDirName, String zipFileName) throws IOException {
    File parentDir = new File(parentDirName + "/");
    if (parentDir.isDirectory()) {
        tempZipFile = new File(parentDir, zipFileName + TMP + ".zip");
        tempZipFile.createNewFile();
        if (tempZipFile.canWrite()) {
            zos = new ZipArchiveOutputStream(new BufferedOutputStream(new FileOutputStream(tempZipFile)));
            zipFile = new File(parentDir, zipFileName + ".zip");
        }
    }
}