1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > java zip ant 密码_java对 zip文件的压缩和解压(ant解决中文乱码)

java zip ant 密码_java对 zip文件的压缩和解压(ant解决中文乱码)

时间:2018-10-16 22:07:01

相关推荐

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

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。