1、解压
ZipFile zipFile = null;
try {
// 支持中文
Charset charset = Charset.forName("GBK");
zipFile = new ZipFile(file, charset);
createDirectory(outputDir, null);// 创建输出目录
@SuppressWarnings("rawtypes")
Enumeration enums = zipFile.entries();
while (enums.hasMoreElements()) {
ZipEntry entry = (ZipEntry) enums.nextElement();
if (entry.isDirectory()) {
// 是目录
createDirectory(outputDir, entry.getName());// 创建空目录
} else {
// 是文件
File tmpFile = new File(outputDir + "/" + entry.getName());
createDirectory(tmpFile.getParent() + "/", null);// 创建输出目录
InputStream in = null;
OutputStream out = null;
try {
in = zipFile.getInputStream(entry);
try{
out = new FileOutputStream(tmpFile);
int length = 0;
byte[] b = new byte[2048];
while ((length = in.read(b)) != -1) {
out.write(b, 0, length);
}
}finally{
IOUtils.closeQuietly(out);
}
} finally {
IOUtils.closeQuietly(in);
}
}
}
} catch (Exception e) {
logger.error("解压缩文件出现异常", e);
throw new IOException("解压缩文件出现异常", e);
} finally {
try {
if (zipFile != null) {
zipFile.close();
}
} catch (Exception ex) {
logger.error("关闭zipFile出现异常", ex);
//throw new IOException("关闭zipFile出现异常", ex);//commented for sonar
}
}
2、压缩并response出去
zos = new ZipOutputStream(response.getOutputStream());
for(String promptName : promptNameArr){
PromptFile prompt= promptFileSV.sysGetFile(promptName);
if (prompt == null) {
logger.warn("没有文件" + promptName+",可能被删除");
continue;
}
File file = promptController.auditionFileFind(promptName);
if(!file.exists()) {
logger.warn("没有文件" + promptName+",可能被删除");
continue;
}
wavFiles.add(file);
zos.putNextEntry(new ZipEntry(promptName+".wav"));
fis = new FileInputStream(file);
try
{
byte b[] = new byte[1024];
int n = 0;
while((n = fis.read(b)) != -1){
zos.write(b, 0, n);
}
}finally {
zos.flush();
IOUtils.closeQuietly(fis);
}
}
zos.flush();