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

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

Description

add File To Zip

License

LGPL

Declaration

private static void addFileToZip(String path, File srcFile, ZipOutputStream zip, boolean flag)
            throws IOException 

Method Source Code


//package com.java2s;
//License from project: LGPL 

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 {
    private static void addFileToZip(String path, File srcFile, ZipOutputStream zip, boolean flag)
            throws IOException {
        if (flag == true) {
            zip.putNextEntry(new ZipEntry(path + "/" + srcFile.getName() + "/"));
        } else {/*ww  w .j a  v  a  2s .c  om*/
            if (srcFile.isDirectory()) {
                addFolderToZip(path, srcFile, zip);
            } else {
                int len;
                byte[] buf = new byte[1024];
                FileInputStream in = new FileInputStream(srcFile);
                String pathPrefix = (!path.equals("")) ? path + "/" : "";
                zip.putNextEntry(new ZipEntry(pathPrefix + srcFile.getName()));

                while ((len = in.read(buf)) > 0) {
                    zip.write(buf, 0, len);
                }

                in.close();
            }
        }
    }

    private static void addFolderToZip(String path, File srcFolder, ZipOutputStream zip) throws IOException {
        if (srcFolder.list().length == 0) {
            addFileToZip(path, srcFolder, zip, true);
        } else {
            for (File file : srcFolder.listFiles()) {
                if (path.equals("")) {
                    addFileToZip(srcFolder.getName(), file, zip, false);
                } else {
                    addFileToZip(path + "/" + srcFolder.getName(), file, zip, false);
                }
            }
        }
    }
}

Related

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