java压缩文件

/**
* 压缩文件
*
* @method zip_file
* @param file_path 需压缩的文件路径
*/
private void zip_file(String file_path){
File file = new File(file_path);
FileInputStream fileInputStream = null;
BufferedInputStream bufferedInputStream = null;
ZipOutputStream zipOutputStream = null;
try {
fileInputStream = new FileInputStream(file);
bufferedInputStream = new BufferedInputStream(fileInputStream);
byte[] buf = new byte[1024];
int len;
FileOutputStream fileOutputStream = new FileOutputStream(GlobalVar.serverConfig.getProperty("vasserver.adcftp.srcdir")+file.getName()+".ZIP");
BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
zipOutputStream = new ZipOutputStream(bos);//压缩包
ZipEntry zipEntry = new ZipEntry(file.getName());//压缩包里的文件名称
zipOutputStream.putNextEntry(zipEntry);//写入新的 ZIP 文件条目并将流定位到条目数据的开始处
while(-1 != (len=bufferedInputStream.read(buf))){
zipOutputStream.write(buf, 0, len);
zipOutputStream.flush();
}

} catch (FileNotFoundException e) {
log.error("未找到需压缩的文件!-->" + e.getMessage());
e.printStackTrace();
System.out.println("未找到需压缩的文件!-->" + e.getMessage());
} catch (IOException e) {
log.error("文件压缩异常!-->" + e.getMessage());
e.printStackTrace();
System.out.println("文件压缩异常!-->" + e.getMessage());
} finally {
try {
if(null != zipOutputStream)
zipOutputStream.close();
if(null != bufferedInputStream)
bufferedInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}

}
}



引自:[url]http://www.blogjava.net/mrcmd/archive/2007/08/24/138963.html[/url]

参阅(Java中解压缩文件 ZIP):[url]http://cuisuqiang.iteye.com/blog/1757334[/url]

你可能感兴趣的:(Java)