Java Zip Folder zipFolder(File srcFolder, String destZipFile)

Here you can find the source of zipFolder(File srcFolder, String destZipFile)

Description

zip Folder

License

Open Source License

Declaration

static public File zipFolder(File srcFolder, String destZipFile) throws Exception 

Method Source Code


//package com.java2s;
//it under the terms of the GNU Affero General Public License as published by

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;

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

import java.io.InputStream;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {
    static public File zipFolder(File srcFolder, String destZipFile) throws Exception {
        final File file = new File(srcFolder, destZipFile);
        final ZipOutputStream zip = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
        try {/*from   ww w  .j ava 2  s  .co m*/
            addFolderToZip("", srcFolder, zip, destZipFile);
        } finally {
            zip.close();
        }
        return file;
    }

    static private void addFolderToZip(String path, File srcFolder, ZipOutputStream zip, String destZipFile)
            throws Exception {
        for (String fileName : srcFolder.list()) {
            if (path.equals("")) {
                addFileToZip("/", new File(srcFolder, fileName), zip, destZipFile);
            } else {
                addFileToZip(srcFolder.getName(), new File(srcFolder, fileName), zip, destZipFile);
            }
        }
    }

    static private void addFileToZip(String path, File srcFile, ZipOutputStream zip, String destZipFile)
            throws Exception {
        if (srcFile.isDirectory()) {
            addFolderToZip(path, srcFile, zip, destZipFile);
        } else if (!srcFile.getName().equals(destZipFile)) {
            byte[] buf = new byte[1024];
            int len;
            final InputStream in = new BufferedInputStream(new FileInputStream(srcFile));
            try {
                if (path.equals("/")) {
                    zip.putNextEntry(new ZipEntry(srcFile.getName()));
                } else {
                    zip.putNextEntry(new ZipEntry(path + "/" + srcFile.getName()));
                }
                while ((len = in.read(buf)) > 0) {
                    zip.write(buf, 0, len);
                }
            } finally {
                in.close();
            }
        }
    }
}

Related

  1. zipDir(String origDir, File dirObj, ZipOutputStream zos)
  2. zipFolder(File root)
  3. zipFolder(File root)
  4. zipFolder(File source, File destination)
  5. zipFolder(File srcFolder, File destZipFile, boolean zipOnlySrcFolderContentsAndNotSrcFolder)
  6. zipFolder(final File directory, final String path, final ZipOutputStream out)
  7. zipFolder(final File srcFolder, final OutputStream out)
  8. zipFolder(final String srcFolder, final String destZipFile)
  9. zipFolder(String sourceDir, String destDir, String name)