public class MyZipFile { private static final String TAG = MyZipFile.class.getSimpleName(); public static boolean unZipFile(Context context, String inPath, String outPath) throws IOException { InputStream is = null; try { File file = new File(inPath); is = new FileInputStream(file); Log.i(TAG, "unZipFile()inPath = "+inPath+", outPath = " + outPath); if (inPath.contains(".rar") || inPath.contains(".zip")) { Log.i(TAG, "unZipFile() outPath:" + outPath); ZipInputStream zis = new ZipInputStream(is); FileOutputStream fos = null; BufferedOutputStream bos = null; boolean isZipFileOk = false; try { byte[] buffer = new byte[200 * 1024]; int index = outPath.lastIndexOf(File.separator); outPath = outPath.substring(0, index); File fileOut = new File(outPath); if (!fileOut.exists()) { fileOut.mkdirs(); } for (ZipEntry zipEntry = zis.getNextEntry(); zipEntry != null; zipEntry = zis.getNextEntry()) { String fileName = zipEntry.getName(); fileOut = new File(outPath + File.separator + fileName); if (zipEntry.isDirectory()) {//如果是一个目录 fileOut.mkdir(); } else { //如果是文件 if (fileOut.exists()) { //if (BuildConfig.DEBUG || isUpZipDeleteFile) { // if (true) { // fileOut.delete(); // } else { continue; //} } else { checkFile(fileOut.getParent()); } int count = 0; boolean ret = fileOut.createNewFile(); fos = new FileOutputStream(fileOut); bos=new BufferedOutputStream(fos); //加缓存 while ((count = zis.read(buffer)) > 0) { bos.write(buffer, 0, count); } if(bos != null) { bos.flush(); bos.close(); bos = null; } if(fos != null) fos.close(); fos = null; } } if(zis != null) { zis.close(); zis = null; } isZipFileOk = true; } catch (Exception e) { e.printStackTrace(); isZipFileOk = false; } finally { if(bos != null) { bos.flush(); bos.close(); bos = null; } if(fos != null) fos.close(); fos = null; if(zis != null) { zis.close(); zis = null; } return isZipFileOk; } } } catch (Exception e) { e.printStackTrace(); Log.i(TAG, "unZipFile() Exception:" + e.getMessage()); } return false; } /** * 检测目录,不存在则创建 * * @param filePath */ public static boolean checkFile(String filePath) { File file = new File(filePath); if (!file.exists()) { boolean ret = file.mkdirs(); return ret; } return true; } }
解压 输入解压包输出解压文件路径
最新推荐文章于 2021-08-31 14:49:37 发布