用java.util.zip实现文件压缩
// 要压缩的文件——可以有多个
File file = new File("d:/111.csv");
// 一个压缩的文件对应一个ZipEntry
ZipEntry zip = new ZipEntry(file.getName());
// 定义zip输出流
ZipOutputStream zos = null ;
FileInputStream fis = null ;
try {
zos = new ZipOutputStream(new FileOutputStream(
"e:/zipname.zip"));
// 把zipEntry放到zip输出流里
zos.putNextEntry(zip);
fis = new FileInputStream(file);
byte[] b;
b = new byte[fis.available()];
fis.read(b);
zip.setSize(b.length);
zip.setTime(new java.util.Date().getTime());
zos.write(b);
zos.write(b);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally
{
try {
zos.closeEntry();
zos.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}