/**
* @param srcPathFile 源文件路径(带文件名)
* @param destPaht 目标文件路径(不带文件名)
* @param zipName 目标文件名
* @return
* @throws Exception
*/
public static String zipectract(String srcPathFile, String destPaht, String zipName)
throws Exception {
FileOutputStream outputStream = null;
ZipOutputStream zipOutputStream = null;
FileInputStream inputStream = null;
try {
File file = new File(srcPathFile);
outputStream = new FileOutputStream(destPaht+File.separatorChar+zipName);
zipOutputStream =new ZipOutputStream(outputStream, Charset.forName("utf-8"));
zipOutputStream.setMethod(ZipOutputStream.DEFLATED);
zipOutputStream.putNextEntry(new ZipEntry(file.getName()));
inputStream = new FileInputStream(file);
byte[] b = new byte[1024];
int len = -1;
while ( ( len=inputStream.read(b,0,b.length)) != -1) {
zipOutputStream.write(b,0,len);
}
zipOutputStream.flush();
System.out.println(" @@@ 压缩文件生成:"+destPaht+File.separatorChar+zipName);
} catch (Exception e) {
e.printStackTrace();
} finally {
/**
* 关闭顺序很重要,先打开后关。
*/
if (inputStream!=null) {
inputStream.close();
}
if (zipOutputStream!=null) {
zipOutputStream.closeEntry();
zipOutputStream.close();
}
if (outputStream!=null) {
outputStream.close();
}
}
return zipName;
}