热门标签 | HotTags
当前位置:  开发笔记 > 运维 > 正文

Android如何实现压缩和解压缩文件

这篇文章主要介绍了Android实现压缩和解压文件的实例代码,涉及到批量压缩文件夹,解压缩一个文件等方面的知识点,本文介绍的非常详细,具有参考借鉴价值,感兴趣的朋友一起看下吧

废话不多说了,直接给大家贴java代码了,具体代码如下所示:

Java代码

package com.maidong.utils; 
import java.io.BufferedInputStream; 
import java.io.BufferedOutputStream; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.io.UnsupportedEncodingException; 
import java.util.ArrayList; 
import java.util.Collection; 
import java.util.Enumeration; 
import java.util.zip.ZipEntry; 
import java.util.zip.ZipException; 
import java.util.zip.ZipFile; 
import java.util.zip.ZipOutputStream; 
import org.apache.http.protocol.HTTP; 
public class ZipUtils { 
private static final int BUFF_SIZE = 1024 * 1024; // 1M Byte 
/**
* 批量压缩文件(夹)
* 
* @param resFileList
* 要压缩的文件(夹)列表
* @param zipFile
* 生成的压缩文件
* @throws IOException
* 当压缩过程出错时抛出
*/ 
public static void zipFiles(Collection resFileList, File zipFile) throws IOException { 
ZipOutputStream zipout = null; 
try { 
zipout = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile), BUFF_SIZE)); 
for (File resFile : resFileList) { 
zipFile(resFile, zipout, ""); 
} 
} finally { 
if (zipout != null) 
zipout.close(); 
} 
} 
/**
* 批量压缩文件(夹)
* 
* @param resFileList
* 要压缩的文件(夹)列表
* @param zipFile
* 生成的压缩文件
* @param comment
* 压缩文件的注释
* @throws IOException
* 当压缩过程出错时抛出
*/ 
public static void zipFiles(Collection resFileList, File zipFile, String comment) throws IOException { 
ZipOutputStream zipout = null; 
try { 
zipout = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile), BUFF_SIZE)); 
for (File resFile : resFileList) { 
zipFile(resFile, zipout, ""); 
} 
zipout.setComment(comment); 
} finally { 
if (zipout != null) 
zipout.close(); 
} 
} 
/**
* 解压缩一个文件
* 
* @param zipFile
* 压缩文件
* @param folderPath
* 解压缩的目标目录
* @throws IOException
* 当解压缩过程出错时抛出
*/ 
public static void upZipFile(File zipFile, String folderPath) throws ZipException, IOException { 
File desDir = new File(folderPath); 
if (!desDir.exists()) { 
desDir.mkdirs(); 
} 
ZipFile zf = new ZipFile(zipFile); 
InputStream in = null; 
OutputStream out = null; 
try { 
for (Enumeration<&#63;> entries = zf.entries(); entries.hasMoreElements();) { 
ZipEntry entry = ((ZipEntry) entries.nextElement()); 
in = zf.getInputStream(entry); 
String str = folderPath + File.separator + entry.getName(); 
str = new String(str.getBytes("8859_1"), HTTP.UTF_8); 
File desFile = new File(str); 
if (!desFile.exists()) { 
File fileParentDir = desFile.getParentFile(); 
if (!fileParentDir.exists()) { 
fileParentDir.mkdirs(); 
} 
desFile.createNewFile(); 
} 
out = new FileOutputStream(desFile); 
byte buffer[] = new byte[BUFF_SIZE]; 
int realLength; 
while ((realLength = in.read(buffer)) > 0) { 
out.write(buffer, 0, realLength); 
} 
} 
} finally { 
if (in != null) 
in.close(); 
if (out != null) 
out.close(); 
} 
} 
/**
* 解压文件名包含传入文字的文件
* 
* @param zipFile
* 压缩文件
* @param folderPath
* 目标文件夹
* @param nameContains
* 传入的文件匹配名
* @throws ZipException
* 压缩格式有误时抛出
* @throws IOException
* IO错误时抛出
*/ 
public static ArrayList upZipSelectedFile(File zipFile, String folderPath, String nameContains) throws ZipException, 
IOException { 
ArrayList fileList = new ArrayList(); 
File desDir = new File(folderPath); 
if (!desDir.exists()) { 
desDir.mkdir(); 
} 
ZipFile zf = new ZipFile(zipFile); 
InputStream in = null; 
OutputStream out = null; 
try { 
for (Enumeration<&#63;> entries = zf.entries(); entries.hasMoreElements();) { 
ZipEntry entry = ((ZipEntry) entries.nextElement()); 
if (entry.getName().contains(nameContains)) { 
in = zf.getInputStream(entry); 
String str = folderPath + File.separator + entry.getName(); 
str = new String(str.getBytes("8859_1"), HTTP.UTF_8); 
// str.getBytes(AppConstans.UTF_8),"8859_1" 输出 
// str.getBytes("8859_1"),AppConstans.UTF_8 输入 
File desFile = new File(str); 
if (!desFile.exists()) { 
File fileParentDir = desFile.getParentFile(); 
if (!fileParentDir.exists()) { 
fileParentDir.mkdirs(); 
} 
desFile.createNewFile(); 
} 
out = new FileOutputStream(desFile); 
byte buffer[] = new byte[BUFF_SIZE]; 
int realLength; 
while ((realLength = in.read(buffer)) > 0) { 
out.write(buffer, 0, realLength); 
} 
fileList.add(desFile); 
} 
} 
} finally { 
if (in != null) 
in.close(); 
if (out != null) 
out.close(); 
} 
return fileList; 
} 
/**
* 获得压缩文件内文件列表
* 
* @param zipFile
* 压缩文件
* @return 压缩文件内文件名称
* @throws ZipException
* 压缩文件格式有误时抛出
* @throws IOException
* 当解压缩过程出错时抛出
*/ 
public static ArrayList getEntriesNames(File zipFile) throws ZipException, IOException { 
ArrayList entryNames = new ArrayList(); 
Enumeration<&#63;> entries = getEntriesEnumeration(zipFile); 
while (entries.hasMoreElements()) { 
ZipEntry entry = ((ZipEntry) entries.nextElement()); 
entryNames.add(new String(getEntryName(entry).getBytes(HTTP.UTF_8), "8859_1")); 
} 
return entryNames; 
} 
/**
* 获得压缩文件内压缩文件对象以取得其属性
* 
* @param zipFile
* 压缩文件
* @return 返回一个压缩文件列表
* @throws ZipException
* 压缩文件格式有误时抛出
* @throws IOException
* IO操作有误时抛出
*/ 
public static Enumeration<&#63;> getEntriesEnumeration(File zipFile) throws ZipException, IOException { 
ZipFile zf = new ZipFile(zipFile); 
return zf.entries(); 
} 
/**
* 取得压缩文件对象的注释
* 
* @param entry
* 压缩文件对象
* @return 压缩文件对象的注释
* @throws UnsupportedEncodingException
*/ 
public static String getEntryComment(ZipEntry entry) throws UnsupportedEncodingException { 
return new String(entry.getComment().getBytes(HTTP.UTF_8), "8859_1"); 
} 
/**
* 取得压缩文件对象的名称
* 
* @param entry
* 压缩文件对象
* @return 压缩文件对象的名称
* @throws UnsupportedEncodingException
*/ 
public static String getEntryName(ZipEntry entry) throws UnsupportedEncodingException { 
return new String(entry.getName().getBytes(HTTP.UTF_8), "8859_1"); 
} 
/**
* 压缩文件
* 
* @param resFile
* 需要压缩的文件(夹)
* @param zipout
* 压缩的目的文件
* @param rootpath
* 压缩的文件路径
* @throws FileNotFoundException
* 找不到文件时抛出
* @throws IOException
* 当压缩过程出错时抛出
*/ 
private static void zipFile(File resFile, ZipOutputStream zipout, String rootpath) throws FileNotFoundException, IOException { 
rootpath = rootpath + (rootpath.trim().length() == 0 &#63; "" : File.separator) + resFile.getName(); 
rootpath = new String(rootpath.getBytes("8859_1"), HTTP.UTF_8); 
BufferedInputStream in = null; 
try { 
if (resFile.isDirectory()) { 
File[] fileList = resFile.listFiles(); 
for (File file : fileList) { 
zipFile(file, zipout, rootpath); 
} 
} else { 
byte buffer[] = new byte[BUFF_SIZE]; 
in = new BufferedInputStream(new FileInputStream(resFile), BUFF_SIZE); 
zipout.putNextEntry(new ZipEntry(rootpath)); 
int realLength; 
while ((realLength = in.read(buffer)) != -1) { 
zipout.write(buffer, 0, realLength); 
} 
in.close(); 
zipout.flush(); 
zipout.closeEntry(); 
} 
} finally { 
if (in != null) 
in.close(); 
// if (zipout != null); 
// zipout.close(); 
} 
} 
} 

代码到此结束,关于Android实现压缩和解压缩文件的全内容就给大家介绍这么多,希望能够帮助到大家!


推荐阅读
  • 深入理解 Oracle 存储函数:计算员工年收入
    本文介绍如何使用 Oracle 存储函数查询特定员工的年收入。我们将详细解释存储函数的创建过程,并提供完整的代码示例。 ... [详细]
  • 本文总结了2018年的关键成就,包括职业变动、购车、考取驾照等重要事件,并分享了读书、工作、家庭和朋友方面的感悟。同时,展望2019年,制定了健康、软实力提升和技术学习的具体目标。 ... [详细]
  • 在计算机技术的学习道路上,51CTO学院以其专业性和专注度给我留下了深刻印象。从2012年接触计算机到2014年开始系统学习网络技术和安全领域,51CTO学院始终是我信赖的学习平台。 ... [详细]
  • CSS 布局:液态三栏混合宽度布局
    本文介绍了如何使用 CSS 实现液态的三栏布局,其中各栏具有不同的宽度设置。通过调整容器和内容区域的属性,可以实现灵活且响应式的网页设计。 ... [详细]
  • Linux 系统启动故障排除指南:MBR 和 GRUB 问题
    本文详细介绍了 Linux 系统启动过程中常见的 MBR 扇区和 GRUB 引导程序故障及其解决方案,涵盖从备份、模拟故障到恢复的具体步骤。 ... [详细]
  • 本文介绍了如何使用jQuery根据元素的类型(如复选框)和标签名(如段落)来获取DOM对象。这有助于更高效地操作网页中的特定元素。 ... [详细]
  • 本文将详细介绍如何使用剪映应用中的镜像功能,帮助用户轻松实现视频的镜像效果。通过简单的步骤,您可以快速掌握这一实用技巧。 ... [详细]
  • 深入理解Cookie与Session会话管理
    本文详细介绍了如何通过HTTP响应和请求处理浏览器的Cookie信息,以及如何创建、设置和管理Cookie。同时探讨了会话跟踪技术中的Session机制,解释其原理及应用场景。 ... [详细]
  • 本文介绍如何在 Xcode 中使用快捷键和菜单命令对多行代码进行缩进,包括右缩进和左缩进的具体操作方法。 ... [详细]
  • 本文介绍了一款用于自动化部署 Linux 服务的 Bash 脚本。该脚本不仅涵盖了基本的文件复制和目录创建,还处理了系统服务的配置和启动,确保在多种 Linux 发行版上都能顺利运行。 ... [详细]
  • 在Linux系统中配置并启动ActiveMQ
    本文详细介绍了如何在Linux环境中安装和配置ActiveMQ,包括端口开放及防火墙设置。通过本文,您可以掌握完整的ActiveMQ部署流程,确保其在网络环境中正常运行。 ... [详细]
  • Android 渐变圆环加载控件实现
    本文介绍了如何在 Android 中创建一个自定义的渐变圆环加载控件,该控件已在多个知名应用中使用。我们将详细探讨其工作原理和实现方法。 ... [详细]
  • 如何在WPS Office for Mac中调整Word文档的文字排列方向
    本文将详细介绍如何使用最新版WPS Office for Mac调整Word文档中的文字排列方向。通过这些步骤,用户可以轻松更改文本的水平或垂直排列方式,以满足不同的排版需求。 ... [详细]
  • 本文总结了在使用Ionic 5进行Android平台APK打包时遇到的问题,特别是针对QRScanner插件的改造。通过详细分析和提供具体的解决方法,帮助开发者顺利打包并优化应用性能。 ... [详细]
  • 360SRC安全应急响应:从漏洞提交到修复的全过程
    本文详细介绍了360SRC平台处理一起关键安全事件的过程,涵盖从漏洞提交、验证、排查到最终修复的各个环节。通过这一案例,展示了360在安全应急响应方面的专业能力和严谨态度。 ... [详细]
author-avatar
逗壳
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有