Java Zip Folder zipDir(File dir, ZipOutputStream zos, String prefix)

Here you can find the source of zipDir(File dir, ZipOutputStream zos, String prefix)

Description

Recursively zips all files in "dir" and its subdirectories into the given ZipOutputStream "zos" using the given path prefix for their names

License

Open Source License

Declaration

private static void zipDir(File dir, ZipOutputStream zos, String prefix) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {
    /**/*w w  w.j a va  2  s  . co m*/
     * Recursively zips all files in "dir" and its subdirectories into the given
     * ZipOutputStream "zos" using the given path prefix for their names
     */
    private static void zipDir(File dir, ZipOutputStream zos, String prefix) {
        File[] entries = dir.listFiles();
        for (int i = 0; i < entries.length; i++) {
            if (entries[i].isDirectory()) {
                // generate directory entry
                ZipEntry zi = new ZipEntry(prefix + "/" + entries[i].getName() + "/");
                try {
                    zos.putNextEntry(zi);
                    zos.closeEntry();
                } catch (IOException ioex) {
                    ioex.printStackTrace();
                }
                zipDir(entries[i], zos, prefix + "/" + entries[i].getName());
            } else {
                FileInputStream fis = null;
                try {
                    fis = new FileInputStream(entries[i]);
                    ZipEntry zi = new ZipEntry(prefix + "/" + entries[i].getName());
                    zos.putNextEntry(zi);
                    copystream(fis, zos);
                    zos.closeEntry();
                } catch (FileNotFoundException ex) {
                    ex.printStackTrace();
                } catch (IOException ioex) {
                    ioex.printStackTrace();
                } finally {
                    try {
                        if (fis != null)
                            fis.close();
                    } catch (Exception e) {
                    }
                }
            }
        }
    }

    /**
     * Copies the input stream to the output stream using a 1 kB buffer
     * @throws IOException
     */
    private static void copystream(InputStream in, OutputStream out) throws IOException {
        byte[] buffer = new byte[1024];
        int len;

        while ((len = in.read(buffer)) >= 0)
            out.write(buffer, 0, len);
    }
}

Related

  1. zip(File rootDir, String zipPath)
  2. zip(File rootDir, String zipPath)
  3. zipDir(File dir, String classPackage, String zipFile)
  4. zipDir(File dir, String relativePath, ZipOutputStream zos)
  5. zipDir(File dir, String relativePath, ZipOutputStream zos, boolean start)
  6. zipDir(File directory, ZipOutputStream zos, String path, Set exclusions)
  7. zipDir(File sourceDir, File destFile)
  8. zipDir(File zipDir, ZipOutputStream zos, boolean recurse)
  9. zipDir(File zipDir, ZipOutputStream zos, String archiveSourceDir)