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());

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值