Android Unzip InputStream UnZipFileToMem(InputStream is)

Here you can find the source of UnZipFileToMem(InputStream is)

Description

Un Zip File To Mem

Declaration

public static Map<String, byte[]> UnZipFileToMem(InputStream is) 

Method Source Code

//package com.java2s;

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

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import android.util.Log;

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

    /**/*from  w w w  .  j ava2  s.  c  o  m*/
     * @deprecated unzip the zip file to memeory, return a map
     */
    public static Map<String, byte[]> UnZipFileToMem(String source) {
        File file = new File(source);
        if (file.exists() == false) {
            return null;
        }
        try {
            return UnZipFileToMem(new FileInputStream(file));
        } catch (Exception e) {
        }
        return null;
    }

    /**
     * @deprecated unzip the zip InputStream to memeory, return a map
     */
    public static Map<String, byte[]> UnZipFileToMem(InputStream is) {
        Map<String, byte[]> ret = new HashMap<String, byte[]>();
        List<byte[]> dataList = new ArrayList<byte[]>();
        List<Integer> dataSizeList = new ArrayList<Integer>();
        try {
            ZipInputStream in = new ZipInputStream(is);
            ZipEntry entry = null;
            while ((entry = in.getNextEntry()) != null) {
                LOGD(" In UnZipFileToMem, the entry name:"
                        + entry.getName());
                if (entry.isDirectory() == true) {
                    ret.put(entry.getName(), null);
                } else {
                    int lenRead = 0;
                    int totalRead = 0;
                    do {
                        byte[] data = new byte[BUFFER_SIZE];
                        lenRead = in.read(data, 0, BUFFER_SIZE);
                        if (lenRead > 0) {
                            dataList.add(data);
                            dataSizeList.add(lenRead);
                            totalRead += lenRead;
                        }
                    } while (lenRead > 0);
                    in.closeEntry();
                    byte[] entryData = new byte[totalRead];
                    int index = 0;
                    for (int count = 0; count < dataList.size(); ++count) {
                        byte[] data = dataList.get(count);
                        Integer length = dataSizeList.get(count);
                        copy(data, entryData, index, length);
                        index += length;
                    }
                    ret.put(entry.getName(), entryData);
                    dataSizeList.clear();
                    dataList.clear();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return ret;
    }

    private static void LOGD(String s) {
        if (DEBUG) {
            Log.d(TAG, s);
        }
    }

    private static void copy(byte[] src, byte[] dest, int offset, int length) {
        for (int index = 0; index < length; ++index) {
            dest[offset + index] = src[index];
        }
    }
}

Related

  1. UnZipFile(InputStream is, String targetPath)