Java Zip File List zipFiles(String srcFolder, String destZipFile)

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

Description

zip Files

License

Apache License

Declaration

@SuppressWarnings("finally")
    public static boolean zipFiles(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 {
    @SuppressWarnings("finally")
    public static boolean zipFiles(String srcFolder, String destZipFile) {
        boolean result = false;
        try {/*from www  .j  ava 2 s .com*/
            // System.out.println("Program Start zipping the given files");
            /*
             * send to the zip procedure
             */
            zipFolder(srcFolder, destZipFile);
            result = true;
            // System.out.println("Given files are successfully zipped");
        } catch (Exception e) {
            System.out.println("Some Errors happned during the zip process");
        } finally {
            return result;
        }
    }

    private 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
         */
        addFolderToZip("", srcFolder, zip);
        /*
         * 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
         */

        // ratul comments the lines below
        // 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);
                zip.putNextEntry(new ZipEntry(path + "/" + folder.getName()));
                while ((len = in.read(buf)) > 0) {
                    /*
                     * Write the Result
                     */
                    zip.write(buf, 0, len);
                }
                in.close();
            }
        }
    }
}

Related

  1. zipFiles(String files[], String fielPath)
  2. zipFiles(String filesPathToZip, String pathToSave)
  3. zipFiles(String output, String sDir, String sSearch)
  4. zipFiles(String output_dir, List files)
  5. zipFiles(String source, String target)
  6. zipFiles(ZipOutputStream out, String path, File... srcFiles)
  7. zipFilesTo(Vector fileVector, String baseDir, File destFile)