Java Zip File List zip(String[] srcFiles, String dstFile, String comment)

Here you can find the source of zip(String[] srcFiles, String dstFile, String comment)

Description

zip

License

Open Source License

Declaration


public static void zip(String[] srcFiles, String dstFile, String comment) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.*;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {

    public static void zip(String[] srcFiles, String dstFile, String comment) throws IOException {
        if (srcFiles.length == 0) {
            return;
        }// www.j av a  2s  .c o  m
        File fileDst = new File(dstFile);
        File parentDir = fileDst.getParentFile();
        if (!parentDir.exists()) {
            parentDir.mkdirs();
        }
        if (fileDst.exists()) {
            fileDst.delete();
        }
        fileDst.createNewFile();

        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(fileDst));
        zos.setMethod(ZipOutputStream.DEFLATED);

        if (comment != null) {
            zos.setComment(comment);
        }

        DataOutputStream dos = new DataOutputStream(zos);

        for (int i = 0; i < srcFiles.length; i++) {
            String entryPath = srcFiles[i];
            zos.putNextEntry(new ZipEntry(entryPath));

            File fileEntry = new File(entryPath);
            FileInputStream fis = new FileInputStream(fileEntry);

            byte[] buff = new byte[8192];
            int len = 0;

            while (true) {
                len = fis.read(buff);
                if (len == -1 || len == 0) {
                    break;
                }

                dos.write(buff, 0, len);
            }

            zos.closeEntry();
            fis.close();
        }
        dos.close();
        zos.close();
    }

    public static void zip(String[] srcFiles, String dstFile) throws IOException {
        zip(srcFiles, dstFile, null);
    }
}

Related

  1. zip(List fileNames, String outFileName)
  2. zip(List srcFiles, OutputStream os)
  3. zip(String[] filename, String outname)
  4. zip(String[] sFl, String sZip)
  5. zip(String[] sourceFiles, String zipFile, String directory)
  6. zipFileList(List files, File destinationDir)
  7. zipFiles(ArrayList files, String destZipFile)
  8. zipFiles(Collection resFileList, File zipFile)
  9. zipFiles(File file, JarOutputStream jos, String pathName)