Java Zip Files addFileToZip(final String path, final String srcFile, final ZipOutputStream zip, boolean flag)

Here you can find the source of addFileToZip(final String path, final String srcFile, final ZipOutputStream zip, boolean flag)

Description

Adds the file to zip.

License

Open Source License

Parameter

Parameter Description
path the path
srcFile the src file
zip the zip
flag the flag

Declaration

private static void addFileToZip(final String path, final String srcFile, final ZipOutputStream zip,
        boolean flag) 

Method Source Code

//package com.java2s;
/*//from   www . ja  v a2  s  .  com
 * 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 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();
        }
    }

    /**
     * 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);
                }
            }
        }
    }
}

Related

  1. addFileToZip(File file, String parentFolderName, ZipOutputStream zip)
  2. addFileToZip(File file, ZipOutputStream zos)
  3. addFileToZip(File in, File parent, ZipOutputStream out)
  4. addFileToZip(File root, File file, ZipOutputStream zos)
  5. addFileToZip(final File file, final String zipName, final ZipOutputStream zipout)
  6. addFileToZip(final String pathInsideZip, final File fileToZip, final ZipOutputStream outZip)
  7. addFileToZip(int skipprefix, File file, ZipOutputStream zipper)
  8. addFileToZip(String path, File srcFile, ZipOutputStream zip, boolean flag)
  9. addFileToZip(String path, File srcFile, ZipOutputStream zip, String destZipFile)