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


推荐阅读
  • 本文讨论了Alink回归预测的不完善问题,指出目前主要针对Python做案例,对其他语言支持不足。同时介绍了pom.xml文件的基本结构和使用方法,以及Maven的相关知识。最后,对Alink回归预测的未来发展提出了期待。 ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • PHP图片截取方法及应用实例
    本文介绍了使用PHP动态切割JPEG图片的方法,并提供了应用实例,包括截取视频图、提取文章内容中的图片地址、裁切图片等问题。详细介绍了相关的PHP函数和参数的使用,以及图片切割的具体步骤。同时,还提供了一些注意事项和优化建议。通过本文的学习,读者可以掌握PHP图片截取的技巧,实现自己的需求。 ... [详细]
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • HDU 2372 El Dorado(DP)的最长上升子序列长度求解方法
    本文介绍了解决HDU 2372 El Dorado问题的一种动态规划方法,通过循环k的方式求解最长上升子序列的长度。具体实现过程包括初始化dp数组、读取数列、计算最长上升子序列长度等步骤。 ... [详细]
  • Android中高级面试必知必会,积累总结
    本文介绍了Android中高级面试的必知必会内容,并总结了相关经验。文章指出,如今的Android市场对开发人员的要求更高,需要更专业的人才。同时,文章还给出了针对Android岗位的职责和要求,并提供了简历突出的建议。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 本文讨论了如何优化解决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文件等步骤,实现同时登录两个或多个微信的效果。相比于使用虚拟机的方法,本方法更简单易行,适用于任何电脑,并且不会消耗过多系统资源。详细步骤和原理解释请参考本文内容。 ... [详细]
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社区 版权所有