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

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

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 {
    static private void addFolderToZip(String path, String srcFolder, ZipOutputStream zip) {
        File folder = new File(srcFolder);
        String fileListe[] = folder.list();
        try {// www  .  j a  va 2s. c om
            int i = 0;
            while (i <= fileListe.length) {
                String newPath = folder.getName();
                if (!path.equalsIgnoreCase(""))
                    newPath = path + "/" + newPath; //This path-separator is hard coded purposely
                addToZip(newPath, srcFolder + File.separator + fileListe[i], zip);
                i++;
            }
        } catch (Exception ex) {
        }
    }

    static private void addToZip(String path, String srcFile, ZipOutputStream zip) {

        File folder = new File(srcFile);
        if (folder.isDirectory()) {
            addFolderToZip(path, srcFile, zip);
        } else {
            // Transfer bytes from in to out
            byte[] buf = new byte[1024];
            int len;
            try {
                FileInputStream in = new FileInputStream(srcFile);
                String nextEntryPath = path + "/" + folder.getName(); //This path-separator is hard coded purposely
                if (path != null && path.trim().equalsIgnoreCase("")) {
                    nextEntryPath = folder.getName();
                }
                zip.putNextEntry(new ZipEntry(nextEntryPath));
                while ((len = in.read(buf)) > 0) {
                    zip.write(buf, 0, len);
                }
                in.close();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }
}

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)