Java Zip Directory zip(File directory, File base, ZipOutputStream zos)

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

Description

zip

License

Open Source License

Declaration

private static void zip(File directory, File base, ZipOutputStream zos) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.File;
import java.io.FileInputStream;

import java.io.IOException;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {
    private static void zip(File directory, File base, ZipOutputStream zos) throws IOException {
        File[] files = directory.listFiles();
        byte[] buffer = new byte[8192];
        int read = 0;
        for (int i = 0, n = files.length; i < n; i++) {
            if (files[i].isDirectory()) {
                zip(files[i], base, zos);
            } else {
                FileInputStream in = new FileInputStream(files[i]);
                ZipEntry entry = new ZipEntry(files[i].getPath().substring(base.getPath().length() + 1));
                zos.putNextEntry(entry);
                while (-1 != (read = in.read(buffer))) {
                    zos.write(buffer, 0, read);
                }//from   w w  w .  j a va2 s  . c  o m
                in.close();
            }
        }
    }
}

Related

  1. addDirToArchive(ZipOutputStream zos, File srcFile)
  2. addDirToArchive(ZipOutputStream zos, String path, File initialDir)
  3. zip(File dir, File base, ZipOutputStream out)
  4. zip(File dir, ZipOutputStream out, String prefix)
  5. zip(File dir2zip, File zipFile)
  6. zip(File directory, File base, ZipOutputStream zos, byte[] buffer)
  7. zip(File directory, File file)
  8. zip(File directory, File zipFile)
  9. zip(File directory, File zipFile)