Android Zip Directory zipFolder(ZipOutputStream zos, File folder, int baseLength)

Here you can find the source of zipFolder(ZipOutputStream zos, File folder, int baseLength)

Description

zip Folder

Declaration

private static void zipFolder(ZipOutputStream zos, File folder,
            int baseLength) throws IOException 

Method Source Code

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

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

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;

    private static void zipFolder(ZipOutputStream zos, File folder,
            int baseLength) throws IOException {
        if (zos == null || folder == null) {
            return;
        }//from w  w  w  .ja  va  2 s.c  om
        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. zipDirectory(File p2BeZippedDir, File pZipFile)
  2. zip(File directory, File base, ZipOutputStream zos)
  3. zip(File directory, File base, ZipOutputStream zos)