android 解压缩zip包
项目里面需要解压缩zip包;或者把项目assets里面的zip包拷贝到手机SD卡里面进行解压。
在这里我是使用的ant.jar这个库:ant.jar
废话不多说,直接附上整个工具类及使用方法:
public class UnZip{/*** 从项目assets文件夹拷贝到手机sd卡* @param context*/public static void CopyAssets(Context context, String zipName) {AssetManager assetManager = context.getAssets();InputStream in = null;OutputStream out = null;try {in = assetManager.open(zipName);out = new FileOutputStream(android.os.Environment.getExternalStorageDirectory() + "/"+ zipName);CopyFile(in, out);in.close();in = null;out.flush();out.close();out = null;} catch (Exception e) {Log.e("Test", e.getMessage());}}private static void CopyFile(InputStream in, OutputStream out) throws IOException {byte[] buf = new byte[1024];int n;while ((n = in.read(buf)) != -1) {out.write(buf, 0, n);}}/*** 解压指定zip文件* * @param unZipfile* 压缩文件的路径* @param destFile* 解压到的目录 */public static void unZip(Context context, String unZipfile, String destFile)throws IOException, FileNotFoundException, ZipException {BufferedInputStream bi;ZipFile zipFile = new ZipFile(new File(unZipfile), "GBK");@SuppressWarnings("rawtypes")Enumeration e = zipFile.getEntries();while (e.hasMoreElements()) {ZipEntry ze2 = (ZipEntry) e.nextElement();String entryName = ze2.getName();String path = destFile + "/" + entryName;if (ze2.isDirectory()) {System.out.println("正在创建解压目录 - " + entryName);File decompressDirFile = new File(path);if (!decompressDirFile.exists()) {decompressDirFile.mkdirs();}} else {System.out.println("正在创建解压文件 - " + entryName);String fileDir = path.substring(0, path.lastIndexOf("/"));File fileDirFile = new File(fileDir);if (!fileDirFile.exists()) {fileDirFile.mkdirs();}BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile + "/" + entryName));bi = new BufferedInputStream(zipFile.getInputStream(ze2));byte[] readContent = new byte[1024];int readCount = bi.read(readContent);while (readCount != -1) {bos.write(readContent, 0, readCount);readCount = bi.read(readContent);}bos.close();}}zipFile.close();}/*** 压缩文件* * @param srcFile* 需要 压缩的目录或者文件* @param destFile* 压缩文件的路径*/public static void doZip(String srcFile, String destFile) {// zipDirectoryPath:需要压缩的文件夹名File zipFile = new File(srcFile);try {// 生成ZipOutputStream,会把压缩的内容全都通过这个输出流输出,最后写到压缩文件中去ZipOutputStream zipOut = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(destFile)));// 设置压缩的注释zipOut.setComment("comment");// 设置压缩的编码,如果要压缩的路径中有中文,就用下面的编码zipOut.setEncoding("GBK");// 启用压缩zipOut.setMethod(ZipOutputStream.DEFLATED);// 压缩级别为最强压缩,但时间要花得多一点zipOut.setLevel(Deflater.BEST_COMPRESSION);handleFile(zipFile, zipOut, "");// 处理完成后关闭我们的输出流zipOut.close();} catch (IOException ioe) {ioe.printStackTrace();}}/*** 由doZip调用,递归完成目录文件读取* * @param zipFile* @param zipOut* @param dirName* 这个主要是用来记录压缩文件的一个目录层次结构的* @throws IOException*/private static void handleFile(File zipFile, ZipOutputStream zipOut, String dirName)throws IOException {System.out.println("遍历文件:" + zipFile.getName());// 如果是一个目录,则遍历if (zipFile.isDirectory()) {File[] files = zipFile.listFiles();if (files.length == 0) {// 如果目录为空,则单独创建之.// 只是放入了空目录的名字zipOut.putNextEntry(new ZipEntry(dirName + zipFile.getName()+ File.separator));zipOut.closeEntry();} else {// 如果目录不为空,则进入递归,处理下一级文件for (File file : files) {// 进入递归,处理下一级的文件handleFile(file, zipOut, dirName + zipFile.getName()+ File.separator);}}}// 如果是文件,则直接压缩else {FileInputStream fileIn = new FileInputStream(zipFile);// 放入一个ZipEntryzipOut.putNextEntry(new ZipEntry(dirName + zipFile.getName()));int length = 0;byte[] buffer = new byte[1024];// 放入压缩文件的流while ((length = fileIn.read(buffer)) > 0) {zipOut.write(buffer, 0, length);}// 关闭ZipEntry,完成一个文件的压缩zipOut.closeEntry();}}
}
场景一、从项目assets文件夹拷贝zip包到手机sd卡并解压。
1、首先拷贝zip包至sd卡:UnZip.CopyAssets(this, xxx.zip);
‘xxx.zip’参数就是压缩包的名字加后缀,这里我的包就放在assets根目录,拷贝到sd卡根目录。
2、解压拷贝至sd卡的zip包:UnZip.unZip(this, ZipFileName, UnZipLocation);
'ZipFileName' 就是zip所在的位置路径,
如:android.os.Environment.getExternalStorageDirectory() + "/xxx.zip"
'UnZipLocation' 就是需要解压到的位置,
如:android.os.Environment.getExternalStorageDirectory() + "/"
至此一个zip包就从项目里面拷贝到sd中并解压了。当然实际项目中还得判断是否有内存卡,是否文件存在等。
场景二、将sd卡中的文件夹压缩成zip包:
UnZip.doZip(FileName, ZipFileName);
‘FileName’ 就是你sd卡中文件夹路径,不管里面有多少个子文件或子文件夹都可以。这里我的路径是:
android.os.Environment.getExternalStorageDirectory() + “/文件夹名”
‘ZipFileName’ 就是需要压缩成zip包的路径。如:android.os.Environment.getExternalStorageDirectory() + “/文件夹名.zip”
好了,文件的解压缩操作大致就这些了。
android 解压缩zip包相关推荐
- Java解压缩zip包+rar包兼容rar4和rar5兼容多操作系统
业务背景:需要将zip包或者rar包进行解压缩,需要兼容rar4和rar5两种版本 maven 依赖包: <dependency><groupId>com.github.jun ...
- android 选择打开系统软件解压缩zip包
//android获取一个用于打开ZIP文件的intentpublic static Intent getZipFileIntent(String param) {Intent intent = ne ...
- android shell 解压zip,Android解压缩ZIP / GZIP数据(基于InflaterInputStream实现)
在实际的项目代码使用过程中,发现如果用Java类库标准指定的GZIPInputStream读取压缩数据解压不能稳定工作,原因不明.反而使用InflaterInputStream可以替代GZIPInpu ...
- 【SpringBoot】ZIP包 压缩解压缩
ZIP包 压缩&&解压缩 ZIP包 压缩&&解压缩 ZIP包 压缩&&解压缩 压缩:第一种方法,压缩不支持文件夹压缩,需要指定待压缩的所有文件路径. 压 ...
- java压缩/解压缩zip格式文件
因为项目要用到压缩.解压缩zip格式压缩包,只好自己封装一个,对于网上流行的中文乱码的问题,本文的解决方法是用apache的包代替jdk里的.基本上还是比较好用的. 废话少说,直接上代码. 1 pac ...
- Android系统Recovery工作原理之使用update.zip升级过程分析(一)---update.zip包的制作【转】...
本文转载自:http://blog.csdn.net/mu0206mu/article/details/7399822 这篇及以后的篇幅将通过分析update.zip包在具体Android系统升级的过 ...
- Android系统系统升级过程分析之------update.zip包的制作
这篇及以后的篇幅将通过分析update.zip包在具体Android系统升级的过程,来理解Android系统中Recovery模式服务的工作原理.我们先从update.zip包的制作开始,然后是And ...
- Android OTA升级原理和流程分析(五)---update.zip包从上层进入Recovery服务
转载自:http://blog.chinaunix.net/uid-22028566-id-3533854.html 文章开头我们就提到update.zip包来源有两种: 一个是OTA在线下载(一般下 ...
- android代码zip怎么用,Android平台实现Zip文件解压缩
[android]代码库在Android平台中如何实现Zip文件的解压缩功能呢? 因为Android内部已经集成了zlib库,对于英文和非密码的Zip文件解压缩还是比较简单的,下面给大家一个解压缩zi ...
最新文章
- cad一键标注闭合区域lisp_自从用了这个CAD神器,我下班总比别人早
- Srinath总结 架构师们遵循的 30 条设计原则
- python如何做一个数据库_Python创建一个新的Django项目(连接到MySQL数据库),python,新建,mysql...
- 第27讲:令人抓狂的 JavaScript 混淆技术
- 使用jMeter对基于SAP ID service进行Authentication的Restful API进行并发测试
- Flash Builder4.6 入门Demo_trace
- 搜索总结c++ 内存泄露问题
- 怎么保护自己的音乐作品不被盗用,用FL制作防盗水印片段。
- LDAP认证-ldap介绍
- 系统需求分析与领域建模
- 程序员如何阅读英文文档
- 了解传销系列之三 : 开心门
- mysql relay_mysql relay log参数汇总
- 囊括3大MCU+DSP开发工程
- 如何在电脑浏览器查看微信文章
- 2021年中国人工智能产业及其重点企业分析(阿里巴巴、百度、腾讯、科大讯飞)[图]
- 开发微信小程序都需要哪些资质?
- PYGAME - Event 事件
- 2020nyist第三场个人赛
- linux学习笔记2.0