源于下载数据量特别大,需要将下载文本打包到服务。jdk自带打包无法满足。使用如下方法能完美解决。
* 服务器打包*
/**
* 使用apache工具打包完美解决大于4G问题
*/
public void apacheZip() {
String fgf = ConstKey.separator;
logger.info("EAST--打包开始");
try{
String uploadFilePath = ConstKey.initSysMap.get(SysInit.offlinedown.name())+fgf+suitCode+fgf+fileNameDate+fgf;
System.out.println("EAST打包路径:"+uploadFilePath);
String zipPath = ConstKey.initSysMap.get(SysInit.offlinedown.name())+fgf+suitCode+fgf+fileNameDate+"_zip"+fgf;
File file = new File(zipPath);
if(!file .exists() && !file .isDirectory())
file .mkdirs();
ZipUtils.compressFilesZip(uploadFilePath,zipPath+fileNameDate+".zip");
}catch(Exception e){
logger.error("EAST打包异常:"+e.getMessage());
e.printStackTrace();
}
logger.info("EAST--打包结束");
}
**打包方法**
/**
* 使用apache旗下的commons-compress
* @param pathname
* @param zipUrl
* @return
* @throws Exception
*/
public static File compressFilesZip(String pathname, String zipUrl) throws Exception {
File srcFile = new File(pathname);
File[] files = srcFile.listFiles();
File zipFile = new File(zipUrl);
if (!zipFile.exists()){
zipFile.createNewFile();
}
ZipArchiveOutputStream zaos = null;
try {
zaos = new ZipArchiveOutputStream(zipFile);
zaos.setUseZip64(Zip64Mode.AsNeeded);
zaos.setEncoding("gbk"); // 解决BCSLinux环境 zip压缩包中的文件中文乱码 LYS
//将每个文件用ZipArchiveEntry封装
//再用ZipArchiveOutputStream写到压缩文件中
for(File file : files) {
if(file != null) {
ZipArchiveEntry zipArchiveEntry = new ZipArchiveEntry(file,file.getName());
zaos.putArchiveEntry(zipArchiveEntry);
if(file.isDirectory()){
continue;
}
InputStream is = null;
try {
is = new BufferedInputStream(new FileInputStream(file));
byte[] buffer = new byte[1024 ];
int len = -1;
while((len = is.read(buffer)) != -1) {
//把缓冲区的字节写入到ZipArchiveEntry
zaos.write(buffer, 0, len);
}
zaos.closeArchiveEntry();
}catch(Exception e) {
throw new RuntimeException(e);
}finally {
if(is != null)
is.close();
}
}
}
zaos.finish();
}
finally {
if(zaos != null) {
zaos.close();
}
}
return zipFile;
}