Java Zip File List zip(List runtimeLibFiles, File saturnContainerDir, File zipFile)

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

Description

zip

License

Apache License

Declaration

public static void zip(List<File> runtimeLibFiles, File saturnContainerDir, File zipFile) throws IOException 

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 final String fileSeparator = System.getProperty("file.separator");

    public static void zip(List<File> runtimeLibFiles, File saturnContainerDir, File zipFile) throws IOException {
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile));
        /*      for(File file : saturnContainerDir.listFiles()) {
             zip(file, "saturn", zos);//from  w w w.  j  a v  a 2 s  .co  m
          }*/

        for (File file : runtimeLibFiles) {
            zip(file, "app" + fileSeparator + "lib", zos);
        }
        zos.close();
    }

    private static void zip(File file, String parent, ZipOutputStream zos) throws IOException {
        if (file == null || !file.exists())
            return;
        if (file.isFile()) {
            String entryName = parent == null ? file.getName() : parent + fileSeparator + file.getName();
            zos.putNextEntry(new ZipEntry(entryName));
            FileInputStream fis = new FileInputStream(file);
            int len = -1;
            byte[] bs = new byte[2048];
            while ((len = fis.read(bs, 0, 2048)) != -1) {
                zos.write(bs, 0, len);
            }
            fis.close();
        } else if (file.isDirectory()) {
            String entryName = parent == null ? file.getName() : parent + fileSeparator + file.getName();
            zos.putNextEntry(new ZipEntry(entryName + "/"));
            File[] listFiles = file.listFiles();
            if (listFiles != null) {
                for (File tmp : file.listFiles()) {
                    zip(tmp, entryName, zos);
                }
            }
        }
    }
}

Related

  1. zip(File zipFile, File parentDir, File[] sources, char pathSeparator)
  2. zip(File zipFile, File[] files)
  3. zip(File zipFile, File[] files)
  4. zip(File zipFile, List files)
  5. zip(Iterable files, String baseFolderName, String toZipFile)
  6. zip(List fileNames, List fileContents)
  7. zip(List fileNames, String outFileName)
  8. zip(List srcFiles, OutputStream os)
  9. zip(String[] filename, String outname)