Java Zip Folder zipFolder(String srcFolder, String destZipFile)

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

Description

zip Folder

License

Apache License

Declaration

static public void zipFolder(String srcFolder, String destZipFile) 

Method Source Code


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

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

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

public class Main {
    static public void zipFolder(String srcFolder, String destZipFile) {
        ZipOutputStream zip = null;
        FileOutputStream fileWriter = null;
        try {/*from   w w w .ja  v  a2s.c o m*/
            fileWriter = new FileOutputStream(destZipFile);
            zip = new ZipOutputStream(fileWriter);
        } catch (Exception ex) {
            ex.printStackTrace();
            return;
        }
        addFolderContentsToZip(srcFolder, zip);
        try {
            zip.flush();
            zip.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    static private void addFolderContentsToZip(String srcFolder, ZipOutputStream zip) {
        File folder = new File(srcFolder);
        String fileListe[] = folder.list();
        try {
            int i = 0;
            while (i <= fileListe.length) {
                addToZip("", 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();
            }
        }
    }

    static private void addFolderToZip(String path, String srcFolder, ZipOutputStream zip) {
        File folder = new File(srcFolder);
        String fileListe[] = folder.list();
        try {
            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) {
        }
    }
}

Related

  1. zipFolder(final String srcFolder, final String destZipFile)
  2. zipFolder(String sourceDir, String destDir, String name)
  3. zipFolder(String sourceFolder, String target)
  4. zipFolder(String srcFolder, String destZipFile)
  5. zipFolder(String srcFolder, String destZipFile)
  6. zipFolder(String srcFolder, String destZipFile, boolean addBaseFolder)
  7. zipFolder(String srcFolderPath, String outputZipPath)
  8. zipFolder(ZipOutputStream zipOut, File folder, File root)
  9. zipFolder2(ZipOutputStream zos, String folderName, String baseFolderName)