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压缩一共分为三步:

  1. 获取相应的ZipOutputStream;
  2. 判断是否是文件夹

​ (1)是:递归;

​ (2)否:根据文件路径、文件名等获取相应的ZipEntry,然后将该文件写入ZipOutputStream;

  1. 关闭相应的输出流;

    /*** 压缩文件* @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文件压缩与解压相关推荐

  1. 7z001怎么解压在安卓手机上面_安卓zip文件压缩RAR解压手机下载-安卓zip文件压缩RAR解压v1.0最新版下载...

    安卓zip文件压缩RAR解压是一款非常好用的手机压缩解压缩神器,在安卓zip文件压缩RAR解压上我们可以看到很多的实用的功能,软件可以帮助我们更好的处理我们手机中的文件,感兴趣的朋友赶紧下载安卓zip ...

  2. cordova 安卓文件多选_安卓zip文件压缩RAR解压软件下载-安卓zip文件压缩RAR解压下载v3.0.4安卓版...

    安卓zip文件压缩RAR解压是一款非常好用的手机压缩解压缩神器,在安卓zip文件压缩RAR解压上我们可以看到很多的实用的功能,软件可以帮助我们更好的处理我们手机中的文件,感兴趣的朋友赶紧下载安卓zip ...

  3. java 操作Zip文件(压缩、解压、加密)

    java 操作Zip文件(压缩.解压.加密) 依赖:点击下载 package com.zxl.test;import net.lingala.zip4j.model.ZipParameters; im ...

  4. 【Android】Android开发文件压缩与解压

    压缩文件或目录 public static void zip(String src,String dest) throws IOException {//定义压缩输出流ZipOutputStream ...

  5. java zip加密压缩_Java解压和压缩带密码的zip文件过程详解

    前言 JDK自带的ZIP操作接口(java.util.zip包,请参看文章末尾的博客链接)并不支持密码,甚至也不支持中文文件名. 为了解决ZIP压缩文件的密码问题,在网上搜索良久,终于找到了winzi ...

  6. c# 文件压缩、解压及下载

    C#打包文件夹成zip格式(包括文件夹和子文件夹下的所有文件) C# 文件压缩与解压(ZIP格式) asp.net实现文件夹及文件压缩,并实现下载 转载于:https://www.cnblogs.co ...

  7. 7z文件压缩、解压 (7zTool.exe)

    工具下载 压缩为7z: 调用zip()函数 7z解压缩: 调用unzip()函数 using System; using System.Collections.Generic; using Syste ...

  8. linux把一个大文件压缩,linux大文件压缩及解压需要注意问题

    注意: 大文件压缩及解压需要在后台进行,如果要查看解压详情,就要输出重定向. 远程服务器,要防止网络断开连接,导致终端关闭,此时终端断开,即使后台进行,解压以及压缩也会停止.解决方法:在指令前加noh ...

  9. Linux文件压缩与解压命令

    1  .zip 格式压缩与解压 压缩命令 zip 压缩文件名 源文件 zip  -r   压缩目录名       源目录 解压命令 unzip 文件名 td@td-Lenovo-IdeaPad-Y41 ...

  10. 文件压缩、解压 (ZipTool.exe)

     工具下载 压缩: 调用zip()函数. 解压缩:调用unzip()函数 添加ZipTool类至应用中,即可实现文件压缩.解压逻辑. using System; using System.Collec ...

最新文章

  1. Nginx 主要应用场景
  2. [YTU]_2626( B 统计程序设计基础课程学生的平均成绩)
  3. react的bind(this)
  4. DynaSLAM跑通的辛酸之路
  5. mapreduce的shuffle机制(来自学习笔记)
  6. linux系统基础与应用,Linux操作系统:基础、原理与应用
  7. day6 break continue for
  8. idea全局搜索快捷键总结
  9. mobile_net在spyder下载不下来
  10. 前端工作中的一些解决问题的方法,可以参考一下
  11. vant swipe 三图一屏
  12. kubectl 命令详解(三十五):rollout undo
  13. 计算机硬盘容量单位换算,为什么新硬盘容量不对是什么原因?原来是硬盘容量单位换算的问题...
  14. qq浏览器的两种开发者工具
  15. 每日思考第 76 期:真正的死亡是被人遗忘
  16. 软件测试工程师应该具备哪些能力?
  17. Calendars and DateFormats should not be static
  18. 【C语言】全面解析指针,指针知识点整理
  19. 全球及中国铟行业发展状况与投资前景建议报告2022-2028年
  20. 计算机专业入学讲座,新生入学教育——计算机专业介绍

热门文章

  1. 2018最新的Java黑马视频教程,包含十次方和乐优商城项目
  2. 主管都在用项目管理Excel表格模板管理项目
  3. 做教学直播时,如何做PPT课件直播?
  4. python-opencv 帧差法目标检测
  5. 好货推荐!两款免费的 Linux 桌面录制工具
  6. 史上最全的自动驾驶研究报告(上)
  7. matlab中simulink文件批量修改版本
  8. 根据经纬度计算两点之间的距离
  9. 国二c语言和南开100题,全国计算机二级C语言题库_南开100题.doc
  10. 学习笔记4--惯性导航及总结