[Android开发]zip文件压缩解压缩
//----------------- DirTraversal.java
package com.once;
import java.io.File;
import java.util.ArrayList;
import java.util.LinkedList;
/**
* 文件夹遍历
* @author once
*
*/
public class DirTraversal {
//no recursion
public static LinkedList<File> listLinkedFiles(String strPath) {
LinkedList<File> list = new LinkedList<File>();
File dir = new File(strPath);
File file[] = dir.listFiles();
for (int i = 0; i < file.length; i++) {
if (file[i].isDirectory())
list.add(file[i]);
else
System.out.println(file[i].getAbsolutePath());
}
File tmp;
while (!list.isEmpty()) {
tmp = (File) list.removeFirst();
if (tmp.isDirectory()) {
file = tmp.listFiles();
if (file == null)
continue;
for (int i = 0; i < file.length; i++) {
if (file[i].isDirectory())
list.add(file[i]);
else
System.out.println(file[i].getAbsolutePath());
}
} else {
System.out.println(tmp.getAbsolutePath());
}
}
return list;
}
//recursion
public static ArrayList<File> listFiles(String strPath) {
return refreshFileList(strPath);
}
public static ArrayList<File> refreshFileList(String strPath) {
ArrayList<File> filelist = new ArrayList<File>();
File dir = new File(strPath);
File[] files = dir.listFiles();
if (files == null)
return null;
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
refreshFileList(files[i].getAbsolutePath());
} else {
if(files[i].getName().toLowerCase().endsWith("zip"))
filelist.add(files[i]);
}
}
return filelist;
}
}
//----------------- ZipUtils.java
package com.once;
import java.io.*;
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;
/**
* Java utils 实现的Zip工具
*
* @author once
*/
public class ZipUtils {
private static final int BUFF_SIZE = 1024 * 1024; // 1M Byte
/**
* 批量压缩文件(夹)
*
* @param resFileList 要压缩的文件(夹)列表
* @param zipFile 生成的压缩文件
* @throws IOException 当压缩过程出错时抛出
*/
public static void zipFiles(Collection<File> resFileList, File zipFile) throws IOException {
ZipOutputStream zipout = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(
zipFile), BUFF_SIZE));
for (File resFile : resFileList) {
zipFile(resFile, zipout, "");
}
zipout.close();
}
/**
* 批量压缩文件(夹)
*
* @param resFileList 要压缩的文件(夹)列表
* @param zipFile 生成的压缩文件
* @param comment 压缩文件的注释
* @throws IOException 当压缩过程出错时抛出
*/
public static void zipFiles(Collection<File> resFileList, File zipFile, String comment)
throws IOException {
ZipOutputStream zipout = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(
zipFile), BUFF_SIZE));
for (File resFile : resFileList) {
zipFile(resFile, zipout, "");
}
zipout.setComment(comment);
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);
for (Enumeration<?> entries = zf.entries(); entries.hasMoreElements();) {
ZipEntry entry = ((ZipEntry)entries.nextElement());
InputStream in = zf.getInputStream(entry);
String str = folderPath + File.separator + entry.getName();
str = new String(str.getBytes("8859_1"), "GB2312");
File desFile = new File(str);
if (!desFile.exists()) {
File fileParentDir = desFile.getParentFile();
if (!fileParentDir.exists()) {
fileParentDir.mkdirs();
}
desFile.createNewFile();
}
OutputStream out = new FileOutputStream(desFile);
byte buffer[] = new byte[BUFF_SIZE];
int realLength;
while ((realLength = in.read(buffer)) > 0) {
out.write(buffer, 0, realLength);
}
in.close();
out.close();
}
}
/**
* 解压文件名包含传入文字的文件
*
* @param zipFile 压缩文件
* @param folderPath 目标文件夹
* @param nameContains 传入的文件匹配名
* @throws ZipException 压缩格式有误时抛出
* @throws IOException IO错误时抛出
*/
public static ArrayList<File> upZipSelectedFile(File zipFile, String folderPath,
String nameContains) throws ZipException, IOException {
ArrayList<File> fileList = new ArrayList<File>();
File desDir = new File(folderPath);
if (!desDir.exists()) {
desDir.mkdir();
}
ZipFile zf = new ZipFile(zipFile);
for (Enumeration<?> entries = zf.entries(); entries.hasMoreElements();) {
ZipEntry entry = ((ZipEntry)entries.nextElement());
if (entry.getName().contains(nameContains)) {
InputStream in = zf.getInputStream(entry);
String str = folderPath + File.separator + entry.getName();
str = new String(str.getBytes("8859_1"), "GB2312");
// str.getBytes("GB2312"),"8859_1" 输出
// str.getBytes("8859_1"),"GB2312" 输入
File desFile = new File(str);
if (!desFile.exists()) {
File fileParentDir = desFile.getParentFile();
if (!fileParentDir.exists()) {
fileParentDir.mkdirs();
}
desFile.createNewFile();
}
OutputStream out = new FileOutputStream(desFile);
byte buffer[] = new byte[BUFF_SIZE];
int realLength;
while ((realLength = in.read(buffer)) > 0) {
out.write(buffer, 0, realLength);
}
in.close();
out.close();
fileList.add(desFile);
}
}
return fileList;
}
/**
* 获得压缩文件内文件列表
*
* @param zipFile 压缩文件
* @return 压缩文件内文件名称
* @throws ZipException 压缩文件格式有误时抛出
* @throws IOException 当解压缩过程出错时抛出
*/
public static ArrayList<String> getEntriesNames(File zipFile) throws ZipException, IOException {
ArrayList<String> entryNames = new ArrayList<String>();
Enumeration<?> entries = getEntriesEnumeration(zipFile);
while (entries.hasMoreElements()) {
ZipEntry entry = ((ZipEntry)entries.nextElement());
entryNames.add(new String(getEntryName(entry).getBytes("GB2312"), "8859_1"));
}
return entryNames;
}
/**
* 获得压缩文件内压缩文件对象以取得其属性
*
* @param zipFile 压缩文件
* @return 返回一个压缩文件列表
* @throws ZipException 压缩文件格式有误时抛出
* @throws IOException IO操作有误时抛出
*/
public static Enumeration<?> 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("GB2312"), "8859_1");
}
/**
* 取得压缩文件对象的名称
*
* @param entry 压缩文件对象
* @return 压缩文件对象的名称
* @throws UnsupportedEncodingException
*/
public static String getEntryName(ZipEntry entry) throws UnsupportedEncodingException {
return new String(entry.getName().getBytes("GB2312"), "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 ? "" : File.separator)
+ resFile.getName();
rootpath = new String(rootpath.getBytes("8859_1"), "GB2312");
if (resFile.isDirectory()) {
File[] fileList = resFile.listFiles();
for (File file : fileList) {
zipFile(file, zipout, rootpath);
}
} else {
byte buffer[] = new byte[BUFF_SIZE];
BufferedInputStream 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();
}
}
}
转自:http://xxw8393.blog.163.com/blog/static/37256834200992624558643/
[Android开发]zip文件压缩解压缩相关推荐
- zip文件压缩解压缩
//----------------- DirTraversal.java package com.once; import java.io.File; import java.util.Arra ...
- linux+shell+解压命令,Shell命令 文件压缩解压缩之gzip、zip详解
本篇文章的主要内容讲述的是shell命令中之文件压缩解压缩之gzip.zip,具有一定参考价值,感兴趣的朋友可以了解一下,希望对你有所帮助. 1.gzip:gzip压缩工具 gzip命令的功能说明: ...
- 服务器里解压缩gz文件夹,Shell命令文件压缩解压缩之gzip、zip的案例分析
Shell命令文件压缩解压缩之gzip.zip的案例分析 发布时间:2020-11-13 10:32:36 来源:亿速云 阅读:114 作者:小新 小编给大家分享一下Shell命令文件压缩解压缩之gz ...
- ZIP文件压缩与解压缩
ZIP4J解压优点 ZIP4J 是一个支持处理ZIP文件的开源库 支持创建,修改,添加,删除,解压 压缩文件 支持读/写密码保护 支持AES加密 128/256 支持标准ZIP加密 支持进度监视器 自 ...
- 利用Java进行zip文件压缩与解压缩
可能存在的业务情况: 1.用户上传了压缩包,需校验压缩包中的文件是否合格. 2.用户上传压缩包,对压缩包中的文件进行批量水印处理 解决思路: 1.读取原压缩包文件,解压缩至临时目录 2.对临时目录中的 ...
- 7z001怎么解压在安卓手机上面_安卓zip文件压缩RAR解压手机下载-安卓zip文件压缩RAR解压v1.0最新版下载...
安卓zip文件压缩RAR解压是一款非常好用的手机压缩解压缩神器,在安卓zip文件压缩RAR解压上我们可以看到很多的实用的功能,软件可以帮助我们更好的处理我们手机中的文件,感兴趣的朋友赶紧下载安卓zip ...
- cordova 安卓文件多选_安卓zip文件压缩RAR解压软件下载-安卓zip文件压缩RAR解压下载v3.0.4安卓版...
安卓zip文件压缩RAR解压是一款非常好用的手机压缩解压缩神器,在安卓zip文件压缩RAR解压上我们可以看到很多的实用的功能,软件可以帮助我们更好的处理我们手机中的文件,感兴趣的朋友赶紧下载安卓zip ...
- java 操作Zip文件(压缩、解压、加密)
java 操作Zip文件(压缩.解压.加密) 依赖:点击下载 package com.zxl.test;import net.lingala.zip4j.model.ZipParameters; im ...
- 文件压缩解压缩,文件查找
文件压缩解压缩 常见压缩档 > *.zip | zip 程序压缩打包的档案: (很常见,但是因为不包含文档名编码信息,跨平台可能会乱码) > *.rar | rar 程序压缩打包的档案:( ...
最新文章
- linux下的几种隐藏技术
- 各厂商防火墙初始登录IP及密码信息
- python【数据结构与算法】KMP和扩展算法
- HTML_DOM简介
- 044、JVM实战总结:高级工程师的硬核技能:JVM的Young GC日志应该怎么看?
- python虚拟环境中安装diango_django是要在虚拟环境激活后安装吗
- linux 移动压缩包 命令,文件的复制、移动、压缩等对SELinux属性关系详解
- xcopy使用与案例使用
- 1、多线程原理与实践 《Java高并发核心编程 卷二》读书笔记
- html表单站内搜,网站集成百度、Bing必应搜索引擎,在网页中实现站内全文搜索...
- Lumen实例讲解:第二部分
- 案例精选 | 冷饭变盛宴?一文谈尽边缘计算
- 佳博 TSC打印机 TSPL指令开发
- 使用HTML编写浣溪沙,《浣溪沙》版本+原文+拼音版+翻译+赏析+作者
- 打印机无法打印,配置端口显示错误?
- LTSPICE使用教程:参数变量和参数扫描
- SwingUtilities.invokeLater
- 艺术科学,物物皆通 ——观《达·芬奇的人生密码》有感
- 飞算全自动软件工程平台:让天下没有难做的软件工程
- Python--从基础到面向对象全过程笔记