Android Zip Unzip File zip(String filePath, String zipPath)

Here you can find the source of zip(String filePath, String zipPath)

Description

zip

Declaration

public static boolean zip(String filePath, String zipPath) 

Method Source Code

//package com.java2s;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
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;
import android.util.Log;

public class Main {
    public static final String TAG = "ZipUtil";
    private final static int BUFFER_SIZE = 8192;

    public static boolean zip(String filePath, String zipPath) {
        try {//from   ww  w  .j  a  va  2s .c o  m
            File file = new File(filePath);
            BufferedInputStream bis = null;
            FileOutputStream fos = new FileOutputStream(zipPath);
            ZipOutputStream zos = new ZipOutputStream(
                    new BufferedOutputStream(fos));
            if (file.isDirectory()) {
                int baseLength = file.getParent().length() + 1;
                zipFolder(zos, file, baseLength);
            } else {
                byte data[] = new byte[BUFFER_SIZE];
                FileInputStream fis = new FileInputStream(filePath);
                bis = new BufferedInputStream(fis, BUFFER_SIZE);
                String entryName = file.getName();
                Log.i(TAG, "zip entry " + entryName);
                ZipEntry entry = new ZipEntry(entryName);
                zos.putNextEntry(entry);
                int count;
                while ((count = bis.read(data, 0, BUFFER_SIZE)) != -1) {
                    zos.write(data, 0, count);
                }
            }
            zos.close();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    private static void zipFolder(ZipOutputStream zos, File folder,
            int baseLength) throws IOException {
        if (zos == null || folder == null) {
            return;
        }
        File[] fileList = folder.listFiles();

        if (fileList == null || fileList.length == 0) {
            return;
        }

        for (File file : fileList) {
            if (file.isDirectory()) {
                zipFolder(zos, file, baseLength);
            } else {
                byte data[] = new byte[BUFFER_SIZE];
                String unmodifiedFilePath = file.getPath();
                String realPath = unmodifiedFilePath.substring(baseLength);
                Log.i(TAG, "zip entry " + realPath);
                FileInputStream fi = new FileInputStream(unmodifiedFilePath);
                BufferedInputStream bis = new BufferedInputStream(fi,
                        BUFFER_SIZE);
                ZipEntry entry = new ZipEntry(realPath);
                zos.putNextEntry(entry);
                int count;
                while ((count = bis.read(data, 0, BUFFER_SIZE)) != -1) {
                    zos.write(data, 0, count);
                }
                bis.close();
            }
        }
    }
}

Related

  1. gUnzip(File srcFile, File destFile)
  2. unJarDirectory(final String in, final File fOut)
  3. unzip(File pZipFile)
  4. unzip(String sourceFileName, String destPath)
  5. zip(String sourceDir, String zipFile)
  6. zip(String[] files, String destZipFilePath)
  7. zip(String[] files, String zipFile)
  8. zip(String[] files, String zipFile)