最近需要对输出的文件进行压缩,了解了下android的压缩功能,源码如下:

activity调用:

try {ZipUtil2.zip(exportPath, exportPath+".zip");} catch (Exception e) {// TODO Auto-generated catch block
            e.printStackTrace();}

压缩类原型:

package com.tecsun.idc.utils;import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;public class ZipUtil2 {public static void zip(String src, String dest) throws IOException {//提供了一个数据项压缩成一个ZIP归档输出流ZipOutputStream out = null;try {File outFile = new File(dest);//源文件或者目录File fileOrDirectory = new File(src);//压缩文件路径out = new ZipOutputStream(new FileOutputStream(outFile));//如果此文件是一个文件,否则为false。if (fileOrDirectory.isFile()) {zipFileOrDirectory(out, fileOrDirectory, "");} else {//返回一个文件或空阵列。File[] entries = fileOrDirectory.listFiles();for (int i = 0; i < entries.length; i++) {// 递归压缩,更新curPathszipFileOrDirectory(out, entries[i], "");}}} catch (IOException ex) {ex.printStackTrace();} finally {//关闭输出流if (out != null) {try {out.close();} catch (IOException ex) {ex.printStackTrace();}}}}private static void zipFileOrDirectory(ZipOutputStream out,File fileOrDirectory, String curPath) throws IOException {//从文件中读取字节的输入流FileInputStream in = null;try {//如果此文件是一个目录,否则返回false。if (!fileOrDirectory.isDirectory()) {// 压缩文件byte[] buffer = new byte[4096];int bytes_read;in = new FileInputStream(fileOrDirectory);//实例代表一个条目内的ZIP归档ZipEntry entry = new ZipEntry(curPath+ fileOrDirectory.getName());//条目的信息写入底层流
        out.putNextEntry(entry);while ((bytes_read = in.read(buffer)) != -1) {out.write(buffer, 0, bytes_read);}out.closeEntry();} else {// 压缩目录File[] entries = fileOrDirectory.listFiles();for (int i = 0; i < entries.length; i++) {// 递归压缩,更新curPaths
         zipFileOrDirectory(out, entries[i], curPath+ fileOrDirectory.getName() + "/");}}} catch (IOException ex) {ex.printStackTrace();// throw ex;} finally {if (in != null) {try {in.close();} catch (IOException ex) {ex.printStackTrace();}}}}@SuppressWarnings("unchecked")public static void unzip(String zipFileName, String outputDirectory)throws IOException {ZipFile zipFile = null;try {zipFile = new ZipFile(zipFileName);Enumeration e = zipFile.entries();ZipEntry zipEntry = null;File dest = new File(outputDirectory);dest.mkdirs();while (e.hasMoreElements()) {zipEntry = (ZipEntry) e.nextElement();String entryName = zipEntry.getName();InputStream in = null;FileOutputStream out = null;try {if (zipEntry.isDirectory()) {String name = zipEntry.getName();name = name.substring(0, name.length() - 1);File f = new File(outputDirectory + File.separator+ name);f.mkdirs();} else {int index = entryName.lastIndexOf("\\");if (index != -1) {File df = new File(outputDirectory + File.separator+ entryName.substring(0, index));df.mkdirs();}index = entryName.lastIndexOf("/");if (index != -1) {File df = new File(outputDirectory + File.separator+ entryName.substring(0, index));df.mkdirs();}File f = new File(outputDirectory + File.separator+ zipEntry.getName());// f.createNewFile();in = zipFile.getInputStream(zipEntry);out = new FileOutputStream(f);int c;byte[] by = new byte[1024];while ((c = in.read(by)) != -1) {out.write(by, 0, c);}out.flush();}} catch (IOException ex) {ex.printStackTrace();throw new IOException("解压失败:" + ex.toString());} finally {if (in != null) {try {in.close();} catch (IOException ex) {}}if (out != null) {try {out.close();} catch (IOException ex) {}}}}} catch (IOException ex) {ex.printStackTrace();throw new IOException("解压失败:" + ex.toString());} finally {if (zipFile != null) {try {zipFile.close();} catch (IOException ex) {}}}}}

转载于:https://www.cnblogs.com/dream550/p/4026137.html

android zip压缩相关推荐

  1. Android 解压 Zip 压缩文件

    一.AndroidManifest.xml 添加 sd 卡读写权限 <uses-permission android:name="android.permission.WRITE_EX ...

  2. android下载zip到assets,Android将assets中的zip压缩文件解压到SD卡

    程序首先要获取写外部存储权限: 程序: package com.hu.andstar; import java.io.File; import java.io.FileOutputStream; im ...

  3. Android 解压 Zip 压缩文件

    文章目录 1.简介 2.代码结构 3.AndroidManifest.xml 添加 sd 卡读写权限 4.MainActivity 文件 5.效果查看 1.简介 Android 解压 SD卡 里的 z ...

  4. android下zip压缩文件加密解密的完美解决方案,Android之zip文件加密解压及进度条的实现...

    zip文件的解压可以使用java的zip库,但是没有实现对加密文件的解压功能,这里可以使用zip4j来实现.具体可以参看该文<Android下zip压缩文件加密解密的完美解决方案 http:// ...

  5. android 解压加密zip,zip压缩解压加密器

    zip压缩解压加密器是一款非常好用的手机文件压缩解压应用软件,zip压缩解压加密器app为用户提供了各种文件压缩功能,可以批量操作,一键解压文件.感兴趣的朋友欢迎使用西西下载! 基本内容 zip压缩解 ...

  6. Android下zip压缩文件加密解密的完美解决方案

    前言 参考博客: http://blog.csdn.net/zhangyihui1986/article/details/7724229 http://blog.csdn.net/zhangyihui ...

  7. rar for android最新版,RAR for Android手机版下载-RAR for Android 安卓压缩软件v5.30 安卓版下载-腾牛安卓网...

    RAR for Android 是一款拥有安卓系统上的压缩软件,同时也支持对损坏的压缩文件zip和rar文件进行修复处理,是一款非常不错的手机压缩软件. 软件介绍: RARLAB 的 RAR for ...

  8. Android 加密压缩文件

    一.背景 常我们再开发过程中,无论是Android 客户端程序还是桌面程序,有可能涉及到文件的上传和下载,多文件时通常做zip压缩处理,将多个文件压缩为一个zip包,方便传输,另外,客户端内也可能会涉 ...

  9. mfcclub android.zip,安卓解压神器RAR for Android Premium v5.30 build 36 Final Proper完美版

    [软件介绍] 产品特色: 感谢Balatan大神分享!!! RAR是来自RARLAB的官方版本 RAR for Android 是来自RARLAB的官方版本,是PC端 winrar 的姐妹,支持创建R ...

最新文章

  1. 文言文编程还不够好玩?这里有个16岁高中生开发的粤语编程项目,GitHub star量600+...
  2. struts2文件上传(2)
  3. 1_3 SingletonMode 单例模式
  4. Storm性能简单测试
  5. Eclipse使用————Working Set工作集
  6. linux 启动 x,(1)linux启动过程
  7. 牛顿法与拟牛顿法学习笔记
  8. PL/SQL第三课(学习笔记)
  9. 《 Ray Tracing from the Ground Up》官方代码的潜在的纹理映射问题
  10. spring+mybatis通用dao层、service层的实现
  11. 统计每小时数据的处理
  12. Chosen.1 翁克 回忆 怪事
  13. ADB Interface 找不到驱动程序 怎么破
  14. Fedora core 5.0加载ntfs分区(yum方法)
  15. position: sticky 详解
  16. 工作奇谈——使用对称密匙加密数据
  17. Mac上最好用的LaTeX编辑器:Texpad for Mac
  18. TPS54231DR IC REG BUCK ADJUSTABLE 2A 8SOIC
  19. android 图片的处理方法,Android图片处理,ImageView的属性和方法大全
  20. python输出如何加单位_如何以十为单位写出数据输出

热门文章

  1. Python标准库-string模块《未完待续》
  2. 【Vue2.0】— 全局事件总线GlobalEventBus(十九)
  3. 为什么有人说:“离职见人品”?
  4. 怎么windows升级?windows版本升级?
  5. 读四年大学,各项费用大概是多少?
  6. 2021第一场教师招聘考试3.28开始!一个月备考来得及吗?
  7. “凡事不发朋友圈的人,都是过的不好的人”你认同这句话吗为什么?
  8. 虽然“互联网电商”导致大量的“线下实体行业”受到冲击,但是“体验行业”永远不会被“互联网电商”影响
  9. 如果财富都在部分人手里会怎样?
  10. 路由器用到坏才换?路由器你在乎哪些功能?