热门标签 | 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实现压缩和解压缩文件的全内容就给大家介绍这么多,希望能够帮助到大家!


推荐阅读
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 本文讨论了Alink回归预测的不完善问题,指出目前主要针对Python做案例,对其他语言支持不足。同时介绍了pom.xml文件的基本结构和使用方法,以及Maven的相关知识。最后,对Alink回归预测的未来发展提出了期待。 ... [详细]
  • 如何实现织梦DedeCms全站伪静态
    本文介绍了如何通过修改织梦DedeCms源代码来实现全站伪静态,以提高管理和SEO效果。全站伪静态可以避免重复URL的问题,同时通过使用mod_rewrite伪静态模块和.htaccess正则表达式,可以更好地适应搜索引擎的需求。文章还提到了一些相关的技术和工具,如Ubuntu、qt编程、tomcat端口、爬虫、php request根目录等。 ... [详细]
  • Android中高级面试必知必会,积累总结
    本文介绍了Android中高级面试的必知必会内容,并总结了相关经验。文章指出,如今的Android市场对开发人员的要求更高,需要更专业的人才。同时,文章还给出了针对Android岗位的职责和要求,并提供了简历突出的建议。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • 本文讲述了如何通过代码在Android中更改Recycler视图项的背景颜色。通过在onBindViewHolder方法中设置条件判断,可以实现根据条件改变背景颜色的效果。同时,还介绍了如何修改底部边框颜色以及提供了RecyclerView Fragment layout.xml和项目布局文件的示例代码。 ... [详细]
  • 本文介绍了C#中数据集DataSet对象的使用及相关方法详解,包括DataSet对象的概述、与数据关系对象的互联、Rows集合和Columns集合的组成,以及DataSet对象常用的方法之一——Merge方法的使用。通过本文的阅读,读者可以了解到DataSet对象在C#中的重要性和使用方法。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • Mac OS 升级到11.2.2 Eclipse打不开了,报错Failed to create the Java Virtual Machine
    本文介绍了在Mac OS升级到11.2.2版本后,使用Eclipse打开时出现报错Failed to create the Java Virtual Machine的问题,并提供了解决方法。 ... [详细]
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • baresip android编译、运行教程1语音通话
    本文介绍了如何在安卓平台上编译和运行baresip android,包括下载相关的sdk和ndk,修改ndk路径和输出目录,以及创建一个c++的安卓工程并将目录考到cpp下。详细步骤可参考给出的链接和文档。 ... [详细]
  • 【Windows】实现微信双开或多开的方法及步骤详解
    本文介绍了在Windows系统下实现微信双开或多开的方法,通过安装微信电脑版、复制微信程序启动路径、修改文本文件为bat文件等步骤,实现同时登录两个或多个微信的效果。相比于使用虚拟机的方法,本方法更简单易行,适用于任何电脑,并且不会消耗过多系统资源。详细步骤和原理解释请参考本文内容。 ... [详细]
  • Android Studio Bumblebee | 2021.1.1(大黄蜂版本使用介绍)
    本文介绍了Android Studio Bumblebee | 2021.1.1(大黄蜂版本)的使用方法和相关知识,包括Gradle的介绍、设备管理器的配置、无线调试、新版本问题等内容。同时还提供了更新版本的下载地址和启动页面截图。 ... [详细]
  • 本文介绍了在SpringBoot中集成thymeleaf前端模版的配置步骤,包括在application.properties配置文件中添加thymeleaf的配置信息,引入thymeleaf的jar包,以及创建PageController并添加index方法。 ... [详细]
  • 知识图谱——机器大脑中的知识库
    本文介绍了知识图谱在机器大脑中的应用,以及搜索引擎在知识图谱方面的发展。以谷歌知识图谱为例,说明了知识图谱的智能化特点。通过搜索引擎用户可以获取更加智能化的答案,如搜索关键词"Marie Curie",会得到居里夫人的详细信息以及与之相关的历史人物。知识图谱的出现引起了搜索引擎行业的变革,不仅美国的微软必应,中国的百度、搜狗等搜索引擎公司也纷纷推出了自己的知识图谱。 ... [详细]
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社区 版权所有