Android zip文件压缩与解压
Android zip文件压缩与解压
Android开发中偶尔需要用到zip文件的压缩与解压,正好公司项目需要用到,趁此机会特意总结了下,分享给大家,也是对我学习Android的记录。
zip压缩
Android中的zip压缩主要用到两个类:ZipEntry,ZipOutputStream,ZipEntry类用于保存一些被压缩文件的信息,如文件名、修改时间等等,部分源码如下:
class ZipEntry implements ZipConstants, Cloneable {String name; // entry namelong time = -1; // modification time (in DOS time)long crc = -1; // crc-32 of entry datalong size = -1; // uncompressed size of entry datalong csize = -1; // compressed size of entry dataint method = -1; // compression methodint flag = 0; // general purpose flagbyte[] extra; // optional extra field data for entryString comment; // optional comment string for entry// Android-changed: Add dataOffset for internal use.long dataOffset;
而ZipOutputStream是目标zip文件的输出流。zip压缩一共分为三步:
- 获取相应的ZipOutputStream;
- 判断是否是文件夹
(1)是:递归;
(2)否:根据文件路径、文件名等获取相应的ZipEntry,然后将该文件写入ZipOutputStream;
关闭相应的输出流;
/*** 压缩文件* @param srcFile 待压缩的源文件* @param rootPath 源文件的根路径* @param zos Zip输出流* @param comment 备注* @return 压缩成功返回true* @throws IOException*/ private static boolean zipFile(final File srcFile,String rootPath,final ZipOutputStream zos,final String comment) throws IOException {rootPath = rootPath + (isSpace(rootPath) ? "" : File.separator) + srcFile.getName();if (srcFile.isDirectory()) {File[] fileList = srcFile.listFiles();if (fileList == null || fileList.length <= 0) {ZipEntry entry = new ZipEntry(rootPath + '/');entry.setComment(comment);zos.putNextEntry(entry);zos.closeEntry();} else {for (File file : fileList) {if (!zipFile(file, rootPath, zos, comment)) return false;}}} else {InputStream is = null;try {is = new BufferedInputStream(new FileInputStream(srcFile));ZipEntry entry = new ZipEntry(rootPath);entry.setComment(comment);zos.putNextEntry(entry);byte buffer[] = new byte[BUFFER_LEN];int len;while ((len = is.read(buffer, 0, BUFFER_LEN)) != -1) {zos.write(buffer, 0, len);}zos.closeEntry();} finally {CloseUtils.closeIO(is);}}return true;}
zip解压
/*** Unzip the file by keyword.** @param zipFile The ZIP file.* @param destDir The destination directory.* @param keyword The keyboard.* @return the unzipped files* @throws IOException if unzip unsuccessfully*/public static List<File> unzipFileByKeyword(final File zipFile,final File destDir,final String keyword) throws IOException {if (zipFile == null || destDir == null) return null;List<File> files = new ArrayList<>();ZipFile zip = new ZipFile(zipFile);Enumeration<?> entries = zip.entries();try {if (isSpace(keyword)) {while (entries.hasMoreElements()) {ZipEntry entry = ((ZipEntry) entries.nextElement());String entryName = entry.getName();if (entryName.contains("../")) {Log.e("ZipUtils", "it's dangerous!");//防止被利用漏洞恶意修改文件return files;}if (!unzipChildFile(destDir, files, zip, entry)) return files;}} else {while (entries.hasMoreElements()) {ZipEntry entry = ((ZipEntry) entries.nextElement());String entryName = entry.getName();if (entryName.contains("../")) {Log.e("ZipUtils", "it's dangerous!");return files;}if (entryName.contains(keyword)) {if (!unzipChildFile(destDir, files, zip, entry)) return files;}}}} finally {zip.close();}return files;}private static boolean unzipChildFile(final File destDir,final List<File> files,final ZipFile zip,final ZipEntry entry)throws IOException {File file = new File(destDir, entry.getName());files.add(file);if (entry.isDirectory()) {return createOrExistsDir(file);//创建文件夹} else {if (!createOrExistsFile(file)) return false;InputStream in = null;OutputStream out = null;try {in = new BufferedInputStream(zip.getInputStream(entry));out = new BufferedOutputStream(new FileOutputStream(file));byte buffer[] = new byte[BUFFER_LEN];int len;while ((len = in.read(buffer)) != -1) {out.write(buffer, 0, len);}} finally {if (in != null) {in.close();}if (out != null) {out.close();}}}return true;}
备注:代码实例来自开源框架AndroidUtilCode,https://github.com/Blankj/AndroidUtilCode 开源了各种开发过程中的常用工具,五星推荐。
Android zip文件压缩与解压相关推荐
- 7z001怎么解压在安卓手机上面_安卓zip文件压缩RAR解压手机下载-安卓zip文件压缩RAR解压v1.0最新版下载...
安卓zip文件压缩RAR解压是一款非常好用的手机压缩解压缩神器,在安卓zip文件压缩RAR解压上我们可以看到很多的实用的功能,软件可以帮助我们更好的处理我们手机中的文件,感兴趣的朋友赶紧下载安卓zip ...
- cordova 安卓文件多选_安卓zip文件压缩RAR解压软件下载-安卓zip文件压缩RAR解压下载v3.0.4安卓版...
安卓zip文件压缩RAR解压是一款非常好用的手机压缩解压缩神器,在安卓zip文件压缩RAR解压上我们可以看到很多的实用的功能,软件可以帮助我们更好的处理我们手机中的文件,感兴趣的朋友赶紧下载安卓zip ...
- java 操作Zip文件(压缩、解压、加密)
java 操作Zip文件(压缩.解压.加密) 依赖:点击下载 package com.zxl.test;import net.lingala.zip4j.model.ZipParameters; im ...
- 【Android】Android开发文件压缩与解压
压缩文件或目录 public static void zip(String src,String dest) throws IOException {//定义压缩输出流ZipOutputStream ...
- java zip加密压缩_Java解压和压缩带密码的zip文件过程详解
前言 JDK自带的ZIP操作接口(java.util.zip包,请参看文章末尾的博客链接)并不支持密码,甚至也不支持中文文件名. 为了解决ZIP压缩文件的密码问题,在网上搜索良久,终于找到了winzi ...
- c# 文件压缩、解压及下载
C#打包文件夹成zip格式(包括文件夹和子文件夹下的所有文件) C# 文件压缩与解压(ZIP格式) asp.net实现文件夹及文件压缩,并实现下载 转载于:https://www.cnblogs.co ...
- 7z文件压缩、解压 (7zTool.exe)
工具下载 压缩为7z: 调用zip()函数 7z解压缩: 调用unzip()函数 using System; using System.Collections.Generic; using Syste ...
- linux把一个大文件压缩,linux大文件压缩及解压需要注意问题
注意: 大文件压缩及解压需要在后台进行,如果要查看解压详情,就要输出重定向. 远程服务器,要防止网络断开连接,导致终端关闭,此时终端断开,即使后台进行,解压以及压缩也会停止.解决方法:在指令前加noh ...
- Linux文件压缩与解压命令
1 .zip 格式压缩与解压 压缩命令 zip 压缩文件名 源文件 zip -r 压缩目录名 源目录 解压命令 unzip 文件名 td@td-Lenovo-IdeaPad-Y41 ...
- 文件压缩、解压 (ZipTool.exe)
工具下载 压缩: 调用zip()函数. 解压缩:调用unzip()函数 添加ZipTool类至应用中,即可实现文件压缩.解压逻辑. using System; using System.Collec ...
最新文章
- BZOJ.1032.[JSOI2007]祖码(区间DP)
- android listview 列加id,Android实战开发之ListView同一个item显示2列的实现方法
- 电路知识--认识原理图(三)
- LiveVideoStack线上分享第五季(七):开源流媒体服务器:为何一定得再撸个新的...
- dotNET Core 3.X 使用 Web API
- [CareerCup] 4.7 Lowest Common Ancestor of a Binary Search Tree 二叉树的最小共同父节点
- Enterprise Library 系列教程
- 磁盘测试----fio
- 【白皮书分享】2022新职业教育洞察白皮书:“职”成机遇,“育”见未来.pdf...
- 【新手向】阿里云上ubuntu+flask+gunicorn+nginx服务器部署(二)项目部署
- velocity 时间显示 时间格式化 时间转化
- Linux下一种高效多定时器实现,Linux下一种高效多定时器实现
- vue学习笔记-11-自定义指令
- 解决苹果手机ios系统app store无法下载讯飞有声的问题
- Pygame简易版2048小游戏:超详细解说,看完还不会可以剁手了(附完整源码)
- 爪哇国新游记之八----读写文件及数组排序
- GeoGebra 实例 时钟
- PostgreSQL问题解决--连接数过多
- 计算机组成原理oe表示什么意思,计算机组成原理课后习题答案解析
- 常用损失函数总结(L1 loss、L2 loss、Negative Log-Likelihood loss、Cross-Entropy loss、Hinge Embedding loss、Margi)
热门文章
- 软件项目活动图 关键路径
- 2021年最好用&完全免费的图片压缩网站、软件推荐(包括GIF)
- matlab保留有效数字指数形式,Matlab中数值计算精度
- uniapp H5页面 点击图片放大预览
- 实时互动白板_使用froala文本编辑器构建实时协作白板第1部分
- CCA分析图如何解读_微生物群落与环境因子关联:全自动的CCA/RDA分析流程!!...
- 二维码的实现原理和实现过程[纠错码编码]
- 表格中计算机设置,如何在excel表格中设置下拉菜单?一招教你搞定!
- java entry的用法_Java ZipEntry setComment()用法及代码示例
- 51单片机串行口波特率计算