new ZipOutputStream(new FileOutputStream("c:/abc.txt"));
在操作文件或者流的时候最好不要这么写,因为这么写你无法在finally里面将流最终关闭,所以当您要删除文件的时候就会有IOException,最终导致文件无法删除!
public String compressionFiles() {
ZipOutputStream zosm = null;
FileOutputStream fosm = null;
try {
fosm = new FileOutputStream("c:/abc.txt");
zosm = new ZipOutputStream(fosm);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (zosm != null) {
zosm.close();
}
if (fosm != null) {
fosm.close();
}
}
}
这样分开出来写,就可以保证所有的流最后都可以在finally中被正确关闭。