Android InputStream Unzip unpackZip(Context context, String s, InputStream is)

Here you can find the source of unpackZip(Context context, String s, InputStream is)

Description

Unpack the given InputStream, representing ZIP file, to app cache dir

Parameter

Parameter Description
context is the context of the application
s is the series, for creating a dir named as its ID
is is the InputStream representig the zip file to unpack

Return

true if operation is successful, false otherwise

Declaration

public static boolean unpackZip(Context context, String s,
        InputStream is) 

Method Source Code

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

import java.io.File;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import android.content.Context;

public class Main {
    /**//from  w w  w  . j a v  a  2s  . c  o  m
     * Unpack the given InputStream, representing ZIP file, to app cache dir
     * 
     * @param context
     *            is the context of the application
     * @param s
     *            is the series, for creating a dir named as its ID
     * @param is
     *            is the InputStream representig the zip file to unpack
     * @return true if operation is successful, false otherwise
     */
    public static boolean unpackZip(Context context, String s,
            InputStream is) {

        ZipInputStream zis;
        try {
            String filename;
            File cache = context.getCacheDir();
            File dir = new File(cache.getAbsolutePath() + "/" + s);
            dir.mkdir();
            zis = new ZipInputStream(new BufferedInputStream(is));
            ZipEntry ze;
            byte[] buffer = new byte[1024];
            int count;

            while ((ze = zis.getNextEntry()) != null) {
                filename = ze.getName();

                // Need to create directories if not exists, or
                // it will generate an Exception...
                if (ze.isDirectory()) {
                    File fmd = new File(dir.getAbsolutePath() + filename);
                    fmd.mkdirs();
                    continue;
                }

                FileOutputStream fout = new FileOutputStream(
                        dir.getAbsolutePath() + "/" + filename);

                while ((count = zis.read(buffer)) != -1) {
                    fout.write(buffer, 0, count);
                }

                fout.close();
                zis.closeEntry();
            }

            zis.close();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }

        return true;
    }
}

Related

  1. unzip(InputStream fileIn, File dirOut)
  2. decompress(InputStream is, OutputStream os)
  3. decompress(InputStream is, OutputStream os)
  4. decompress(InputStream is, OutputStream os)
  5. uncompress(InputStream inputStream)
  6. readEntryAsWhatEverInternal( final ZipEntry entry, final InputStream unzippedInputStream)
  7. getZipFileList(InputStream is)
  8. extZipFile(InputStream is, String extPlace)
  9. decompressFile(File destFile, ZipInputStream zis)