Android Zip Unzip File zip(List listFiles, String destZipFilePath)

Here you can find the source of zip(List listFiles, String destZipFilePath)

Description

Compresses a list of files to a destination zip file

Parameter

Parameter Description
listFiles A collection of files and directories
destZipFilePath The path of the destination zip file

Exception

Parameter Description
FileNotFoundException an exception
IOException an exception

Declaration

public static void zip(List<File> listFiles, String destZipFilePath)
        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.FileOutputStream;
import java.io.IOException;

import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

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

    /**
     * Compresses a list of files to a destination zip file
     * 
     * @param listFiles
     *            A collection of files and directories
     * @param destZipFilePath
     *            The path of the destination zip file
     * @throws FileNotFoundException
     * @throws IOException
     */
    public static void zip(List<File> listFiles, String destZipFilePath)
            throws FileNotFoundException, IOException {
        ZipOutputStream zos = null;
        try {
            zos = new ZipOutputStream(new FileOutputStream(destZipFilePath));
            for (File file : listFiles) {
                if (file.isDirectory()) {
                    zipDirectory(file, file.getName(), zos);
                } else {
                    zipFile(file, zos);
                }
            }
        } finally {
            zos.flush();
            zos.close();
        }
    }

    /**
     * Compresses files represented in an array of paths
     * 
     * @param files
     *            a String array containing file paths
     * @param destZipFilePath
     *            The path of the destination zip file
     * @throws FileNotFoundException
     * @throws IOException
     */
    public static void zip(String[] files, String destZipFilePath)
            throws FileNotFoundException, IOException {
        List<File> listFiles = new ArrayList<File>();
        for (int i = 0; i < files.length; i++) {
            listFiles.add(new File(files[i]));
        }
        zip(listFiles, destZipFilePath);
    }

    /**
     * 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();
            }
        }
    }

    /**
     * Adds a file to the current zip output stream
     * 
     * @param file
     *            the file to be added
     * @param zos
     *            the current zip output stream
     * @throws FileNotFoundException
     * @throws IOException
     */
    private static void zipFile(File file, ZipOutputStream zos)
            throws FileNotFoundException, IOException {
        zos.putNextEntry(new ZipEntry(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. zip(String[] files, String zipFile)
  2. zip(String[] files, String zipFile)
  3. zip(String[] files, String zipFile)
  4. zip(File srcFile, File targetFile)
  5. zip(File[] _files, File _zipFile)
  6. zipFile(File source, String basePath, ZipOutputStream zos)
  7. zipFileAtPath(File sourceFile, String toLocation)
  8. decompress(File file)
  9. decompress(File file)