java 压缩文件夹有几率压缩失败,打开显示不可预料的末端且用其他专业软件解压文件没压缩完全,求解
代码如下:
/**
* 批量压缩文件(夹)
*
* @param resFileList 要压缩的文件(夹)列表
* @param zipFile 生成的压缩文件
* @param comment 压缩文件的注释
* @throws IOException 当压缩过程出错时抛出
public static boolean zipFiles(Collection<File> resFileList, File zipFile, String comment) {
boolean bool = false;
BufferedOutputStream fo = null;
ZipOutputStream zipout = null;
try {
fo = new BufferedOutputStream(new FileOutputStream(zipFile), BUFF_SIZE);
zipout = new ZipOutputStream(fo);
int totalSize = resFileList.size();
int count = 0;
for (File resFile : resFileList) {
zipFile(resFile, zipout, "");
++count;
}
bool = true;
} catch (Exception e) {
e.printStackTrace();
bool = false;
} finally {
if (zipout != null) {
try {
zipout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fo != null) {
try {
fo.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return bool;
}
/**
* 压缩文件
*
* @param resFile 需要压缩的文件(夹)
* @param zipout 压缩的目的文件
* @param rootpath 压缩的文件路径
* @throws FileNotFoundException 找不到文件时抛出
* @throws IOException 当压缩过程出错时抛出
*/
private static void zipFile(File resFile, ZipOutputStream zipout, String rootpath) throws FileNotFoundException, IOException {
rootpath = rootpath + (rootpath.trim().length() == 0 ? "" : File.separator) + resFile.getName();
// rootpath = new String(rootpath.getBytes("8859_1"), "GB2312");
if (resFile.isDirectory()) {
File[] fileList = resFile.listFiles();
for (File file : fileList) {
zipFile(file, zipout, rootpath);
}
} else {
if (resFile.length() == 0) {
return;
}
byte buffer[] = new byte[BUFF_SIZE];
BufferedInputStream in = new BufferedInputStream(new FileInputStream(resFile), BUFF_SIZE);
zipout.setMethod(ZipOutputStream.DEFLATED);
zipout.setLevel(Deflater.BEST_COMPRESSION);
zipout.putNextEntry(new ZipEntry(rootpath));
int realLength;
while ((realLength = in.read(buffer)) != -1) {
zipout.write(buffer, 0, realLength);
}
zipout.flush();
in.close();
zipout.closeEntry();
}
}
}