java zip ant 密码_java对 zip文件的压缩和解压(ant解决中文乱码)
/***@name 解压zip格式压缩包
*@description 相关说明
*@paramsourceZip 源文件
*@paramdestDir 目标文件地址
*@throwsException
*@authorLJCH
*@history 修订历史(历次修订内容、修订人、修订时间等)*/@SuppressWarnings("unchecked")private static void unzip(String sourceZip, String destDir) throwsException {
ZipFile zipFile= null;try{final File f = newFile(sourceZip);if ((!f.exists()) && (f.length() <= 0)) {throw new RuntimeException("要解压的文件不存在!");
}//一定要加上编码,之前解压另外一个文件,没有加上编码导致不能解压
zipFile = new ZipFile(f, "gbk");
String gbkPath;
String strtemp;final Enumeration e =zipFile.getEntries();if (!e.hasMoreElements()) {final File dir = new File(destDir + File.separator + f.getName().substring(0, f.getName().lastIndexOf(".")));if (!dir.exists()) {
dir.mkdirs();
}return;
}while(e.hasMoreElements()) {final org.apache.tools.zip.ZipEntry zipEnt =e.nextElement();
gbkPath=zipEnt.getName();
strtemp= destDir + File.separator +gbkPath;if (zipEnt.isDirectory()) { //目录
final File dir = newFile(strtemp);if (!dir.exists()) {
dir.mkdirs();
}continue;
}else if (zipEnt.getName().substring(zipEnt.getName().length() - 1, zipEnt.getName().length()).equals(File.separator)) {final File dir = newFile(strtemp);if (!dir.exists()) {
dir.mkdirs();
}continue;
}else{//读写文件
final InputStream is =zipFile.getInputStream(zipEnt);final BufferedInputStream bis = newBufferedInputStream(is);//建目录
final String strsubdir =gbkPath;for (int i = 0; i < strsubdir.length(); i++) {if (strsubdir.substring(i, i + 1).equalsIgnoreCase("/")) {final String temp = destDir + File.separator + strsubdir.substring(0, i);final File subdir = newFile(temp);if (!subdir.exists()) {
subdir.mkdir();
}
}
}final FileOutputStream fos = newFileOutputStream(strtemp);final BufferedOutputStream bos = newBufferedOutputStream(fos);intlen;final byte[] buff = new byte[1024];while ((len = bis.read(buff)) != -1) {
bos.write(buff,0, len);
}
bos.close();
fos.close();
}
}
}catch(Exception e) {//logger.error("解压文件出现异常:", e);
throwe;
}finally{
zipFile.close();
}
}/***@name 解压文件用于后期扩展RAR等其他压缩格式
*@description 相关说明
*@paramsourceFile 源文件
*@paramdestDir 目标路径
*@throwsException
*@authorLJCH
*@history 修订历史(历次修订内容、修订人、修订时间等)*/
private static void unfile(String sourceFile, String destDir) throwsException {//根据类型,进行相应的解压缩
final String type = sourceFile.substring(sourceFile.lastIndexOf(".") + 1);final File dir = newFile(destDir);if(!dir.exists()){
dir.mkdirs();
}if (type.toLowerCase().equals("zip")) {
unzip(sourceFile, destDir);
}else{throw new RuntimeException("只支持zip格式的压缩包!"+type);
}
}/***@name 解压到指定目录
*@description 相关说明
*@paramsourceFile 源文件
*@paramdestDir 目标目录
*@throwsException
*@authorLJCH
*@history 修订历史(历次修订内容、修订人、修订时间等)*/
public static void deCompress(String sourceFile, String destDir) throwsException {if (sourceFile == null || destDir == null) {throw new RuntimeException("目录不能为空");
}//保证文件夹路径最后是"/"或者"\"
final char lastChar = destDir.charAt(destDir.length() - 1);if (lastChar != '/' && lastChar != '\\') {
unfile(sourceFile, destDir+File.separator);
}else{
unfile(sourceFile, destDir);
}
}/***@name 解压到指定目录
*@description 相关说明
*@paramsourceFile 源文件
*@paramdestDir 目标路径
*@throwsException
*@authorLJCH
*@history 修订历史(历次修订内容、修订人、修订时间等)*/
public static void deCompress(File sourceFile, String destDir) throwsException {if (!sourceFile.exists() ||sourceFile.isDirectory()) {throw new RuntimeException("文件不存在");
}
deCompress(sourceFile.getPath(), destDir);
}/***@name 解压到当前目录
*@description 相关说明
*@paramsourceFile 源文件
*@throwsException
*@authorLJCH
*@history 修订历史(历次修订内容、修订人、修订时间等)*/
public static void deCompress(String sourceFile) throwsException {//获得文件目录
int i = sourceFile.lastIndexOf("/");final int d = sourceFile.lastIndexOf("\\");if (i == -1 && d == -1) {throw new RuntimeException("目录separator异常");
}else if (i == -1) {
i=d;
}final String destDir = sourceFile.substring(0, i + 1);
unfile(sourceFile, destDir);
}/***@name 解压到当前目录
*@description 相关说明
*@paramsourceFile 源文件
*@throwsException
*@authorLJCH
*@history 修订历史(历次修订内容、修订人、修订时间等)*/
public static void deCompress(File sourceFile) throwsException {if (!sourceFile.exists() ||sourceFile.isDirectory()) {throw new RuntimeException("文件不存在");
}
deCompress(sourceFile.getPath());
}
java zip ant 密码_java对 zip文件的压缩和解压(ant解决中文乱码)相关推荐
- linux带密码解压密码,linux 下文件加密压缩和解压的方法
方法一:用tar命令 对文件加密压缩和解压 压缩: [html] view plain copy tar -zcf - filename |openssl des3 -salt -k password ...
- linux 下文件加密压缩和解压的方法
方法一:用tar命令 对文件加密压缩和解压 压缩: tar -zcf - filename |openssl des3 -salt -k password | dd of=filename.des3 ...
- c#程序操作文件进行压缩和解压
前记录:首先本人项目设计需要将文件进行压缩和解压的操作,语言为c#,从网上找的很多都是半成品,不足而论,并且坑多.一度以为是我的电脑出现了问题,差点把自己的电脑给用win10 重装系统.后来想了一下, ...
- 【Linux】文件的压缩和解压
欢迎来到博主 Apeiron 的博客,祝您旅程愉快 ! 时止则止,时行则行.动静不失其时,其道光明. 目录 1.压缩格式 2.压缩软件 3.tar 命令简介 4.tar 命令压缩 5.总结 1.压 ...
- linux压缩100天以上的文件,如何提高几十甚至上百G文件的压缩和解压效率
如何提高几十甚至上百G文件的压缩和解压效率 (2012-06-05 02:21:35) 标签: 的 如何 杂谈 如何提高几十甚至上百G文件的压缩和解压效率需求: 有几十.上百G的文件需要压缩.解压.前 ...
- Linux下文件的压缩和解压
文件的压缩和解压 Gzip格式的: tar -czvf 要存的名字.tar.gz 要打包的东西或目录 bzip2格式的: tar -cjvf 要存的名字.tar.bz ...
- ZIP文件流压缩和解压
前面写了一篇文章 "ZIP文件压缩和解压", 介绍了" SharpZipLib.Zip " 的使用, 最近的项目中,在使用的过程中, 遇到一些问题. 比如, 现 ...
- 文件的压缩和解压等操作
.1.将文件压缩为zip/rar文件(实质都为zip格式) /*** 创建ZIP文件* @param sourcePath 文件或文件夹路径* @param zipPath 生成的zip文件存在路径( ...
- 哈夫曼编码实现文件的压缩和解压
哈夫曼编码的概念 哈夫曼编码是基于哈夫曼树实现的一种文件压缩方式. 哈夫曼树:一种带权路径最短的最优二叉树,每个叶子结点都有它的权值,离根节点越近,权值越小(根节点权值为0,往下随深度增加依次加一), ...
最新文章
- 第九届国际冬季脑机接口会议(BCI2021)
- 【Python】手把手教你用Python做一个图像融合demo,小白可上手!
- 不是mysql常用类型的是什么_下列选项中不是MySQL中常用数据类型的是()
- C语言二叉树曲折级顺序遍历(附完整源码)
- python电脑推荐_6款Python必备的可视化工具推荐
- 第2小节,深入剖析gym环境构建
- C# - 接口,继承
- PMP杂谈--PMP中一些easy忽视的地方
- php和java环境整合
- 屏保程序学中玩(下)
- poi 读取word内容转html在线预览
- 利用ENVI深度学习进行遥感变化监测
- Adobe Illustrator 中的路径查找器
- 【锂知道】锂电池基本原理解析:充电及放电机制
- 设计模式解密(17)- 备忘录模式
- 读书笔记: 与爱因斯坦月球漫步-记忆术
- Arcgis中的坐标和投影
- 识别 Cisco交换机型号
- 给初入测试/开发程序员的几点建议,把困难当做猎物......
- Unity2019新建空项目都报错(有两个看不到的红色报错)怎么解决?