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:net.bpelunit.util.ZipUtil.java

public static void zipDirectory(File directory, File zipFile) throws IOException {
    @SuppressWarnings("unchecked")
    Collection<File> files = FileUtils.listFiles(directory, null, true);

    FileOutputStream fzos = null;
    ZipOutputStream zos = null;// w w w . j a v a2 s. c  om
    try {
        fzos = new FileOutputStream(zipFile);
        zos = new ZipOutputStream(fzos);

        for (File f : files) {
            String fileNameInZIP = directory.toURI().relativize(f.toURI()).getPath();
            ZipEntry zipEntry = new ZipEntry(fileNameInZIP);
            zos.putNextEntry(zipEntry);
            FileInputStream fileInputStream = new FileInputStream(f);
            try {
                IOUtils.copy(fileInputStream, zos);
            } finally {
                IOUtils.closeQuietly(fileInputStream);
            }
        }
    } finally {
        IOUtils.closeQuietly(zos);
        IOUtils.closeQuietly(fzos);
    }
}

From source file:Main.java

public static File UpdateZipFromPath(String sZipFile, String sPath) throws Exception {
    File tmpFile = File.createTempFile("z4zip-tmp-", ".zip");
    tmpFile.deleteOnExit();/*from   ww  w  .jav  a  2  s  . com*/
    ZipFile inZip = new ZipFile(sZipFile);
    ZipOutputStream outZip = new ZipOutputStream(new FileOutputStream(tmpFile.getPath()));

    Enumeration<? extends ZipEntry> entries = inZip.entries();
    while (entries.hasMoreElements()) {
        ZipEntry e = entries.nextElement();
        outZip.putNextEntry(e);
        if (!e.isDirectory()) {
            File f = new File(sPath + "/" + e.getName());
            if (f.exists()) {
                copy(new FileInputStream(f.getPath()), outZip);
            } else {
                copy(inZip.getInputStream(e), outZip);
            }
        }
        outZip.closeEntry();
    }
    inZip.close();
    outZip.close();
    return tmpFile;
}

From source file:Main.java

/**
 * Compress files to a zip//from w w w  .  ja  v a 2s  . c  om
 * @param zip
 * @param files
 * @throws IOException 
 */
public static void compressToZip(File zip, File[] files) throws IOException {
    byte data[] = new byte[BUFFER];
    FileOutputStream fozip = new FileOutputStream(zip);
    ZipOutputStream zo = new ZipOutputStream(new BufferedOutputStream(fozip));
    for (int i = 0; i < files.length; i++) {
        System.out.println("Adding:" + files[i]);
        FileInputStream fi = new FileInputStream(files[i]);
        BufferedInputStream origin = new BufferedInputStream(fi, BUFFER);
        ZipEntry zipentry = new ZipEntry(files[i].getName());
        zo.putNextEntry(zipentry);
        int count;
        while ((count = origin.read(data, 0, BUFFER)) != -1) {
            zo.write(data, 0, count);
        }
        origin.close();
    }
    zo.close();
}

From source file:com.jaspersoft.jasperserver.jrsh.common.ZipUtil.java

public static File pack(String directory) {
    File dir = new File(directory);
    if (!dir.isDirectory()) {
        throw new DirectoryDoesNotExistException(directory);
    }//from  w ww.j  av a  2 s  . c  om
    directory = StringUtils.chomp(directory, separator);
    String outputFileName = directory.concat(".zip");
    try {
        File arch = new File(outputFileName);
        FileOutputStream fos = null;
        fos = new FileOutputStream(arch);
        ZipOutputStream zos = new ZipOutputStream(fos);
        addFiles(zos, directory, directory);
        zos.close();
        return arch;
    } catch (Exception unimportant) {
        throw new CannotPackDirectoryException();
    }
}

From source file:com.autentia.web.rest.wadl.zipper.Zip.java

Zip(OutputStream outputStream) {
    zipOutputStream = new ZipOutputStream(outputStream);
}

From source file:jodtemplate.io.ZipWriter.java

public void zipResources(final Resources resources, final OutputStream output) throws IOException {
    try (final ZipOutputStream zos = new ZipOutputStream(output);
            final BufferedOutputStream bos = new BufferedOutputStream(zos)) {
        for (Resource resource : resources.getResources()) {
            zos.putNextEntry(new ZipEntry(resource.getName()));
            try (final InputStream is = resource.getInputStream()) {
                zos.write(IOUtils.toByteArray(is));
            }/*  w ww  .  j  a  va  2  s.co  m*/
            zos.closeEntry();
        }
    }
}

From source file:net.grinder.util.LogCompressUtil.java

/**
 * Compress multiple Files./*from w ww  .j  a  v a 2 s.  c  o  m*/
 * 
 * @param logFiles
 *            files to be compressed
 * @return compressed file byte array
 */
public static byte[] compressFile(File[] logFiles) {
    FileInputStream fio = null;
    ByteArrayOutputStream out = null;
    ZipOutputStream zos = null;
    try {

        out = new ByteArrayOutputStream();
        zos = new ZipOutputStream(out);
        for (File each : logFiles) {
            try {
                fio = new FileInputStream(each);
                ZipEntry zipEntry = new ZipEntry(each.getName());
                zipEntry.setTime(each.lastModified());
                zos.putNextEntry(zipEntry);
                byte[] buffer = new byte[COMPRESS_BUFFER_SIZE];
                int count = 0;
                while ((count = fio.read(buffer, 0, COMPRESS_BUFFER_SIZE)) != -1) {
                    zos.write(buffer, 0, count);
                }
                zos.closeEntry();
            } catch (IOException e) {
                LOGGER.error("Error occurs while compress {}", each.getAbsolutePath());
                LOGGER.error("Details", e);
            } finally {
                IOUtils.closeQuietly(fio);
            }
        }
        zos.finish();
        zos.flush();
        return out.toByteArray();
    } catch (IOException e) {
        LOGGER.info("Error occurs while compress log : {} ", e.getMessage());
        LOGGER.debug("Details", e);
        return null;
    } finally {
        IOUtils.closeQuietly(zos);
        IOUtils.closeQuietly(fio);
        IOUtils.closeQuietly(out);
    }
}

From source file:net.firejack.platform.core.utils.ArchiveUtils.java

/**
 * @param basePath//  w w  w.  j  a  v  a  2s.c o m
 * @param zipPath
 * @param filePaths
 * @throws java.io.IOException
 */
public static void zip(File basePath, File zipPath, Map<String, String> filePaths) throws IOException {
    BufferedInputStream origin = null;
    ZipOutputStream out = null;
    FileOutputStream dest = null;
    try {
        dest = new FileOutputStream(zipPath);
        out = new ZipOutputStream(new BufferedOutputStream(dest));
        //out.setMethod(ZipOutputStream.DEFLATED);
        byte data[] = new byte[BUFFER];
        for (Map.Entry<String, String> file : filePaths.entrySet()) {
            String filename = file.getKey();
            String filePath = file.getValue();
            System.out.println("Adding: " + basePath.getPath() + filePath + " => " + filename);
            File f = new File(basePath, filePath);
            if (f.exists()) {
                FileInputStream fi = new FileInputStream(f);
                origin = new BufferedInputStream(fi, BUFFER);
                ZipEntry entry = new ZipEntry(filename);
                entry.setCrc(FileUtils.checksumCRC32(new File(basePath, filePath)));
                out.putNextEntry(entry);
                int count;
                while ((count = origin.read(data, 0, BUFFER)) != -1) {
                    out.write(data, 0, count);
                }
                origin.close();
            }
        }
    } finally {
        if (origin != null)
            origin.close();
        if (out != null)
            out.close();
        if (dest != null)
            dest.close();
    }
}

From source file:edu.clemson.cs.nestbed.common.util.ZipUtils.java

public static void zipDirectory(File directory, File output) throws IOException {
    ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(output));
    zipDirectory(directory, directory.getName(), zos);
    zos.close();/*from w  w w  . jav a 2s  . c  om*/
}

From source file:com.ikanow.aleph2.analytics.spark.utils.SparkPyTechnologyUtils.java

/** Create a zip containing the Aleph2 driver
 * @param bucket/*ww  w .  jav  a 2  s.  c o m*/
 * @param job
 * @throws IOException
 */
public static String writeAleph2DriverZip(final String signature) throws IOException {
    final String tmp_dir = System.getProperty("java.io.tmpdir");
    final String filename = tmp_dir + "/aleph2_driver_py_" + signature + ".zip";

    final InputStream io_stream = Thread.currentThread().getContextClassLoader()
            .getResourceAsStream("aleph2_driver.py");
    final FileOutputStream fout = new FileOutputStream(filename);
    final ZipOutputStream zout = new ZipOutputStream(fout);
    final ZipEntry ze = new ZipEntry("aleph2_driver.py");
    zout.putNextEntry(ze);
    zout.write(IOUtils.toString(io_stream).getBytes());
    zout.closeEntry();
    zout.close();

    return filename;
}