Java Zip File zip(String filesDirToBeZipped, String destFileName, String manifest)

Here you can find the source of zip(String filesDirToBeZipped, String destFileName, String manifest)

Description

zip

License

BSD License

Declaration

public static void zip(String filesDirToBeZipped, String destFileName, String manifest) 

Method Source Code


//package com.java2s;
/*L//w  w  w . j  av a2s  .c  o  m
 *  Copyright SAIC, Ellumen and RSNA (CTP)
 *
 *
 *  Distributed under the OSI-approved BSD 3-Clause License.
 *  See http://ncip.github.com/national-biomedical-image-archive/LICENSE.txt for details.
 */

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Vector;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Main {
    static final int BUFFER = 2048;

    public static void zip(String filesDirToBeZipped, String destFileName, String manifest) {

        try {
            BufferedInputStream origin = null;
            FileOutputStream dest = new FileOutputStream(destFileName);
            ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));
            byte data[] = new byte[BUFFER];
            // get a list of files from current directory
            File f = new File(filesDirToBeZipped);
            String files[] = f.list();

            Vector v = new Vector();
            for (int i = 0; i < files.length; i++) {
                v.add(filesDirToBeZipped + files[i]);
            }
            if (manifest != null) {
                v.add(manifest);
            }

            for (int i = 0; i < v.size(); i++) {
                String filePath = (String) (v.get(i));
                FileInputStream fi = new FileInputStream(filePath);
                origin = new BufferedInputStream(fi, BUFFER);
                ZipEntry entry = new ZipEntry(filePath);
                out.putNextEntry(entry);
                int count;
                while ((count = origin.read(data, 0, BUFFER)) != -1) {
                    out.write(data, 0, count);
                }
                origin.close();
            }
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Related

  1. zip(InputStream is)
  2. zip(InputStream is, String dataFileName, File zipFile)
  3. zip(OutputStream outputStream, File targetFile)
  4. zip(String data, String fileName)
  5. zip(String dir, String destFile)
  6. zip(String inputStr)
  7. zip(String outputFileName, String inputFileName)
  8. Zip(String path, File file)
  9. zip(String path, String zipFilePath)