生成的csv文件16k,然后压缩后解压变成了20k,并且有部分重复。暂时不知道原因,也没报错信息。记录下来说不定哪天就知道了,嘻嘻~
压缩代码如下:
/**
* 将存放在sourceFilePath目录下的源文件,打包成fileName名称的zip文件,并存放到zipFilePath路径下
*
* @param sourceFilePath
* @param zipFilePath
* @return
*/
public static boolean fileToZip(String sourceFilePath, String zipFilePath) throws Exception {
boolean flag = false;
// 读取原文件
File sourceFile = new File(sourceFilePath);
FileInputStream fis = null;
BufferedInputStream bis = null;
FileOutputStream fos = null;
ZipOutputStream zos = null;
if (!sourceFile.exists() || sourceFile.list().length < 1) {
LinkedHashMap<String, Object> logMap = new LinkedHashMap<String, Object>();
logMap.put("sourceFile is not exist", sourceFilePath);
MeUtils.info("sourceFile status", logMap);
} else {
try {
File zipFile = new File(zipFilePath);
if (zipFile.exists()) {
zipFile.delete();
// 如果文件存在,则删除
}
File[] sourceFiles = sourceFile.listFiles();
fos = new FileOutputStream(zipFile);
zos = new ZipOutputStream(new BufferedOutputStream(fos));
// 设置字符流缓冲区
byte[] bufs = new byte[10240];
for (int i = 0; i < sourceFiles.length; i++) {
String fileName = sourceFiles[i].getName();
fileName = new String(fileName.getBytes("GBK"), System.getProperty("sun.jnu.encoding"));
// 创建压缩文件
ZipEntry zipEntry = new ZipEntry(fileName);
zos.putNextEntry(zipEntry);
// 读取待压缩的文件并写进压缩包里
fis = new FileInputStream(sourceFiles[i]);
bis = new BufferedInputStream(fis, 10240);
while (bis.read(bufs) != -1) {
zos.write(bufs);
}
}
flag = true;
} catch (Exception e) {
MeUtils.error(e);
} finally {
// 关闭流
try {
if (bis != null) {
bis.close();
}
if (zos != null) {
zos.close();
}
} catch (Exception e) {
MeUtils.error(e);
}
}
}
return flag;
}
日志: