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:ch.ivyteam.ivy.maven.util.ClasspathJar.java

public void create(List<String> classpathEntries) throws IOException {
    jar.getParentFile().mkdir();//from   www.  j  a va2  s.c om
    try (ZipOutputStream zipStream = new ZipOutputStream(new FileOutputStream(jar))) {
        String name = StringUtils.substringBeforeLast(jar.getName(), ".");
        writeManifest(name, zipStream, classpathEntries);
    }
}

From source file:be.fedict.eid.dss.document.zip.ZIPSignatureOutputStream.java

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

    byte[] signatureData = toByteArray();

    /*/*w  ww .j a  va 2s. c  om*/
     * 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(ODFUtil.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 ZIP package.
     */
    zipEntry = new ZipEntry(ODFUtil.SIGNATURE_FILE);
    LOG.debug("writing " + zipEntry.getName());
    zipOutputStream.putNextEntry(zipEntry);
    IOUtils.write(signatureData, zipOutputStream);
    zipOutputStream.close();
}

From source file:de.tudarmstadt.ukp.clarin.webanno.api.dao.ZipUtils.java

/**
 * While exporting annotation documents, some of the writers generate multiple outputs, e.g. a
 * type system file in addition to the annotation data. This method generates a zip file if the
 * exported file do contain multiple file output
 * /*from w  ww .j a va2 s.  co m*/
 * @param srcFolder source folder.
 * @param destZipFile target folder.
 * @throws IOException if an I/O error occurs.
 */
public static void zipFolder(File srcFolder, File destZipFile) throws IOException {
    ZipOutputStream zip = null;
    try {
        zip = new ZipOutputStream(new FileOutputStream(destZipFile));

        for (File file : srcFolder.getAbsoluteFile().listFiles()) {
            addToZip(zip, srcFolder.getAbsoluteFile(), file);
        }
        zip.flush();
    } finally {
        closeQuietly(zip);
    }
}

From source file:com.jcalvopinam.core.Zipping.java

private static void splitAndZipFile(File inputFile, int bufferSize, CustomFile customFile) throws IOException {

    int counter = 1;
    byte[] bufferPart;
    byte[] buffer = new byte[bufferSize];
    File newFile;//  ww  w .  j ava  2 s .  com
    FileInputStream fileInputStream;
    ZipOutputStream out;
    String temporalName;
    String outputFileName;

    try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(inputFile))) {

        int tmp;

        System.out.println("Please wait while the file is split:");
        while ((tmp = bis.read(buffer)) > 0) {
            temporalName = String.format("%s.%03d", customFile.getFileName(), counter);
            newFile = new File(inputFile.getParent(), temporalName);

            try (FileOutputStream fileOutputStream = new FileOutputStream(newFile)) {
                fileOutputStream.write(buffer, 0, tmp);
            }

            fileInputStream = new FileInputStream(newFile);//file001.zip
            outputFileName = String.format("%s%s_%03d%s", customFile.getPath(), customFile.getFileName(),
                    counter, Extensions.ZIP.getExtension());
            out = new ZipOutputStream(new FileOutputStream(outputFileName));

            out.putNextEntry(new ZipEntry(customFile.getFileNameExtension()));

            bufferPart = new byte[CustomFile.BYTE_SIZE];
            int count;

            while ((count = fileInputStream.read(bufferPart)) > 0) {
                out.write(bufferPart, 0, count);
                System.out.print(".");
            }

            counter++;
            fileInputStream.close();
            out.close();

            FileUtils.deleteQuietly(newFile);
        }
    }

    System.out.println("\nEnded process!");
}

From source file:com.orange.ocara.model.export.docx.DocxWriter.java

/**
 * Transforms the unzipped directory (templateDirectory) to exportFile docx file (exportFile) using exporter.
 *
 * @throws DocxWriterException If failed exportFile export
 *///from ww w .  j  ava  2  s.c o m
public void export() throws DocxWriterException {

    if (!checkDirectory(workingDirectory)) {
        throw new DocxWriterException("Working directory could not be checked");
    }

    if (!checkFile(exportFile)) {
        throw new DocxWriterException("Export File could not be checked");
    }

    if (exporter == null) {
        throw new DocxWriterException("Exporter could not be null");
    }

    // compile
    try {
        exporter.compile(workingDirectory);
    } catch (DocxExporterException e) {
        throw new DocxWriterException("Failed to compile exporter", e);
    }

    // zip templateDirectory to exportFile
    final String rootPath = FilenameUtils.normalize(workingDirectory.getPath() + "/");
    ZipOutputStream zos = null;

    try {
        zos = new ZipOutputStream(new FileOutputStream(exportFile));
        zipDirectory(rootPath, workingDirectory, zos);
    } catch (IOException e) {
        throw new DocxWriterException("Failed to zip template directory", e);
    } finally {
        IOUtils.closeQuietly(zos);
    }

}

From source file:de.micromata.genome.gdbfs.ZipWriteFileSystem.java

/**
 * Instantiates a new zip write file system.
 *
 * @param os the os//from   w w w  .  j  a v a  2  s  . co  m
 */
public ZipWriteFileSystem(OutputStream os) {
    this.outputStream = os;
    zout = new ZipOutputStream(outputStream);
}

From source file:io.inkstand.it.StaticContentITCase.java

private static File createZip(final File file, final String... resources) throws IOException {

    try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream((file)))) {

        for (String resourceName : resources) {

            final InputStream is = StaticContentITCase.class.getResourceAsStream(resourceName);
            final ZipEntry zipEntry = new ZipEntry(resourceName);
            zos.putNextEntry(zipEntry);/*from  ww w.j a va  2s .com*/
            IOUtils.copy(is, zos);
            zos.closeEntry();
        }
    }
    return file;
}

From source file:apim.restful.exportimport.utils.ArchiveGenerator.java

public static void writeZipFile(File directoryToZip, List<File> fileList) {

    try {/*from  w ww .  j  ava2 s  .  co m*/
        FileOutputStream fos = new FileOutputStream(directoryToZip.getPath() + ".zip");
        ZipOutputStream zos = new ZipOutputStream(fos);

        for (File file : fileList) {
            if (!file.isDirectory()) { // we only zip files, not directories
                addToZip(directoryToZip, file, zos);
            }
        }

        zos.close();
        fos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.espringtran.compressor4j.processor.GzipProcessor.java

/**
 * Compress data//  w  ww.ja  va 2  s  .c  om
 * 
 * @param fileCompressor
 *            FileCompressor object
 * @return
 * @throws Exception
 */
@Override
public byte[] compressData(FileCompressor fileCompressor) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GzipCompressorOutputStream cos = new GzipCompressorOutputStream(baos);
    ZipOutputStream zos = new ZipOutputStream(cos);
    try {
        zos.setLevel(fileCompressor.getLevel().getValue());
        zos.setMethod(ZipOutputStream.DEFLATED);
        zos.setComment(fileCompressor.getComment());
        for (BinaryFile binaryFile : fileCompressor.getMapBinaryFile().values()) {
            zos.putNextEntry(new ZipEntry(binaryFile.getDesPath()));
            zos.write(binaryFile.getData());
            zos.closeEntry();
        }
        zos.flush();
        zos.finish();
    } catch (Exception e) {
        FileCompressor.LOGGER.error("Error on compress data", e);
    } finally {
        zos.close();
        cos.close();
        baos.close();
    }
    return baos.toByteArray();
}

From source file:md.archivers.ZIPArchiver.java

private void zipDirectory(File zipFile, File directory) throws FileNotFoundException, IOException {
    ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile));
    FileInputStream fin = null;//from  ww  w .  j av a  2s .  co  m
    for (File d : directory.listFiles()) {
        for (File f : d.listFiles()) {
            zos.putNextEntry(new ZipEntry(createNameForZip(f)));
            try {
                fin = new FileInputStream(f);
                IOUtils.copy(fin, zos);
            } finally {
                if (fin != null)
                    fin.close();
            }
            zos.closeEntry();
        }
    }
    zos.close();
}