java 压缩文件代码

ZipOutputStream out = null;
FileInputStream in = null;
int b;
try {
out = new ZipOutputStream(new FileOutputStream(zipName + ".zip"));
out.putNextEntry(new ZipEntry(sourceFileName));
in = new FileInputStream(souceFile);
byte[] by = new byte[1024];
while ((b = in.read(by)) != -1){
out.write(by, 0, b);
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
log.error("压缩文件失败!", e1);
} catch (IOException e) {
e.printStackTrace();
log.error("压缩文件失败!", e);
} finally{
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
log.error("关闭流失败!", e);
}
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
log.error("关闭流失败!", e);
}
}

使用apache的ant包可以解决中国问题。 附件中的ant替换tomcat的ant.jar即可

猜你喜欢

转载自xiyuliuguang.iteye.com/blog/1855037