Java Zip File List zipFiles(File output, File root, List fileList)

Here you can find the source of zipFiles(File output, File root, List fileList)

Description

zip Files

License

Apache License

Declaration

private static void zipFiles(File output, File root, List<File> fileList) 

Method Source Code


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

import java.io.File;
import java.io.FileInputStream;
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 {
    private static void zipFiles(File output, File root, List<File> fileList) {
        try {//from  w  w w  .  j av a2s .  c om
            FileOutputStream fos = new FileOutputStream(output);
            ZipOutputStream zos = new ZipOutputStream(fos);

            for (File file : fileList) {
                if (file.isFile()) {
                    addFileToZip(root, file, zos);
                }
            }

            zos.close();
            fos.close();
        } catch (IOException e) {
            throw new RuntimeException("Unable to write zip file.", e);
        }
    }

    private static void addFileToZip(File root, File file, ZipOutputStream zos) throws IOException {
        FileInputStream fis = new FileInputStream(file);

        String filePath = file.getCanonicalPath();
        String zipFilePath = filePath.substring(root.getCanonicalPath().length() + 1, filePath.length());
        ZipEntry zipEntry = new ZipEntry(zipFilePath);
        zos.putNextEntry(zipEntry);

        byte[] bytes = new byte[1024];
        int length;
        while ((length = fis.read(bytes)) >= 0) {
            zos.write(bytes, 0, length);
        }

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

Related

  1. zip(String[] srcFiles, String dstFile, String comment)
  2. zipFileList(List files, File destinationDir)
  3. zipFiles(ArrayList files, String destZipFile)
  4. zipFiles(Collection resFileList, File zipFile)
  5. zipFiles(File file, JarOutputStream jos, String pathName)
  6. zipFiles(File rootDir, File currDir, ZipOutputStream zos)
  7. zipFiles(File[] dirList, ZipOutputStream zos)
  8. zipFiles(File[] listFiles2Zip, File output)
  9. zipFiles(final File outputFile, final File[] files)