Java tutorial
//package com.java2s; //License from project: Apache License import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.security.MessageDigest; import java.util.ArrayList; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import android.util.Log; public class Main { private static final String TAG = "NarUtil"; private static void extractZipToPath(ArrayList<ZipEntry> entries, ZipFile nar, String targetPath) throws IOException { extractZipToPath(entries, nar, targetPath, false); } private static void extractZipToPath(ArrayList<ZipEntry> entries, ZipFile nar, String targetPath, boolean strip) throws IOException { Log.d(TAG, " =>extracting to" + targetPath); checkAndMakeDir(targetPath); for (ZipEntry e : entries) { if (e.isDirectory()) { //checkAndMakeDir(targetPath + e.getName()); } else { extractFileToPath(nar, targetPath, e, false, strip); } } } private static void checkAndMakeDir(String dir) { File f = new File(dir); if (f.isDirectory() == false) { Log.d(TAG, " ->creating dir:" + dir); if (f.mkdirs() == false) Log.d(TAG, "failed to create dir"); } } private static void extractFileToPath(ZipFile nar, String targetPath, ZipEntry e, boolean ignorename, boolean strip) throws FileNotFoundException, IOException { File f = null; if (ignorename) f = new File(targetPath); else { f = new File(targetPath + "/" + (strip ? stripExtraLevel(e.getName()) : e.getName())); } File fP = f.getParentFile(); if (fP != null && fP.exists() == false) { boolean s = fP.mkdirs(); Log.d(TAG, "fp make" + s); } FileOutputStream os = new FileOutputStream(f); InputStream is = nar.getInputStream(e); copyFile(is, os); } private static String stripExtraLevel(String inPath) { Log.d(TAG, "inPath is:" + inPath); // find first '/' in the path and return substring int firstSlashIndex = inPath.indexOf('/'); if (firstSlashIndex > 0) { return inPath.substring(firstSlashIndex + 1); } return inPath; } public static byte[] copyFile(InputStream is, FileOutputStream os) throws IOException { MessageDigest digester = null; try { digester = MessageDigest.getInstance("MD5"); byte[] buffer = new byte[16 * 1024]; int length; while ((length = is.read(buffer)) > 0) { os.write(buffer, 0, length); digester.update(buffer, 0, length); } os.flush(); os.close(); //os.getFD().sync(); //Log.d(TAG, "done copying"); } catch (Exception e) { e.printStackTrace(); } finally { is.close(); Log.d(TAG, "done copying"); } if (digester != null) { byte[] digest = digester.digest(); return digest; } return null; } }