Example usage for java.util.zip ZipOutputStream closeEntry

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

Introduction

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

Prototype

public void closeEntry() throws IOException 

Source Link

Document

Closes the current ZIP entry and positions the stream for writing the next entry.

Usage

From source file:br.univali.celine.lms.utils.zip.Zip.java

public void zip(ArrayList<String> fileList, File destFile, int compressionLevel) throws Exception {

    if (destFile.exists())
        throw new Exception("File " + destFile.getName() + " already exists!!");

    int fileLength;
    byte[] buffer = new byte[4096];

    FileOutputStream fos = new FileOutputStream(destFile);
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    ZipOutputStream zipFile = new ZipOutputStream(bos);
    zipFile.setLevel(compressionLevel);//from w w w .j  a v a  2 s .com

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

        FileInputStream fis = new FileInputStream(fileList.get(i));
        BufferedInputStream bis = new BufferedInputStream(fis);
        ZipEntry ze = new ZipEntry(FilenameUtils.getName(fileList.get(i)));
        zipFile.putNextEntry(ze);

        while ((fileLength = bis.read(buffer, 0, 4096)) > 0)
            zipFile.write(buffer, 0, fileLength);

        zipFile.closeEntry();
        bis.close();
    }

    zipFile.close();

}

From source file:ch.randelshofer.cubetwister.PreferencesTemplatesPanel.java

private void exportInternalTemplate(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exportInternalTemplate
    if (exportFileChooser == null) {
        exportFileChooser = new JFileChooser();
        exportFileChooser.setFileFilter(new ExtensionFileFilter("zip", "Zip Archive"));
        exportFileChooser.setSelectedFile(
                new File(userPrefs.get("cubetwister.exportedHTMLTemplate", "CubeTwister HTML-Template.zip")));
        exportFileChooser.setApproveButtonText(labels.getString("filechooser.export"));
    }/* www  .ja  va 2  s. com*/
    if (JFileChooser.APPROVE_OPTION == exportFileChooser.showSaveDialog(this)) {
        userPrefs.put("cubetwister.exportedHTMLTemplate", exportFileChooser.getSelectedFile().getPath());
        final File target = exportFileChooser.getSelectedFile();
        new BackgroundTask() {

            @Override
            public void construct() throws IOException {
                if (!target.getParentFile().exists()) {
                    target.getParentFile().mkdirs();
                }
                InputStream in = null;
                OutputStream out = null;
                try {
                    in = getClass().getResourceAsStream("/htmltemplates.tar.bz");
                    out = new FileOutputStream(target);
                    exportTarBZasZip(in, out);
                } finally {
                    try {
                        if (in != null) {
                            in.close();
                        }
                    } finally {
                        if (out != null) {
                            out.close();
                        }
                    }
                }
            }

            private void exportTarBZasZip(InputStream in, OutputStream out) throws IOException {
                ZipOutputStream zout = new ZipOutputStream(out);
                TarInputStream tin = new TarInputStream(new BZip2CompressorInputStream(in));

                for (TarArchiveEntry inEntry = tin.getNextEntry(); inEntry != null;) {
                    ZipEntry outEntry = new ZipEntry(inEntry.getName());
                    zout.putNextEntry(outEntry);
                    if (!inEntry.isDirectory()) {
                        Files.copyStream(tin, zout);
                    }
                    zout.closeEntry();
                }
                zout.finish();
            }
        }.start();
    }
}

From source file:edu.stanford.epad.plugins.qifpwrapper.QIFPHandler.java

public static File generateZipFile(List<File> files, String dirPath) {

    File dir_file = new File(dirPath);
    File zip_file = new File(dirPath + "/temp_" + (fileNum++) + ".zip");
    int dir_l = dir_file.getAbsolutePath().length();

    FileOutputStream fos = null;/*from w w w.  ja va  2 s.co m*/
    try {
        fos = new FileOutputStream(zip_file);
    } catch (FileNotFoundException e1) {
        log.warning("File not found", e1);
        return null;
    }

    ZipOutputStream zipout = new ZipOutputStream(fos);
    zipout.setLevel(1);
    for (int i = 0; i < files.size(); i++) {
        File f = (File) files.get(i);
        if (f.canRead()) {
            log.info("Adding file: " + f.getAbsolutePath());
            try {
                zipout.putNextEntry(new ZipEntry(f.getAbsolutePath().substring(dir_l + 1)));
            } catch (Exception e) {
                log.warning("Error adding to zip file", e);
                return null;
            }
            BufferedInputStream fr;
            try {
                fr = new BufferedInputStream(new FileInputStream(f));

                byte buffer[] = new byte[0xffff];
                int b;
                while ((b = fr.read(buffer)) != -1)
                    zipout.write(buffer, 0, b);

                fr.close();
                zipout.closeEntry();

            } catch (Exception e) {
                log.warning("Error closing zip file", e);
                return null;
            }
        }
    }

    try {
        zipout.finish();
        fos.flush();
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
    log.info("file zipped and returning as " + zip_file.getAbsolutePath());
    return zip_file;

}

From source file:ZipUtilTest.java

public void testUnpackEntryFromFile() throws IOException {
    final String name = "foo";
    final byte[] contents = "bar".getBytes();

    File file = File.createTempFile("temp", null);
    try {/* w  ww  . j  ava  2  s. co m*/
        // Create the ZIP file
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(file));
        try {
            zos.putNextEntry(new ZipEntry(name));
            zos.write(contents);
            zos.closeEntry();
        } finally {
            IOUtils.closeQuietly(zos);
        }

        // Test the ZipUtil
        byte[] actual = ZipUtil.unpackEntry(file, name);
        assertNotNull(actual);
        assertEquals(new String(contents), new String(actual));
    } finally {
        FileUtils.deleteQuietly(file);
    }
}

From source file:ZipUtilTest.java

public void testUnpackEntryFromStream() throws IOException {
    final String name = "foo";
    final byte[] contents = "bar".getBytes();

    File file = File.createTempFile("temp", null);
    try {/*from  w  w w .  java2  s.com*/
        // Create the ZIP file
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(file));
        try {
            zos.putNextEntry(new ZipEntry(name));
            zos.write(contents);
            zos.closeEntry();
        } finally {
            IOUtils.closeQuietly(zos);
        }

        FileInputStream fis = new FileInputStream(file);
        // Test the ZipUtil
        byte[] actual = ZipUtil.unpackEntry(fis, name);
        assertNotNull(actual);
        assertEquals(new String(contents), new String(actual));
    } finally {
        FileUtils.deleteQuietly(file);
    }
}

From source file:ZipUtilTest.java

public void testUnpackEntryFromStreamToFile() throws IOException {
    final String name = "foo";
    final byte[] contents = "bar".getBytes();

    File file = File.createTempFile("temp", null);
    try {// w ww.  ja va2s  .  c  o  m
        // Create the ZIP file
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(file));
        try {
            zos.putNextEntry(new ZipEntry(name));
            zos.write(contents);
            zos.closeEntry();
        } finally {
            IOUtils.closeQuietly(zos);
        }

        FileInputStream fis = new FileInputStream(file);

        File outputFile = File.createTempFile("temp-output", null);

        boolean result = ZipUtil.unpackEntry(fis, name, outputFile);
        assertTrue(result);

        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(outputFile));
        byte[] actual = new byte[1024];
        int read = bis.read(actual);
        bis.close();

        assertEquals(new String(contents), new String(actual, 0, read));
    } finally {
        FileUtils.deleteQuietly(file);
    }
}

From source file:freenet.client.async.ContainerInserter.java

private String createZipBucket(OutputStream os) throws IOException {
    if (logMINOR)
        Logger.minor(this, "Create a ZIP Bucket");

    ZipOutputStream zos = new ZipOutputStream(os);
    try {/*from   w ww.  j a va  2 s  .c o m*/
        ZipEntry ze;

        for (ContainerElement ph : containerItems) {
            ze = new ZipEntry(ph.targetInArchive);
            ze.setTime(0);
            zos.putNextEntry(ze);
            BucketTools.copyTo(ph.data, zos, ph.data.size());
            zos.closeEntry();
        }
    } finally {
        zos.close();
    }

    return ARCHIVE_TYPE.ZIP.mimeTypes[0];
}

From source file:com.googlecode.clearnlp.component.AbstractStatisticalComponent.java

protected void saveDefaultConfiguration(ZipOutputStream zout, String entryName) throws Exception {
    zout.putNextEntry(new ZipEntry(entryName));
    PrintStream fout = UTOutput.createPrintBufferedStream(zout);
    LOG.info("Saving configuration.\n");

    fout.println(s_models.length);/*w w w .  j  a  va 2  s . c  o m*/

    fout.flush();
    zout.closeEntry();
}

From source file:org.carlspring.strongbox.artifact.generator.NugetPackageGenerator.java

private void addNugetNuspecFile(Nuspec nuspec, ZipOutputStream zos) throws IOException, JAXBException {
    ZipEntry ze = new ZipEntry(nuspec.getId() + ".nuspec");
    zos.putNextEntry(ze);//from  w  w w  . j  a v  a  2  s  . c o m

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    nuspec.saveTo(baos);

    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());

    byte[] buffer = new byte[4096];
    int len;
    while ((len = bais.read(buffer)) > 0) {
        zos.write(buffer, 0, len);
    }

    bais.close();
    zos.closeEntry();
}

From source file:com.mgmtp.jfunk.common.util.ExtendedFile.java

private void zip(String prefix, final File file, final ZipOutputStream zipOut) throws IOException {
    if (file.isDirectory()) {
        prefix = prefix + file.getName() + '/';
        for (File child : file.listFiles()) {
            zip(prefix, child, zipOut);//from  w ww  .  j av a  2s. c om
        }
    } else {
        FileInputStream in = null;
        try {
            in = new FileInputStream(file);
            zipOut.putNextEntry(new ZipEntry(prefix + file.getName()));
            IOUtils.copy(in, zipOut);
        } finally {
            IOUtils.closeQuietly(in);
            zipOut.flush();
            zipOut.closeEntry();
        }
    }
}