Android Zip Unzip Directory zipDirectory(File folder, String parentFolder, ZipOutputStream zos)

Here you can find the source of zipDirectory(File folder, String parentFolder, ZipOutputStream zos)

Description

Adds a directory to the current zip output stream

Parameter

Parameter Description
folder the directory to be added
parentFolder the path of parent directory
zos the current zip output stream

Exception

Parameter Description
FileNotFoundException an exception
IOException an exception

Declaration

private static void zipDirectory(File folder, String parentFolder,
        ZipOutputStream zos) throws FileNotFoundException, IOException 

Method Source Code

//package com.java2s;
import java.io.BufferedInputStream;

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

import java.io.IOException;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {
    /**// ww  w  .ja v a2 s  . co  m
     * Size of the buffer to read/write data
     */
    private static final int BUFFER_SIZE = 4096;

    /**
     * Adds a directory to the current zip output stream
     * 
     * @param folder
     *            the directory to be added
     * @param parentFolder
     *            the path of parent directory
     * @param zos
     *            the current zip output stream
     * @throws FileNotFoundException
     * @throws IOException
     */
    private static void zipDirectory(File folder, String parentFolder,
            ZipOutputStream zos) throws FileNotFoundException, IOException {
        for (File file : folder.listFiles()) {
            if (file.isDirectory()) {
                zipDirectory(file, parentFolder + "/" + file.getName(), zos);
                continue;
            }
            zos.putNextEntry(new ZipEntry(parentFolder + "/"
                    + file.getName()));
            BufferedInputStream bis = null;
            try {
                bis = new BufferedInputStream(new FileInputStream(file));
                long bytesRead = 0;
                byte[] bytesIn = new byte[BUFFER_SIZE];
                int read = 0;
                while ((read = bis.read(bytesIn)) != -1) {
                    zos.write(bytesIn, 0, read);
                    bytesRead += read;
                }
                zos.closeEntry();
            } finally {
                bis.close();
            }
        }
    }
}

Related

  1. zipFileAtPath(String[] sourcePaths, String toLocation)
  2. zipFileAtPath(String[] sourcePaths, String toLocation)
  3. zipFileAtPath(final String sourcePath, final String toLocation)
  4. zipDirectory(File directory, File zip)
  5. zipDirectory(File directory, File zip)
  6. compress(String filePath, int width, int height)
  7. compress(String paramString)
  8. compress(String path)
  9. compress(String path)