问题产生的原因:可能是用到文件流未正确关闭
解决办法是:1.检查待压缩文件的流是否都正常关闭,且按顺序
2.生成压缩文件的过程中用到的流是否正常关闭,且按顺序
try {
File zipFile = new File(fileName);
fileName = zipFile.getName();
if(!zipFile.exists())
zipFile.createNewFile();
FileOutputStream f = new FileOutputStream(zipFile);
/**
* 作用是为任何OutputStream产生校验和
* 第一个参数是制定产生校验和的输出流,第二个参数是指定Checksum的类型 (Adler32(较快)和CRC32两种)
*/
CheckedOutputStream csum = new CheckedOutputStream(f, new Adler32());
// // 用于将数据压缩成Zip文件格式
ZipOutputStream zos = new ZipOutputStream(csum);
List<String> filePaths = compressDTO.getFilePaths();
for (int i = 0; i < filePaths.size(); i++) {
String path = filePaths.get(i);
/* 获取Object,返回结果为OSSObject对象 */
OSSObject ossObject = client.getObject(config.getAliyunBucketName(), path);
// 读去Object内容 返回
InputStream inputStream = ossObject.getObjectContent();
// 对于每一个要被存放到压缩包的文件,都必须调用ZipOutputStream对象的putNextEntry()方法,确保压缩包里面文件不同名
// zos.putNextEntry(new ZipEntry(ossfile.getKey().split("/")[2]));
String substring = path.substring(path.lastIndexOf("/")+1);
String replace = substring.replace("_", "/");
zos.putNextEntry(new ZipEntry(replace + "/" + substring));
int bytesRead = 0;
// 向压缩文件中输出数据
while ((bytesRead = inputStream.read()) != -1) {
zos.write(bytesRead);
}
inputStream.close();
}
zos.close();
client.shutdown();
} catch (Exception e) {
log.info("压缩文件报错:" + e.getMessage());
} finally {
}