Java Zip Directory zipDirectory(File directory, File zipFile)

Here you can find the source of zipDirectory(File directory, File zipFile)

Description

zip Directory

License

LGPL

Declaration

public static void zipDirectory(File directory, File zipFile) throws IOException 

Method Source Code


//package com.java2s;
//License from project: LGPL 

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import java.io.IOException;

import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Main {
    public static void zipDirectory(File directory, File zipFile) throws IOException {
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile));
        addDir(directory, directory, out);
        out.close();//w  w w.j a va  2s . c  om
    }

    private static void addDir(File root, File directory, ZipOutputStream out) throws IOException {
        File[] files = directory.listFiles();
        byte[] tmpBuf = new byte[1024];

        for (int i = 0; i < files.length; i++) {
            if (files[i].isDirectory()) {
                addDir(root, files[i], out);
                continue;
            }
            FileInputStream in = new FileInputStream(files[i].getAbsolutePath());
            out.putNextEntry(new ZipEntry(files[i].getAbsolutePath().substring(root.getAbsolutePath().length())));
            int len;
            while ((len = in.read(tmpBuf)) > 0) {
                out.write(tmpBuf, 0, len);
            }
            out.closeEntry();
            in.close();
        }
    }
}

Related

  1. zipDirectory(File dir, File zipFile)
  2. zipDirectory(File dir, String base, ZipOutputStream zout)
  3. zipDirectory(File dir, String zipDirName)
  4. zipDirectory(File directory, File zip)
  5. zipDirectory(File directory, File zip)
  6. zipDirectory(File directory, File zipFile)
  7. zipDirectory(File directory, File zipFile, Pattern exclusion)
  8. zipDirectory(File directory, ZipOutputStream zipout, final FilenameFilter filter)
  9. zipDirectory(File dirToZip, File outputDir, String fileName)