Java Zip Folder zipFolder(String srcFolder, String destZipFile)

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

Description

zip Folder

License

LGPL

Declaration

public static void zipFolder(String srcFolder, String destZipFile) throws Exception 

Method Source Code

//package com.java2s;
/**/*from ww w  .jav a 2  s.co  m*/
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3, 29 June 2007;
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.gnu.org/licenses/lgpl-3.0.txt
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.*;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {
    public static void zipFolder(String srcFolder, String destZipFile) throws Exception {
        ZipOutputStream zip = null;
        FileOutputStream fileWriter = null;
        /*
        * create the output stream to zip file result
        */
        fileWriter = new FileOutputStream(destZipFile);
        zip = new ZipOutputStream(fileWriter);
        /*
        * add the folder to the zip
        */

        File f = new File(srcFolder);
        File[] subFiles = f.listFiles();
        for (int i = 0; i < subFiles.length; i++) {
            File subFile = subFiles[i];
            if (subFile.isDirectory()) {
                addFolderToZip("", subFile.getAbsolutePath(), zip);
            } else {
                addFileToZip("", subFile.getAbsolutePath(), zip, false);
            }

        }

        /*
        * close the zip objects
        */
        zip.flush();
        zip.close();
    }

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

        /*
         * check the empty folder
         */
        if (folder.list().length == 0) {
            System.out.println(folder.getName());
            addFileToZip(path, srcFolder, zip, true);
        } else {
            /*
             * list the files in the folder
             */
            for (String fileName : folder.list()) {
                if (path.equals("")) {
                    addFileToZip(folder.getName(), srcFolder + "/" + fileName, zip, false);
                } else {
                    addFileToZip(path + "/" + folder.getName(), srcFolder + "/" + fileName, zip, false);
                }
            }
        }
    }

    private static void addFileToZip(String path, String srcFile, ZipOutputStream zip, boolean flag)
            throws Exception {
        /*
        * create the file object for inputs
        */
        File folder = new File(srcFile);

        /*
         * if the folder is empty add empty folder to the Zip file
         */
        if (flag == true) {
            zip.putNextEntry(new ZipEntry(path + "/" + folder.getName() + "/"));
        } else { /*
                 * if the current name is directory, recursively traverse it to get the files
                 */
            if (folder.isDirectory()) {
                /*
                 * if folder is not empty
                 */
                addFolderToZip(path, srcFile, zip);
            } else {
                /*
                 * write the file to the output
                */
                byte[] buf = new byte[1024];
                int len;
                FileInputStream in = new FileInputStream(srcFile);

                if (path.equals("")) {
                    zip.putNextEntry(new ZipEntry(folder.getName()));
                } else {
                    zip.putNextEntry(new ZipEntry(path + "/" + folder.getName()));
                }

                while ((len = in.read(buf)) > 0) {
                    /*
                     * Write the Result
                    */
                    zip.write(buf, 0, len);
                }
            }
        }
    }
}

Related

  1. zipFolder(final File directory, final String path, final ZipOutputStream out)
  2. zipFolder(final File srcFolder, final OutputStream out)
  3. zipFolder(final String srcFolder, final String destZipFile)
  4. zipFolder(String sourceDir, String destDir, String name)
  5. zipFolder(String sourceFolder, String target)
  6. zipFolder(String srcFolder, String destZipFile)
  7. zipFolder(String srcFolder, String destZipFile)
  8. zipFolder(String srcFolder, String destZipFile, boolean addBaseFolder)
  9. zipFolder(String srcFolderPath, String outputZipPath)