Example usage for java.util.zip ZipOutputStream ZipOutputStream

List of usage examples for java.util.zip ZipOutputStream ZipOutputStream

Introduction

In this page you can find the example usage for java.util.zip ZipOutputStream ZipOutputStream.

Prototype

public ZipOutputStream(OutputStream out) 

Source Link

Document

Creates a new ZIP output stream.

Usage

From source file:abfab3d.io.output.SVXWriter.java

/**
 * Writes a grid out to an svx file/* w ww . java2s .  c  o  m*/
 * @param grid
 * @param os
 */
public void write(AttributeGrid grid, OutputStream os) {
    ZipOutputStream zos = null;

    try {

        zos = new ZipOutputStream(os);

        ZipEntry zentry = new ZipEntry("manifest.xml");
        zos.putNextEntry(zentry);
        writeManifest(grid, zos);
        zos.closeEntry();

        SlicesWriter sw = new SlicesWriter();
        AttributeDesc attDesc = grid.getAttributeDesc();

        for (int i = 0; i < attDesc.size(); i++) {

            AttributeChannel channel = attDesc.getChannel(i);
            String channelPattern = channel.getName() + "/" + "slice%04d.png";
            sw.writeSlices(grid, zos, channelPattern, 0, 0, getSlicesCount(grid), m_orientation,
                    channel.getBitCount(), channel);
        }
    } catch (IOException ioe) {

        ioe.printStackTrace();

    } finally {
        IOUtils.closeQuietly(zos);
    }

}

From source file:org.pdfgal.pdfgalweb.utils.impl.ZipUtilsImpl.java

@Override
public String zipFiles(final List<String> urisList, final String fileName)
        throws FileNotFoundException, IOException {

    String generatedFileName = null;

    if (CollectionUtils.isNotEmpty(urisList) && StringUtils.isNotEmpty(fileName)) {

        // Autogeneration of file name
        generatedFileName = this.fileUtils.getAutogeneratedName(fileName + ".zip");

        // ZIP file is created.
        final FileOutputStream fos = new FileOutputStream(generatedFileName);
        final ZipOutputStream zos = new ZipOutputStream(fos);

        // Files are added to ZIP.
        for (final String uri : urisList) {
            if (StringUtils.isNotEmpty(uri)) {
                this.addToZipFile(uri, zos, fileName);
            }/*from   www . j  a v a2  s .c  o  m*/
        }

        zos.close();
        fos.close();
    }

    return generatedFileName;
}

From source file:ZipSourceCallable.java

@Override
public String invoke(File f, VirtualChannel channel) throws IOException {
    String sourceFilePath = workspace.getRemote();

    // Create a temp file to zip into so we do not zip ourselves
    File tempFile = File.createTempFile(f.getName(), null, null);
    try (OutputStream zipFileOutputStream = new FileOutputStream(tempFile)) {
        try (ZipOutputStream out = new ZipOutputStream(zipFileOutputStream)) {
            zipSource(workspace, sourceFilePath, out, sourceFilePath);
        }/*from  w  w  w  .j  av a  2s .  c  o  m*/
    } catch (IOException e) {
        throw e;
    } catch (Exception e) {
        throw new IOException(e);
    }

    // Copy zip to the location we expect it to be in
    FileUtils.copyFile(tempFile, f);

    try {
        tempFile.delete();
    } catch (Exception e) {
        // If this fails, the file will just be cleaned up
        // by the system later.  We are just trying to be
        // good citizens here.
    }

    String zipFileMD5;

    // Build MD5 checksum before returning
    try (InputStream zipFileInputStream = new FileInputStream(f)) {
        zipFileMD5 = new String(encodeBase64(DigestUtils.md5(zipFileInputStream)), Charsets.UTF_8);
        return zipFileMD5;
    }
}

From source file:com.xpn.xwiki.plugin.packaging.AbstractPackageTest.java

/**
 * Create a XAR file using java.util.zip.
 *
 * @param docs The documents to include.
 * @param encodings The charset for each document.
 * @param packageXmlEncoding The encoding of package.xml
 * @return the XAR file as a byte array.
 *///from ww w .  java  2 s  .  co m
protected byte[] createZipFile(XWikiDocument docs[], String[] encodings, String packageXmlEncoding)
        throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ZipOutputStream zos = new ZipOutputStream(baos);
    ZipEntry zipp = new ZipEntry("package.xml");
    zos.putNextEntry(zipp);
    zos.write(getEncodedByteArray(getPackageXML(docs, packageXmlEncoding), packageXmlEncoding));
    for (int i = 0; i < docs.length; i++) {
        String zipEntryName = docs[i].getSpace() + "/" + docs[i].getName();
        if (docs[i].getTranslation() != 0) {
            zipEntryName += "." + docs[i].getLanguage();
        }
        ZipEntry zipe = new ZipEntry(zipEntryName);
        zos.putNextEntry(zipe);
        String xmlCode = docs[i].toXML(false, false, false, false, getContext());
        zos.write(getEncodedByteArray(xmlCode, encodings[i]));
    }
    zos.finish();
    zos.close();
    return baos.toByteArray();
}

From source file:org.cloudfoundry.client.lib.io.DynamicZipInputStream.java

/**
 * Create a new {@link DynamicZipInputStream} instance.
 *
 * @param entries the zip entries that should be written to the stream
 *///from ww  w. j  a v a2s .c o m
public DynamicZipInputStream(Iterable<Entry> entries) {
    Assert.notNull(entries, "Entries must not be null");
    this.zipStream = new ZipOutputStream(getOutputStream());
    this.entries = entries.iterator();
}

From source file:dk.netarkivet.common.utils.ZipUtils.java

/** Zip the contents of a directory into a file.
 *  Does *not* zip recursively./*w  w  w .  j a  v a 2  s.c  om*/
 *
 * @param dir The directory to zip.
 * @param into The (zip) file to create.  The name should typically end
 * in .zip, but that is not required.
 */
public static void zipDirectory(File dir, File into) {
    ArgumentNotValid.checkNotNull(dir, "File dir");
    ArgumentNotValid.checkNotNull(into, "File into");
    ArgumentNotValid.checkTrue(dir.isDirectory(), "directory '" + dir + "' to zip is not a directory");
    ArgumentNotValid.checkTrue(into.getAbsoluteFile().getParentFile().canWrite(),
            "cannot write to '" + into + "'");

    File[] files = dir.listFiles();
    FileOutputStream out;
    try {
        out = new FileOutputStream(into);
    } catch (IOException e) {
        throw new IOFailure("Error creating ZIP outfile file '" + into + "'", e);
    }
    ZipOutputStream zipout = new ZipOutputStream(out);
    try {
        try {
            for (File f : files) {
                if (f.isFile()) {
                    ZipEntry entry = new ZipEntry(f.getName());
                    zipout.putNextEntry(entry);
                    FileUtils.writeFileToStream(f, zipout);
                } // Not doing directories yet.
            }
        } finally {
            zipout.close();
        }
    } catch (IOException e) {
        throw new IOFailure("Failed to zip directory '" + dir + "'", e);
    }
}

From source file:apim.restful.importexport.utils.ArchiveGeneratorUtil.java

/**
 * Generate archive file/*  w  w  w. j ava2s . co m*/
 *
 * @param directoryToZip Location of the archive
 * @param fileList       List of files to be included in the archive
 * @throws APIExportException If an error occurs while adding files to the archive
 */
private static void writeArchiveFile(File directoryToZip, List<File> fileList) throws APIExportException {

    FileOutputStream fileOutputStream = null;
    ZipOutputStream zipOutputStream = null;

    try {
        fileOutputStream = new FileOutputStream(directoryToZip.getPath() + ".zip");
        zipOutputStream = new ZipOutputStream(fileOutputStream);
        for (File file : fileList) {
            if (!file.isDirectory()) {
                addToArchive(directoryToZip, file, zipOutputStream);
            }
        }

    } catch (IOException e) {
        log.error("I/O error while adding files to archive" + e.getMessage());
        throw new APIExportException("I/O error while adding files to archive", e);
    } finally {
        IOUtils.closeQuietly(zipOutputStream);
        IOUtils.closeQuietly(fileOutputStream);
    }
}

From source file:be.fedict.eid.applet.service.signer.asic.ASiCSignatureOutputStream.java

@Override
public void close() throws IOException {
    super.close();

    byte[] signatureData = toByteArray();

    /*/*from w ww .j  av  a  2s .  c o  m*/
     * Copy the original ZIP content.
     */
    ZipOutputStream zipOutputStream = new ZipOutputStream(this.targetOutputStream);
    ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(this.originalZipFile));
    ZipEntry zipEntry;
    while (null != (zipEntry = zipInputStream.getNextEntry())) {
        if (!zipEntry.getName().equals(ASiCUtil.SIGNATURE_FILE)) {
            ZipEntry newZipEntry = new ZipEntry(zipEntry.getName());
            zipOutputStream.putNextEntry(newZipEntry);
            LOG.debug("copying " + zipEntry.getName());
            IOUtils.copy(zipInputStream, zipOutputStream);
        }
    }
    zipInputStream.close();

    /*
     * Add the XML signature file to the ASiC package.
     */
    zipEntry = new ZipEntry(ASiCUtil.SIGNATURE_FILE);
    LOG.debug("writing " + zipEntry.getName());
    zipOutputStream.putNextEntry(zipEntry);
    IOUtils.write(signatureData, zipOutputStream);
    zipOutputStream.close();
}

From source file:au.com.permeance.liferay.util.zip.ZipWriter.java

public ZipWriter() throws IOException {
    String tempFilePrefix = PortalUUIDUtil.generate();
    String tempFileSuffix = ZIP_FILE_EXT;
    this.zipFile = File.createTempFile(tempFilePrefix, tempFileSuffix);
    this.zos = new ZipOutputStream(new FileOutputStream(this.zipFile));
}

From source file:com.c4om.autoconf.ulysses.configanalyzer.packager.ZipConfigurationPackager.java

/**
 * This implementation compresses files into a ZIP file
 * @see com.c4om.autoconf.ulysses.interfaces.configanalyzer.packager.CompressedFileConfigurationPackager#compressFiles(java.io.File, java.util.List, java.io.File)
 *//*w  w w  . j a va2 s . c om*/
@Override
protected void compressFiles(File actualRootFolder, List<String> entries, File zipFile)
        throws FileNotFoundException, IOException {
    //We use the FileUtils method so that necessary directories are automatically created.
    FileOutputStream zipFOS = FileUtils.openOutputStream(zipFile);
    ZipOutputStream zos = new ZipOutputStream(zipFOS);
    for (String entry : entries) {
        ZipEntry ze = new ZipEntry(entry);
        zos.putNextEntry(ze);
        FileInputStream sourceFileIN = new FileInputStream(
                actualRootFolder.getAbsolutePath() + File.separator + entry);
        IOUtils.copy(sourceFileIN, zos);
        sourceFileIN.close();
    }
    zos.closeEntry();
    zos.close();
}