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

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

Description

add File To Zip

License

Open Source License

Declaration

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

Method Source Code


//package com.java2s;
//it under the terms of the GNU Affero General Public License as published by

import java.io.BufferedInputStream;

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

import java.io.InputStream;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {
    static private void addFileToZip(String path, File srcFile, ZipOutputStream zip, String destZipFile)
            throws Exception {
        if (srcFile.isDirectory()) {
            addFolderToZip(path, srcFile, zip, destZipFile);
        } else if (!srcFile.getName().equals(destZipFile)) {
            byte[] buf = new byte[1024];
            int len;
            final InputStream in = new BufferedInputStream(new FileInputStream(srcFile));
            try {
                if (path.equals("/")) {
                    zip.putNextEntry(new ZipEntry(srcFile.getName()));
                } else {
                    zip.putNextEntry(new ZipEntry(path + "/" + srcFile.getName()));
                }/*w w w .  ja  va 2  s .co m*/
                while ((len = in.read(buf)) > 0) {
                    zip.write(buf, 0, len);
                }
            } finally {
                in.close();
            }
        }
    }

    static private void addFolderToZip(String path, File srcFolder, ZipOutputStream zip, String destZipFile)
            throws Exception {
        for (String fileName : srcFolder.list()) {
            if (path.equals("")) {
                addFileToZip("/", new File(srcFolder, fileName), zip, destZipFile);
            } else {
                addFileToZip(srcFolder.getName(), new File(srcFolder, fileName), zip, destZipFile);
            }
        }
    }
}

Related

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