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

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

Description

add Folder To Zip

License

Apache License

Declaration

private static void addFolderToZip(String path, String srcFolder, ZipOutputStream zip) throws Exception 

Method Source Code


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

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

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

public class Main {
    private static void addFolderToZip(String path, String srcFolder, ZipOutputStream zip) throws Exception {
        File folder = new File(srcFolder);

        for (String fileName : folder.list()) {
            if (path.equals("")) {
                addFileToZip(folder.getName(), srcFolder + "/" + fileName, zip);
            } else {
                addFileToZip(path + "/" + folder.getName(), srcFolder + "/" + fileName, zip);
            }// w  ww .  ja  va 2s. co  m
        }
    }

    private static void addFileToZip(String path, String srcFile, ZipOutputStream zip) throws Exception {

        File folder = new File(srcFile);
        if (folder.isDirectory()) {
            addFolderToZip(path, srcFile, zip);
        } else {
            byte[] buf = new byte[1024];
            int len;
            FileInputStream in = new FileInputStream(srcFile);
            zip.putNextEntry(new ZipEntry(path + "/" + folder.getName()));
            while ((len = in.read(buf)) > 0) {
                zip.write(buf, 0, len);
            }
        }
    }
}

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, File srcFolder, ZipOutputStream zip, String destZipFile)
  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)
  8. addFolderToZip(String path, String srcFolder, ZipOutputStream zip, boolean includeFullPath)
  9. addFolderToZip(String pathInsideZip, final File folderToZip, final ZipOutputStream outZip)