Java Zip Folder addFolderToZip(String path, File srcFolder, ZipOutputStream zip, String destZipFile)

Here you can find the source of addFolderToZip(String path, File srcFolder, ZipOutputStream zip, String destZipFile)

Description

add Folder To Zip

License

Open Source License

Declaration

static private void addFolderToZip(String path, File srcFolder, ZipOutputStream zip, 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.File;
import java.io.FileInputStream;

import java.io.InputStream;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {
    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);
            }//from w w  w  .  ja va2  s.  c  o m
        }
    }

    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. addFolderToZip(File folder, String parentFolderName, ZipOutputStream zip)
  2. addFolderToZip(String folderPath, ZipOutputStream out)
  3. addFolderToZip(String path, File srcFolder, ZipOutputStream zip)
  4. addFolderToZip(String path, String srcFolder, ZipOutputStream zip)
  5. addFolderToZip(String path, String srcFolder, ZipOutputStream zip)
  6. addFolderToZip(String path, String srcFolder, ZipOutputStream zip)
  7. addFolderToZip(String path, String srcFolder, ZipOutputStream zip, boolean addFolder)