Here you can find the source of UnzipSubDir(String source, String subdir, String targetPath)
Parameter | Description |
---|---|
source | source zip files |
subdir | subdir want to unzip in the source |
target | targetPath to place out files |
public static boolean UnzipSubDir(String source, String subdir, String targetPath)
//package com.java2s; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; 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; protected static byte buf[] = new byte[BUFFER_SIZE]; private static List<Object> mTmpBuffer = new ArrayList<Object>(); /**/*w w w. j a va2 s .c o m*/ * unzip a subdir in the source file to the targetPath. similar with the * UnzipSubDir(InputStream is, String subdir, String targetPath) * * @param source * source zip files * @param subdir * subdir want to unzip in the source * @param target * targetPath to place out files * @return true if successfully. false if failed. failed if source file is * not exists. */ public static boolean UnzipSubDir(String source, String subdir, String targetPath) { File file = new File(source); if (file.exists() == false) { return false; } try { return UnzipSubDir(new FileInputStream(file), subdir, targetPath); } catch (FileNotFoundException e) { e.printStackTrace(); } return false; } /** * unzip subdir in the InputStream to the targePath. if target path is not * exists, create the dir, if the target path is exists and not a dir, * delete it. * * @param is * InputStream contain the files data * @param subdir * subdir want to out put * @param targetPath * targetPath to place the out files * @return true if successfully, false if failed. false if the inputstream * is null or the subdir is null or the targetPath is null or the * subdir is empty or the targetPath is empty. */ public static boolean UnzipSubDir(InputStream is, String subdir, String targetPath) { LOGD(" In UnzipSubDir : subdir = " + subdir + " target = " + targetPath); if (subdir == null || targetPath == null || is == null || subdir.equals("") || targetPath.equals("")) { return false; } checkDirectory(targetPath); ZipInputStream in = new ZipInputStream(is); ZipEntry entry = null; try { while ((entry = in.getNextEntry()) != null) { if (entry.getName().startsWith(subdir) == false) continue; final String fullName = targetPath + entry.getName().substring(subdir.length()); if (entry.isDirectory() == true) { File file = new File(fullName); if (file.exists() == false) { file.mkdirs(); } } else { doOutputFile(in, fullName); in.closeEntry(); } } in = null; return true; } catch (IOException e) { in = null; e.printStackTrace(); } return false; } private static void LOGD(String s) { if (DEBUG) { Log.d(TAG, s); } } private static boolean checkDirectory(String dir) { File dirObj = new File(dir); if (dirObj.exists()) { if (!dirObj.isDirectory()) { dirObj.delete(); } return false; } if (!dirObj.exists()) { dirObj.mkdirs(); } return true; } private static void doOutputFile(InputStream is, String filename) throws IOException, FileNotFoundException { FileOutputStream os = new FileOutputStream(filename); BufferedOutputStream bos = new BufferedOutputStream(os, BUFFER_SIZE); int len; while ((len = is.read(buf, 0, BUFFER_SIZE)) > 0) { bos.write(buf, 0, len); } bos.flush(); bos.close(); os.close(); mTmpBuffer.add(os); mTmpBuffer.add(bos); } }