package cn.org.jshuwei.j2ee.util;
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 org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;
/**
* 文件压缩解压工具类
*
* @author huwei(jshuwei.org.cn)
* @since 1.2
*
*/
public class ZipUtil {
/**
* 压缩ZIP文件,将baseDirName目录下的所有文件压缩到targetFileName.ZIP文件中
*
* @param baseDirName
* 需要压缩的文件的跟目录
* @param targetFileName
* 压缩有生成的文件名
*/
public static void zipFile(String baseDirName, String targetFileName) {
if (baseDirName == null) {
return;
}
File file = new File(baseDirName);
if (!file.exists()) {
return;
}
String baseDirPath = file.getAbsolutePath();
File targetFile = new File(targetFileName);
try {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
targetFile));
if (file.isFile()) {
fileToZip(baseDirPath, file, out);
} else {
dirToZip(baseDirPath, file, out);
}
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 解压缩ZIP文件,将ZIP文件里的内容解压到targetBaseDirName目录下
*
* @param zipFileName
* 待解压缩的ZIP文件名
* @param targetBaseDirName
* 目标目录
*/
@SuppressWarnings("unchecked")
public static void unzipFile(String zipFileName, String targetBaseDirName) {
if (!targetBaseDirName.endsWith(File.separator)) {
targetBaseDirName += File.separator;
}
try {
ZipFile zipFile = new ZipFile(zipFileName);
ZipEntry entry = null;
String entryName = null;
String targetFileName = null;
byte[] buffer = new byte[4096];
int bytes_read;
Enumeration entrys = zipFile.getEntries();//.entries();
while (entrys.hasMoreElements()) {
entry = (ZipEntry) entrys.nextElement();
entryName = entry.getName();
targetFileName = targetBaseDirName + entryName;
if (entry.isDirectory()) {
new File(targetFileName).mkdirs();
continue;
} else {
new File(targetFileName).getParentFile().mkdirs();
}
File targetFile = new File(targetFileName);
FileOutputStream os = new FileOutputStream(targetFile);
InputStream is = zipFile.getInputStream(entry);
while ((bytes_read = is.read(buffer)) != -1) {
os.write(buffer, 0, bytes_read);
}
os.close();
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static void dirToZip(String baseDirPath, File dir,
ZipOutputStream out) {
if (dir.isDirectory()) {
File[] files = dir.listFiles();
if (files.length == 0) {
ZipEntry entry = new ZipEntry(getEntryName(baseDirPath, dir));
try {
out.putNextEntry(entry);
out.closeEntry();
} catch (IOException e) {
e.printStackTrace();
}
return;
}
for (int i = 0; i < files.length; i++) {
if (files[i].isFile()) {
fileToZip(baseDirPath, files[i], out);
} else {
dirToZip(baseDirPath, files[i], out);
}
}
}
}
private static void fileToZip(String baseDirPath, File file,
ZipOutputStream out) {
FileInputStream in = null;
ZipEntry entry = null;
byte[] buffer = new byte[4096];
int bytes_read;
if (file.isFile()) {
try {
in = new FileInputStream(file);
entry = new ZipEntry(getEntryName(baseDirPath, file));
out.putNextEntry(entry);
while ((bytes_read = in.read(buffer)) != -1) {
out.write(buffer, 0, bytes_read);
}
out.closeEntry();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static String getEntryName(String baseDirPath, File file) {
if (!baseDirPath.endsWith(File.separator)) {
baseDirPath += File.separator;
}
String filePath = file.getAbsolutePath();
if (file.isDirectory()) {
filePath += "/";
}
int index = filePath.indexOf(baseDirPath);
return filePath.substring(index + baseDirPath.length());
}
}
分享到:
相关推荐
J2EE常用工具类汇总 J2EE常用工具类—Value Object工具类 ...J2EE常用工具类—文件打包解包处理类 J2EE常用工具类——文件操作类 J2EE常用工具类——邮件处理类(javaMail发送邮件) J2EE常用工具类——字符串工具类
通用文件打包解包工具 rdb等打包解包工具,
一个简单的文件打包&解包程序,方便使用和学习。
etc解包打包工具
RDB打包解包工具RDB打包解包工具RDB打包解包工具RDB打包解包工具RDB打包解包工具RDB打包解包工具RDB打包解包工具RDB打包解包工具RDB打包解包工具RDB打包解包工具RDB打包解包工具RDB打包解包工具RDB打包解包工具RDB...
RDB打包解包工具 打包解包RDB的工具
安卓机型一键解包打包工具 做rom的工具 解包 打包系统 解包rec 解包boot 打包rec 打包boot 解包系统 解包其他分区img等 对应到解包打包选项较多 资源自测较好 建议win7以上机型使用 资源较大。建议留有空间解压
Rdb打包解包工具支持QQ2009及以上版本的解包和重新创建、支持QQ影音皮肤文件的解包和支持结构化文档的解包和打包操作等。Rdb打包解包工具可以自动检测文件格式,用户只需拖动任何文件到文本框中,程序会自动检测文件...
APK打包解包工具,这个是手机版本,完全可以完美打包以安装apk
用于img格式镜像文件解包和打包的好工具!
通用文件打包解包助手.exe_文件修改制作通用RDB文件打包解包助手
sis,sisx文件打包解包工具(中文版)
GDF文件的的解包打包,专用工具 ,可用于多种游戏修改
PKG文件解包工具,能快速解包PKG文件,只需要输入简单命令,导出和导入文件目录即可。包含工具和教程。
2.本工具只能解包/打包RDB文件,文件结构以QQ2009SP3的文件为基准.如果软件提示版本特征不对时,可能会发生错误. 3.本工具无法解压 *.gmd 加密文件 1、首先你要准备photoshop软件 和RDB打包解包工具(下载见附件)...
网上现在能搜索到的system.new.dat文件打包解包工具均仅支持6.0及以下ROM 经过我不懈的努力搜索,终于找到一款7.X能用的工具 懂的人自然知道我在说什么 先看看有没有人需要这个工具 需要的话我再发上来 找这个工具的...
用于打包彩虹岛SPF文件或解包,纯源码,可以直接运行~~~~~~~~~~~~~
rom 一键解包 打包 做第三方rom工具 完美版CM 资源较大。功能较多。 工具支持; 解包打包boot和recovery 解包打包ODM格式任意 解包打包system格式 解包打包bat后缀格式 解包打包br后缀格式 解包打包odm。img 解包打包...
通用文件打包解包操作 好用 下了 要给评论的呀
rk29打包解包工具ultra2.2\RK2918解包打包教程.doc VCDS ESCWRE