Java Zip Folder zipFolder(File srcFolder, File destZipFile, boolean zipOnlySrcFolderContentsAndNotSrcFolder)

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

Description

zip Folder

License

LGPL

Declaration

public static void zipFolder(File srcFolder, File destZipFile, boolean zipOnlySrcFolderContentsAndNotSrcFolder)
            throws IOException 

Method Source Code


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

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

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {
    public static void zipFolder(File srcFolder, File destZipFile, boolean zipOnlySrcFolderContentsAndNotSrcFolder)
            throws IOException {
        destZipFile.getParentFile().mkdirs();

        FileOutputStream fileWriter = new FileOutputStream(destZipFile);
        ZipOutputStream zip = new ZipOutputStream(fileWriter);

        if (zipOnlySrcFolderContentsAndNotSrcFolder) {
            for (File file : srcFolder.listFiles()) {
                addFileToZip("", file, zip, false);
            }//from w ww. j  a v  a  2  s  .  c o m
        } else {
            addFolderToZip("", srcFolder, zip);
        }

        zip.flush();
        zip.close();
    }

    private static void addFileToZip(String path, File srcFile, ZipOutputStream zip, boolean flag)
            throws IOException {
        if (flag == true) {
            zip.putNextEntry(new ZipEntry(path + "/" + srcFile.getName() + "/"));
        } else {
            if (srcFile.isDirectory()) {
                addFolderToZip(path, srcFile, zip);
            } else {
                int len;
                byte[] buf = new byte[1024];
                FileInputStream in = new FileInputStream(srcFile);
                String pathPrefix = (!path.equals("")) ? path + "/" : "";
                zip.putNextEntry(new ZipEntry(pathPrefix + srcFile.getName()));

                while ((len = in.read(buf)) > 0) {
                    zip.write(buf, 0, len);
                }

                in.close();
            }
        }
    }

    private static void addFolderToZip(String path, File srcFolder, ZipOutputStream zip) throws IOException {
        if (srcFolder.list().length == 0) {
            addFileToZip(path, srcFolder, zip, true);
        } else {
            for (File file : srcFolder.listFiles()) {
                if (path.equals("")) {
                    addFileToZip(srcFolder.getName(), file, zip, false);
                } else {
                    addFileToZip(path + "/" + srcFolder.getName(), file, zip, false);
                }
            }
        }
    }
}

Related

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