Java Zip File List zip(String[] filename, String outname)

Here you can find the source of zip(String[] filename, String outname)

Description

zip

License

Apache License

Declaration

public static boolean zip(String[] filename, String outname) 

Method Source Code


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

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

public class Main {
    static final int BUFFER = 2048;

    public static boolean zip(String[] filename, String outname) {

        boolean test = true;
        try {// w ww  . ja  v a  2s. co  m
            BufferedInputStream origin = null;
            FileOutputStream dest = new FileOutputStream(outname);
            ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));
            byte data[] = new byte[BUFFER];

            for (int i = 0; i < filename.length; i++) {
                File file = new File(filename[i]);
                FileInputStream fi = new FileInputStream(file);
                origin = new BufferedInputStream(fi, BUFFER);
                ZipEntry entry = new ZipEntry(file.getName());
                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) {
            test = false;
            e.printStackTrace();
        }
        return test;
    }
}

Related

  1. zip(Iterable files, String baseFolderName, String toZipFile)
  2. zip(List runtimeLibFiles, File saturnContainerDir, File zipFile)
  3. zip(List fileNames, List fileContents)
  4. zip(List fileNames, String outFileName)
  5. zip(List srcFiles, OutputStream os)
  6. zip(String[] sFl, String sZip)
  7. zip(String[] sourceFiles, String zipFile, String directory)
  8. zip(String[] srcFiles, String dstFile, String comment)
  9. zipFileList(List files, File destinationDir)