Java Zip Folder zipFolder(String srcFolderPath, String outputZipPath)

Here you can find the source of zipFolder(String srcFolderPath, String outputZipPath)

Description

Zip folder

License

Open Source License

Parameter

Parameter Description
srcFolderPath source folder path
outputZipPath output zip file path

Exception

Parameter Description
FileNotFoundException an exception
IOException an exception

Declaration

public static void zipFolder(String srcFolderPath, String outputZipPath)
        throws FileNotFoundException, IOException 

Method Source Code

//package com.java2s;
/**//from  ww  w . j av a2s.  c  o  m
 * Copyright (c) Microsoft Corporation. All rights reserved.
 * Licensed under the MIT License. See License.txt in the project root for
 * license information.
 */

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

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {
    /**
     * Zip folder
     * @param srcFolderPath source folder path
     * @param outputZipPath output zip file path
     * @throws FileNotFoundException
     * @throws IOException
     */
    public static void zipFolder(String srcFolderPath, String outputZipPath)
            throws FileNotFoundException, IOException {
        ZipOutputStream zip = null;
        FileOutputStream fileWriter = null;

        // Create the output stream to zip file result
        fileWriter = new FileOutputStream(outputZipPath);
        zip = new ZipOutputStream(fileWriter);

        // Add the folder to the zip
        addFolderToZip("", srcFolderPath, zip);

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

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

        // Check the empty folder
        String[] files = folder.list();
        if (files == null || files.length == 0) {
            addFileToZip(path, srcFolder, zip, true);
        } else {
            // List the files in the folder
            for (String fileName : files) {
                if (path.equals("")) {
                    addFileToZip(folder.getName(), srcFolder + File.separator + fileName, zip, false);
                } else {
                    addFileToZip(path + File.separator + folder.getName(), srcFolder + File.separator + fileName,
                            zip, false);
                }
            }
        }
    }

    private static void addFileToZip(String path, String srcFile, ZipOutputStream zip, boolean isEmptyFolder)
            throws IOException {
        File folder = new File(srcFile);

        if (isEmptyFolder == true) {
            zip.putNextEntry(new ZipEntry(path + "/" + folder.getName() + "/"));
        } else {
            if (folder.isDirectory()) {
                addFolderToZip(path, srcFile, zip);
            } else {
                byte[] buf = new byte[1024];
                int len;
                try (FileInputStream in = new FileInputStream(srcFile)) {
                    zip.putNextEntry(new ZipEntry(path + "/" + folder.getName()));
                    while ((len = in.read(buf)) > 0) {
                        zip.write(buf, 0, len);
                    }
                }
            }
        }
    }
}

Related

  1. zipFolder(String sourceFolder, String target)
  2. zipFolder(String srcFolder, String destZipFile)
  3. zipFolder(String srcFolder, String destZipFile)
  4. zipFolder(String srcFolder, String destZipFile)
  5. zipFolder(String srcFolder, String destZipFile, boolean addBaseFolder)
  6. zipFolder(ZipOutputStream zipOut, File folder, File root)
  7. zipFolder2(ZipOutputStream zos, String folderName, String baseFolderName)