代码下载优化

This commit is contained in:
20932067@zju.edu.cn 2021-02-28 21:20:17 +08:00
parent e26b9feb0a
commit 290036d38c
1 changed files with 99 additions and 50 deletions

View File

@ -1,10 +1,11 @@
package com.hchyun.generator.controller;
import java.io.IOException;
import java.io.Serializable;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.servlet.http.HttpServletResponse;
import com.hchyun.generator.aop.Log;
@ -165,9 +166,11 @@ public class GenController extends GenBaseController {
@GetMapping("/download/{tableName}")
public void download(HttpServletResponse response, @PathVariable("tableName") String tableName) throws IOException {
byte[] data = genTableService.downloadCode(tableName);
genCode(response, data);
}
/**
* 生成代码自定义路径
*/
@ -204,6 +207,52 @@ public class GenController extends GenBaseController {
String[] tableNames = Convert.toStrArray(tables);
byte[] data = genTableService.downloadCode(tableNames);
genCode(response, data);
compress(data);
}
/**
* 将bytes流写入到本地
* @param bytes
*/
public static void compress(byte[] bytes) {
try {
FileOutputStream fileOutputStream = new FileOutputStream("F:\\Work\\表白\\huhyun.zip");
fileOutputStream.write(bytes);
fileOutputStream.flush();
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 压缩到文件
*
* @param sourceFileNames 源文件列表
* @param zipFileName 目标压缩文件
*/
public static void compress(List<String> sourceFileNames, String zipFileName) {
byte[] buf = new byte[1024];
File zipFile = new File(zipFileName);
try {
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile));
for (int i = 0; i < sourceFileNames.size(); i++) {
File sourceFile = new File(sourceFileNames.get(i));
FileInputStream fis = new FileInputStream(sourceFile);
zos.putNextEntry(new ZipEntry(sourceFile.getName()));
// logge.info("压缩当前文件:{}",sourceFile.getName());
int len;
while ((len = fis.read(buf)) > 0) {
zos.write(buf, 0, len);
}
zos.closeEntry();
fis.close();
}
zos.close();
System.out.println("压缩完成");
} catch (Exception e) {
// log.error("压缩文件异常:",e);
}
}
/**