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

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

Description

add File To Zip

License

Apache License

Declaration

private static void addFileToZip(String path, String srcFile, ZipOutputStream zip) throws Exception 

Method Source Code


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

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

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

public class Main {
    private static void addFileToZip(String path, String srcFile, ZipOutputStream zip) throws Exception {

        File folder = new File(srcFile);
        if (folder.isDirectory()) {
            addFolderToZip(path, srcFile, zip);
        } else {//from   w  w  w.  j a  v  a  2 s.  c  o m
            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) {
                zip.write(buf, 0, len);
            }
        }
    }

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

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

Related

  1. addFileToZip(final String path, final String srcFile, final ZipOutputStream zip, boolean flag)
  2. addFileToZip(final String pathInsideZip, final File fileToZip, final ZipOutputStream outZip)
  3. addFileToZip(int skipprefix, File file, ZipOutputStream zipper)
  4. addFileToZip(String path, File srcFile, ZipOutputStream zip, boolean flag)
  5. addFileToZip(String path, File srcFile, ZipOutputStream zip, String destZipFile)
  6. addFileToZip(String path, String srcFile, ZipOutputStream zip)
  7. addFileToZip(ZipOutputStream out, InputStream in, String entry)
  8. addFileToZip(ZipOutputStream zipOutputStream, File file, String basePath)
  9. addFileToZip(ZipOutputStream zipOutputStream, String path, byte[] bytes)