Android Zip Unzip Directory zipDirectory(File directory, File zip)

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

Description

zip Directory

Declaration

public static final void zipDirectory(File directory, File zip)
            throws IOException 

Method Source Code

//package com.java2s;

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 zipDirectory(File directory, File zip)
            throws IOException {
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zip));
        zip(directory, directory, zos);/*from  www . j a  v  a2 s  .  c  o m*/
        zos.close();
    }

    private static final 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. zipSubFolder(ZipOutputStream out, File folder, int basePathLength)
  2. zipSubFolder(final ZipOutputStream out, final File folder, final int basePathLength)
  3. zipFileAtPath(String[] sourcePaths, String toLocation)
  4. zipFileAtPath(String[] sourcePaths, String toLocation)
  5. zipFileAtPath(final String sourcePath, final String toLocation)
  6. zipDirectory(File directory, File zip)
  7. zipDirectory(File folder, String parentFolder, ZipOutputStream zos)
  8. compress(String filePath, int width, int height)
  9. compress(String paramString)