Java Zip Folder addFolderToZip(String path, String srcFolder, ZipOutputStream zip)

Here you can find the source of addFolderToZip(String path, String srcFolder, ZipOutputStream zip)

Description

Adds the folder to zip.

License

Open Source License

Parameter

Parameter Description
path the path
srcFolder the src folder
zip the zip

Declaration

private static void addFolderToZip(String path, String srcFolder, ZipOutputStream zip) 

Method Source Code

//package com.java2s;
/*/*from   w  w  w .j av a 2 s.c om*/
 * FileUtil.java
 * Copyright (c) 2014, CODEROAD, All Rights Reserved.
 *
 * This software is the confidential and proprietary information of CODEROAD
 * ("Confidential Information"). You shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement you entered into with
 * CODEROAD.
 */

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

import java.io.IOException;

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

public class Main {
    /** The Constant SLASH. */
    private static final String SLASH = "/";

    /**
     * Adds the folder to zip.
     * 
     * @param path the path
     * @param srcFolder the src folder
     * @param zip the zip
     */
    private static void addFolderToZip(String path, String srcFolder, ZipOutputStream zip) {

        final File folder = new File(srcFolder);

        if (folder.list().length == 0) {
            addFileToZip(path, srcFolder, zip, true);
        } else {
            for (String fileName : folder.list()) {
                if (path.equals("")) {
                    addFileToZip(folder.getName(), srcFolder + "/" + fileName, zip, false);
                } else {
                    addFileToZip(path + "/" + folder.getName(), srcFolder + "/" + fileName, zip, false);
                }
            }
        }
    }

    /**
     * Adds the file to zip.
     * 
     * @param path the path
     * @param srcFile the src file
     * @param zip the zip
     * @param flag the flag
     */
    private static void addFileToZip(final String path, final String srcFile, final ZipOutputStream zip,
            boolean flag) {

        try {
            final File folder = new File(srcFile);

            if (folder.isDirectory()) {
                addFolderToZip(path, srcFile, zip);
            } else {
                final byte[] buf = new byte[1024];
                int len;
                final FileInputStream inputStream = new FileInputStream(srcFile);
                zip.putNextEntry(
                        new ZipEntry(new StringBuilder(path).append(SLASH).append(folder.getName()).toString()));

                while (inputStream.read(buf) > 0) {
                    len = inputStream.read(buf);
                    zip.write(buf, 0, len);
                }
                inputStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Related

  1. addFolderToZip(String folderPath, ZipOutputStream out)
  2. addFolderToZip(String path, File srcFolder, ZipOutputStream zip)
  3. addFolderToZip(String path, File srcFolder, ZipOutputStream zip, String destZipFile)
  4. addFolderToZip(String path, String srcFolder, ZipOutputStream zip)
  5. addFolderToZip(String path, String srcFolder, ZipOutputStream zip)
  6. addFolderToZip(String path, String srcFolder, ZipOutputStream zip, boolean addFolder)
  7. addFolderToZip(String path, String srcFolder, ZipOutputStream zip, boolean includeFullPath)
  8. addFolderToZip(String pathInsideZip, final File folderToZip, final ZipOutputStream outZip)
  9. doZip(File fileIn, String fileOut)