Java Zip File zip(String zipFileName, String inputFile)

Here you can find the source of zip(String zipFileName, String inputFile)

Description

zip

License

Apache License

Declaration

public static void zip(String zipFileName, String inputFile) throws Exception 

Method Source Code


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

import java.util.zip.*;
import java.io.*;

public class Main {
    public static void zip(String zipFileName, String inputFile) throws Exception {
        zip(zipFileName, new File(inputFile));
    }/*  ww w  . ja va2 s. com*/

    public static void zip(String zipFileName, File inputFile) throws Exception {
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
        zip(out, inputFile, "");
        System.out.println("zip done");
        out.finish();
        out.close();
    }

    public static void zip(ZipOutputStream out, File f, String base) throws Exception {
        System.out.println("Zipping " + f.getName());
        if (f.isDirectory()) {
            File[] fl = f.listFiles();
            out.putNextEntry(new ZipEntry(base + "/"));
            base = base.length() == 0 ? "" : base + "/";
            for (int i = 0; i < fl.length; i++) {
                zip(out, fl[i], base + fl[i].getName());
            }
        } else {
            out.putNextEntry(new ZipEntry(base));
            FileInputStream in = new FileInputStream(f);
            int b;
            while ((b = in.read()) != -1)
                out.write(b);
            in.close();
        }

    }
}

Related

  1. zip(String payload)
  2. zip(String src, String des)
  3. zip(String src, String dest, PrintStream stream)
  4. zip(String srcPath)
  5. zip(String zipFileName, Map entries)
  6. zip(String zipFileName, String[] zipEntries)
  7. zip(String zipName, String[] fileNames, boolean path)
  8. zip(String zipPath, Map input)
  9. zipFile(File aFileToZip)