`
vbtboy
  • 浏览: 46859 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

用ant来解压文件

阅读更多
最近在做一个项目要用到java来解压zip文件,但是java的api解压时碰到中文文件就比较脆弱了,
所以可以用ant的api来解压。下面是简单的java代码

ZipFile zipFile = new ZipFile("d:\\a.zip");
String strPath, gbkPath, strtemp;
strPath = tempFile.getAbsolutePath();
java.util.Enumeration e = zipFile.getEntries();
while (e.hasMoreElements()) {
ZipEntry zipEnt = (ZipEntry) e.nextElement();
gbkPath = zipEnt.getName();
if (zipEnt.isDirectory()) {
strtemp = strPath + File.separator + gbkPath;
File dir = new File(strtemp);
dir.mkdirs();
continue;
} else {
// 读写文件
InputStream is = zipFile.getInputStream(zipEnt);
BufferedInputStream bis = new BufferedInputStream(is);
gbkPath = zipEnt.getName();
strtemp = strPath + File.separator + gbkPath;

// 建目录

new File(strtemp.substring(0, strtemp
.lastIndexOf(File.separator) > strtemp
.lastIndexOf("/") ? strtemp
.lastIndexOf(File.separator) : strtemp
.lastIndexOf("/"))).mkdirs();

FileOutputStream fos = new FileOutputStream(strtemp);
BufferedOutputStream bos = new BufferedOutputStream(fos);
int c;
while ((c = bis.read()) != -1) {
bos.write(c);
}
bos.flush();
bos.close();
fos.close();
}
}
zipFile.close();

ps:最后在部署到weblogic8.1.5时竟然不能用,看了一下原来weblogic的环境中已经包含了一个低版本的ant.jar,
故须在weblogic启动的环境变量中增加新的ant.jar.
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics