Java Zip File zip(File file, File zip)

Here you can find the source of zip(File file, File zip)

Description

zip

License

Apache License

Declaration

public static void zip(File file, File zip) 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.Deflater;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Main {

    public static void zip(File file, File zip) throws IOException {
        ZipOutputStream zos = null;
        try {/*from   w  ww . j av a  2  s  .c o  m*/
            String name = file.getName();
            zos = new ZipOutputStream(new FileOutputStream(zip));

            ZipEntry entry = new ZipEntry(name);
            zos.putNextEntry(entry);

            FileInputStream fis = null;
            try {
                fis = new FileInputStream(file);
                byte[] byteBuffer = new byte[1024];
                int bytesRead = -1;
                while ((bytesRead = fis.read(byteBuffer)) != -1) {
                    zos.write(byteBuffer, 0, bytesRead);
                }
                zos.flush();
            } finally {
                try {
                    fis.close();
                } catch (Exception e) {
                }
            }
            zos.closeEntry();

            zos.flush();
        } finally {
            try {
                zos.close();
            } catch (Exception e) {
            }
        }
    }

    public static File zip(List<File> files, String zipFileName) {
        return zip(files, zipFileName, null);
    }

    public static File zip(List<File> files, String zipFileName, String baseFolder) {
        String folder = "";
        if (baseFolder == null || baseFolder.length() < 1) {
            folder = "";
        } else {
            if (folder.endsWith(File.separator) == false) {
                folder = baseFolder + File.separator;
            }
        }
        File zipfile = new File(zipFileName);
        byte[] buf = new byte[1024 * 4];
        try {
            ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));
            out.setLevel(Deflater.BEST_SPEED);
            for (File file : files) {
                FileInputStream in = new FileInputStream(file);
                out.putNextEntry(new ZipEntry(folder + file.getName()));
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
                out.closeEntry();
                in.close();
            }
            out.close();
            return zipfile;
        } catch (IOException ex) {
            System.err.println(ex.getMessage());
        }
        return null;
    }
}

Related

  1. zip(File destination, File... files)
  2. zip(File f, File destfile)
  3. zip(File f, File out)
  4. zip(File file, File output)
  5. zip(File file, String dest)
  6. zip(File fromFile, File toFile)
  7. zip(File input, File output)
  8. zip(File input, File outputZip)