Java Zip Directory zip(File directory, File base, ZipOutputStream zos, byte[] buffer)

Here you can find the source of zip(File directory, File base, ZipOutputStream zos, byte[] buffer)

Description

zip

License

Apache License

Declaration

public static final void zip(File directory, File base, ZipOutputStream zos, byte[] buffer) 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.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Main {
    public static final void zip(File directory, File base, ZipOutputStream zos, byte[] buffer) throws IOException {
        int read = 0;
        for (File file : directory.listFiles()) {
            if (file.isDirectory()) {
                zip(file, base, zos, buffer);
            } else {
                FileInputStream in = new FileInputStream(file);
                try {
                    ZipEntry entry = new ZipEntry(file.getPath().substring(base.getPath().length() + 1));
                    zos.putNextEntry(entry);
                    while ((read = in.read(buffer)) > -1) {
                        zos.write(buffer, 0, read);
                    }// w ww.j a  v a 2 s . c o m
                } finally {
                    in.close();
                }
            }
        }
    }

    public static final void zip(File directory, File zip) throws IOException {
        byte[] buffer = new byte[8192];
        FileOutputStream fos = new FileOutputStream(zip);
        ZipOutputStream zos = new ZipOutputStream(fos);
        try {
            zip(directory, directory, zos, buffer);
        } finally {
            zos.close();
            fos.close();
        }
    }
}

Related

  1. addDirToArchive(ZipOutputStream zos, String path, File initialDir)
  2. zip(File dir, File base, ZipOutputStream out)
  3. zip(File dir, ZipOutputStream out, String prefix)
  4. zip(File dir2zip, File zipFile)
  5. zip(File directory, File base, ZipOutputStream zos)
  6. zip(File directory, File file)
  7. zip(File directory, File zipFile)
  8. zip(File directory, File zipFile)
  9. zipDirectories(List directories, File baseDirectory, File zipFile)