Android Unzip File UnZipFileToMem(String source)

Here you can find the source of UnZipFileToMem(String source)

Description

Un Zip File To Mem

Declaration

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

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 .  jav  a2 s . com*/
     * @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. unZip(String zipfile, String destDir)
  2. UpZip(String zipFileString, String fileString)
  3. UpZip(String zipFileString, String fileString)
  4. UpZip(String zipFileString, String fileString)
  5. unzipArchive(File archive, File outputDir)
  6. UnZipFile(String source, String targetPath)
  7. unZip(String zipFile, String destinationDirectory)
  8. unZipFromAssets(Context ctx, String file, String destinationDirectory)