本文实例为大家分享了AndroID Zip解压缩工具类的具体代码,供大家参考,具体内容如下
package com.example.zip.commons.utils;import androID.util.Log;import java.io.file;import java.io.fileinputStream;import java.io.fileOutputStream;import java.io.inputStream;import java.util.ArrayList;import java.util.List;import java.util.zip.ZipEntry;import java.util.zip.Zipfile;import java.util.zip.ZipinputStream;import java.util.zip.ZipOutputStream;/** * @author: lijuan * @description: 解压ZIP文件 * @date: 2017-04-11 * @time: 09:22 */public class ZipUtils { public static final String TAG="ZIP"; public ZipUtils(){ } /** * 解压zip到指定的路径 * @param zipfileString ZIP的名称 * @param outPathString 要解压缩路径 * @throws Exception */ public static voID UnZipFolder(String zipfileString,String outPathString) throws Exception { ZipinputStream inZip = new ZipinputStream(new fileinputStream(zipfileString)); ZipEntry zipEntry; String szname = ""; while ((zipEntry = inZip.getNextEntry()) != null) { szname = zipEntry.getname(); if (zipEntry.isDirectory()) { //获取部件的文件夹名 szname = szname.substring(0,szname.length() - 1); file folder = new file(outPathString + file.separator + szname); folder.mkdirs(); } else { Log.e(TAG,outPathString + file.separator + szname); file file = new file(outPathString + file.separator + szname); if (!file.exists()){ Log.e(TAG,"Create the file:" + outPathString + file.separator + szname); file.getParentfile().mkdirs(); file.createNewfile(); } // 获取文件的输出流 fileOutputStream out = new fileOutputStream(file); int len; byte[] buffer = new byte[1024]; // 读取(字节)字节到缓冲区 while ((len = inZip.read(buffer)) != -1) { // 从缓冲区(0)位置写入(字节)字节 out.write(buffer,len); out.flush(); } out.close(); } } inZip.close(); } public static voID UnZipFolder(String zipfileString,String outPathString,String szname) throws Exception { ZipinputStream inZip = new ZipinputStream(new fileinputStream(zipfileString)); ZipEntry zipEntry; while ((zipEntry = inZip.getNextEntry()) != null) { //szname = zipEntry.getname(); if (zipEntry.isDirectory()) { //获取部件的文件夹名 szname = szname.substring(0,len); out.flush(); } out.close(); } } inZip.close(); } /** * 压缩文件和文件夹 * @param srcfileString 要压缩的文件或文件夹 * @param zipfileString 解压完成的Zip路径 * @throws Exception */ public static voID ZipFolder(String srcfileString,String zipfileString)throws Exception { //创建ZIP ZipOutputStream outZip = new ZipOutputStream(new fileOutputStream(zipfileString)); //创建文件 file file = new file(srcfileString); //压缩 Zipfiles(file.getParent()+file.separator,file.getname(),outZip); //完成和关闭 outZip.finish(); outZip.close(); } /** * 压缩文件 * @param folderString * @param fileString * @param zipOutputSteam * @throws Exception */ private static voID Zipfiles(String folderString,String fileString,ZipOutputStream zipOutputSteam)throws Exception{ if(zipOutputSteam == null) return; file file = new file(folderString+fileString); if (file.isfile()) { ZipEntry zipEntry = new ZipEntry(fileString); fileinputStream inputStream = new fileinputStream(file); zipOutputSteam.putNextEntry(zipEntry); int len; byte[] buffer = new byte[4096]; while((len=inputStream.read(buffer)) != -1) { zipOutputSteam.write(buffer,len); } zipOutputSteam.closeEntry(); } else { //文件夹 String fileList[] = file.List(); //没有子文件和压缩 if (fileList.length <= 0) { ZipEntry zipEntry = new ZipEntry(fileString+file.separator); zipOutputSteam.putNextEntry(zipEntry); zipOutputSteam.closeEntry(); } //子文件和递归 for (int i = 0; i < fileList.length; i++) { Zipfiles(folderString,fileString+ file.separator+fileList[i],zipOutputSteam); } } } /** * 返回zip的文件输入流 * @param zipfileString zip的名称 * @param fileString ZIP的文件名 * @return inputStream * @throws Exception */ public static inputStream UpZip(String zipfileString,String fileString)throws Exception { Zipfile zipfile = new Zipfile(zipfileString); ZipEntry zipEntry = zipfile.getEntry(fileString); return zipfile.getinputStream(zipEntry); } /** * 返回ZIP中的文件列表(文件和文件夹) * @param zipfileString ZIP的名称 * @param bContainFolder 是否包含文件夹 * @param bContainfile 是否包含文件 * @return * @throws Exception */ public static List<file> GetfileList(String zipfileString,boolean bContainFolder,boolean bContainfile)throws Exception { List<file> fileList = new ArrayList<file>(); ZipinputStream inZip = new ZipinputStream(new fileinputStream(zipfileString)); ZipEntry zipEntry; String szname = ""; while ((zipEntry = inZip.getNextEntry()) != null) { szname = zipEntry.getname(); if (zipEntry.isDirectory()) { // 获取部件的文件夹名 szname = szname.substring(0,szname.length() - 1); file folder = new file(szname); if (bContainFolder) { fileList.add(folder); } } else { file file = new file(szname); if (bContainfile) { fileList.add(file); } } } inZip.close(); return fileList; }}
添加权限
<!-- 允许应用程序写数据到外部存储设备(主要是SD卡) --> <uses-permission androID:name="androID.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission androID:name="androID.permission.READ_EXTERNAL_STORAGE" /> <!-- 在sdcard中创建/删除文件的权限 --> <uses-permission androID:name="androID.permission.MOUNT_UNMOUNT_fileSYstemS" />
在具体的场景中测试一下
ZipUtils.UnZipFolder(AppConfig.DOWNLOAD_PATH + info.getfilename(),AppConfig.UNZIP_PATH);
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的Android Zip解压缩工具类分享全部内容,希望文章能够帮你解决Android Zip解压缩工具类分享所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)