Java Zip Directory zipDirectory(File folder, File outputFile)

Here you can find the source of zipDirectory(File folder, File outputFile)

Description

zip Directory

License

Open Source License

Declaration

public static void zipDirectory(File folder, File outputFile) 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.FileOutputStream;
import java.io.IOException;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {
    public static void zipDirectory(File folder, File outputFile) throws IOException {
        ZipOutputStream zos = null;
        try {//from w ww  .jav a  2 s.c om
            zos = new ZipOutputStream(new FileOutputStream(outputFile));
            zip(folder, folder, zos);
            zos.close();
        } catch (IOException ioException) {
            throw ioException;
        } finally {
            try {
                if (zos != null) {
                    zos.close();
                }
            } catch (IOException ioException) {
            }
        }
    }

    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);
                }
                in.close();
            }
        }
    }
}

Related

  1. zipDirectory(File directory, File zipFile)
  2. zipDirectory(File directory, File zipFile)
  3. zipDirectory(File directory, File zipFile, Pattern exclusion)
  4. zipDirectory(File directory, ZipOutputStream zipout, final FilenameFilter filter)
  5. zipDirectory(File dirToZip, File outputDir, String fileName)
  6. zipDirectory(File inputDirectory, File zipFile)
  7. zipDirectory(File root, File directory, ZipOutputStream zos)
  8. zipDirectory(File srcDir, File destFile)
  9. zipDirectory(File zipDir, ZipOutputStream zos)