Java Zip Directory zip(File dir, File base, ZipOutputStream out)

Here you can find the source of zip(File dir, File base, ZipOutputStream out)

Description

zip

License

Open Source License

Declaration

private static void zip(File dir, File base, ZipOutputStream out) 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 dir, File base, ZipOutputStream out) throws IOException {
        File[] files = dir.listFiles();
        byte[] buffer = new byte[8192];
        final String FILE_SEP = System.getProperty("file.separator");

        for (int i = 0; i < files.length; i++) {
            if (files[i].isDirectory()) {
                zip(files[i], base, out); // recursively zip sub-directories
            } else {
                FileInputStream fin = new FileInputStream(files[i]);
                ZipEntry entry = new ZipEntry(
                        files[i].getPath().substring(base.getPath().length() + 1).replace(FILE_SEP, "/"));
                out.putNextEntry(entry);

                int bytes_read = 0;
                while ((bytes_read = fin.read(buffer)) != -1) {
                    out.write(buffer, 0, bytes_read);
                }//from w  w w .j  a  v  a 2 s. c om
                fin.close();
            }
        }
    }

    private static void zip(File[] files, ZipOutputStream out) throws IOException {
        byte[] buffer = new byte[8192];

        for (int i = 0; i < files.length; i++) {
            if (files[i].isDirectory()) {
                zip(files[i], files[i], out); // recursively zip sub-directories
            } else {
                FileInputStream fin = new FileInputStream(files[i]);
                ZipEntry entry = new ZipEntry(files[i].getPath());
                out.putNextEntry(entry);

                int bytes_read = 0;
                while ((bytes_read = fin.read(buffer)) != -1) {
                    out.write(buffer, 0, bytes_read);
                }
                fin.close();
            }
        }
    }

    private static void zip(File[] files, File base, ZipOutputStream out) throws IOException {
        byte[] buffer = new byte[8192];
        final String FILE_SEP = System.getProperty("file.separator");

        for (int i = 0; i < files.length; i++) {
            File file_on_disk = new File(base.getPath() + FILE_SEP + files[i].getPath());
            if (file_on_disk.isDirectory()) {
                zip(file_on_disk, base, out); // recursively zip sub-directories
            } else {
                FileInputStream fin = new FileInputStream(file_on_disk);
                ZipEntry entry = new ZipEntry(files[i].getPath().replace(FILE_SEP, "/"));
                out.putNextEntry(entry);

                int bytes_read = 0;
                while ((bytes_read = fin.read(buffer)) != -1) {
                    out.write(buffer, 0, bytes_read);
                }
                fin.close();
            }
        }
    }
}

Related

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