首先是边读边写到文件夹中,接着压缩。
入参为文件夹路径 比如D盘下的。。。。
反参为压缩过后压缩文件的路径,转成http路径给到前端下载。
/**
* 打包导出查询出的测试log
* @param list
*/
@Override
public String exportOrderLog(List<WorkOrderLogPageVo> list,String ipUrl) {
if (ObjectUtils.isEmpty(list)){
throw new ServiceException("未查询到数据,无法导出");
}
long now = System.currentTimeMillis();
//查询出的数据写到当前时间的文件夹中放到orderlog目录下
list.stream().filter(a -> StringUtils.isNotEmpty(a.getUrl())).forEach(data -> {
String profile = RuoYiConfig.getProfile() + data.getUrl();
File f = new File(profile.replace(Constants.RESOURCE_PREFIX,""));
File targetFile = new File(RuoYiConfig.getProfile() + "/"+orderlog+"/"+now+"/"+data.getLogFileName());
try {
if (!targetFile.getParentFile().exists()) {
targetFile.getParentFile().mkdirs();
}
targetFile.createNewFile();
InputStream in = new FileInputStream(f);
OutputStream out = new FileOutputStream(targetFile);
BufferedOutputStream bos = new BufferedOutputStream(out);
int len = -1;
byte[] buf = new byte[1024];
while ((len = in.read(buf)) != -1) {
bos.write(buf, 0, len);
}
// 关流顺序,先打开的后关闭--必须先关闭再读取
bos.close();
out.close();
in.close();
} catch (Exception e) {
e.printStackTrace();
}
});
//开始压缩此当前时间的文件夹为.zip
compressFileToZip(RuoYiConfig.getProfile() + "/"+ orderlog +"/"+now);
//返回压缩文件路径
return ipUrl + "/profile/" + orderlog + "/" + now + ".zip";
}
/**
* @Title: compressAllFileZip
* @Description: 传递文件路径压缩文件,传递文件夹路径压缩文件夹,注:空的文件夹不会出现在压缩包内
* @param @param compresspath 需要压缩的文件夹的目录
* @return void 返回类型
* @throws
*/
public static boolean compressFileToZip(String compresspath) {
boolean bool = false;
try {
ZipOutputStream zipOutput = null;
File file = new File(compresspath);
if(file.isDirectory()){
zipOutput = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(compresspath + ".zip")));
compressZip(zipOutput, file, ""); //递归压缩文件夹,最后一个参数传""压缩包就不会有当前文件夹;传file.getName(),则有当前文件夹;
}else{
zipOutput = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(compresspath.substring(0, compresspath.lastIndexOf(".")) + ".zip")));
zipOFile(zipOutput, file); //压缩单个文件
}
zipOutput.closeEntry();
zipOutput.close();
bool = true;
} catch (Exception e) {
e.printStackTrace();
}
return bool;
}
/**
* @Title: compressZip
* @Description: 子文件夹中可能还有文件夹,进行递归
* @param @param zipOutput
* @param @param file
* @param @param suffixpath
* @param @throws IOException
* @return void 返回类型
* @throws
*/
private static void compressZip(ZipOutputStream zipOutput, File file, String suffixpath) {
File[] listFiles = file.listFiles();// 列出所有的文件
for(File fi : listFiles){
if(fi.isDirectory()){
if(suffixpath.equals("")){
compressZip(zipOutput, fi, fi.getName());
}else{
compressZip(zipOutput, fi, suffixpath + File.separator + fi.getName());
}
}else{
zip(zipOutput, fi, suffixpath);
}
}
}
/**
* @Title: zip
* @Description: 压缩的具体操作
* @param @param zipOutput
* @param @param file 文件
* @param @param suffixpath 文件夹拼接路径
* @return void 返回类型
* @throws
*/
public static void zip(ZipOutputStream zipOutput, File file, String suffixpath) {
try {
ZipEntry zEntry = null;
if(suffixpath.equals("")){
zEntry = new ZipEntry(file.getName());
}else{
zEntry = new ZipEntry(suffixpath + File.separator + file.getName());
}
zipOutput.putNextEntry(zEntry);
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
byte[] buffer = new byte[1024];
int read = 0;
while((read = bis.read(buffer)) != -1){
zipOutput.write(buffer, 0, read);
}
bis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @Title: zip
* @Description: 压缩单个文件
* @param @param zipOutput
* @param @param file 文件
* @return void 返回类型
* @throws
*/
public static void zipOFile(ZipOutputStream zipOutput, File file) {
try {
ZipEntry zEntry = new ZipEntry(file.getName());
zipOutput.putNextEntry(zEntry);
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
byte[] buffer = new byte[1024];
int read = 0;
while((read = bis.read(buffer)) != -1){
zipOutput.write(buffer, 0, read);
}
bis.close();
} catch (Exception e) {
e.printStackTrace();
}
}