Unzip InputStream to a directory - Android java.util.zip

Android examples for java.util.zip:ZipInputStream

Description

Unzip InputStream to a directory

Demo Code

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class Main{

    /**//from   w  ww  . j  a va 2  s .c om
     * Unzip to a directory.
     */
    public static void unzipToPath(InputStream is, File filePath)
            throws Exception {
        ZipInputStream zis = new ZipInputStream(is);
        unzipToPath(zis, filePath.getPath());
    }
    public static void unzipToPath(ZipInputStream zis, String path)
            throws Exception {
        byte[] buffer = new byte[10 * 1024];
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            File entryFile = new File(path, entry.getName());
            if (entry.isDirectory()) {
                if (!entryFile.exists()) {
                    entryFile.mkdirs();
                }
                continue;
            }
            if (entryFile.getParentFile() != null
                    && !entryFile.getParentFile().exists()) {
                entryFile.getParentFile().mkdirs();
            }
            if (!entryFile.exists()) {
                entryFile.createNewFile();
                entryFile.setReadable(true);
                entryFile.setExecutable(true);
            }
            streamToPath(zis, entryFile);
        }
        zis.close();
    }

}

Related Tutorials