Java Zip File List zip(File zipFile, List files)

Here you can find the source of zip(File zipFile, List files)

Description

zip

License

Apache License

Declaration

public static File zip(File zipFile, List<File> files) throws FileNotFoundException 

Method Source Code

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

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import java.util.List;
import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {
    public static File zip(File zipFile, List<File> files) throws FileNotFoundException {
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile));
        zip(out, files);/*from w  w w.ja v a  2s.c o m*/
        return zipFile;
    }

    public static void zip(ZipOutputStream out, List<File> srcFiles) {
        try {
            for (File srcFile : srcFiles) {
                zip(out, srcFile);
            }
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void zip(ZipOutputStream out, File srcFile) throws IOException {
        out.putNextEntry(new ZipEntry(srcFile.getName()));
        FileInputStream fis = new FileInputStream(srcFile);
        byte[] buffer = new byte[1024];
        int length;
        while ((length = fis.read(buffer, 0, buffer.length)) != -1) {
            out.write(buffer, 0, length);
        }
        out.closeEntry();
        fis.close();
    }
}

Related

  1. zip(Collection files, File zipFile)
  2. zip(File zipFile, File parentDir, File[] sources, char pathSeparator)
  3. zip(File zipFile, File[] files)
  4. zip(File zipFile, File[] files)
  5. zip(Iterable files, String baseFolderName, String toZipFile)
  6. zip(List runtimeLibFiles, File saturnContainerDir, File zipFile)
  7. zip(List fileNames, List fileContents)
  8. zip(List fileNames, String outFileName)