多文件压缩zip导出到浏览器页面
在多个文件导出的时候,我们常常会将多文件压缩之后导出zip文件到页面。下面直接上代码。
@ApiOperation(value = "导出模板文件")
@GetMapping("exportTemplate")
public void exportTemplate(HttpServletRequest request, HttpServletResponse response) {
try {
// 设置文件编码及相应消息头,防止中文乱码
String userAgent = request.getHeader("user-agent").toLowerCase();
String exportFileName = "科研课题导入模板";
if (userAgent.contains("msie") || userAgent.contains("like gecko"))
{
// win10 ie edge 浏览器 和其他系统的ie
exportFileName = URLEncoder.encode(exportFileName, "UTF-8");
} else
{
exportFileName = new String(exportFileName.getBytes("UTF-8"), "ISO-8859-1");
}
response.setCharacterEncoding("UTF-8");
response.setContentType("multipart/form-data");
response.setHeader("content-disposition", "attachment;filename=" + exportFileName + ".zip");
获取系统文件
List<File> srcFiles = new LinkedList<File>();
File file = new File( "e:/a.xlsx");
File fileGc = new File( "e:/课题A_过程.docx");
File fileJt = new File("e:/课题A_结题.docx");
File fileKt = new File("e:/课题A_开题.docx");
srcFiles.add(file);
srcFiles.add(fileGc);
srcFiles.add(fileJt);
srcFiles.add(fileKt);
ZipOutputStream out = new ZipOutputStream(response.getOutputStream());
try {
for (int i = 0; i < srcFiles.size(); i++)
{
File srcFile = srcFiles.get(i);
byte[] buf = new byte[1024];
out.putNextEntry(new ZipEntry(srcFile.getName()));
int len;
FileInputStream in = new FileInputStream(srcFiles.get(i));
while ((len = in.read(buf)) != -1) {
out.write(buf, 0, len);
out.flush();
}
in.close();
out.closeEntry();
}
out.close();
} catch (Exception e) {
throw new RuntimeException("zipFile error from ZipUtils", e);
}finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
System.out.println("ZipUtil toZip close exception" + e);
}
}
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
读取系统zip文件(解压)
在一次需要导入多个文件的时候,往往我们会将多文件压缩为一个zip文件,然后在后台再解压zip文件,读取文件内容,下面我将java读取zip文件的方法代码贴上,最近项目用的。亲测可用。
/**
* 解压文件
* @param zipPath 要解压的目标文件
* @param descDir 指定解压目录
*/
public static void decompressZip(String zipPath, String descDir) {
File zipFile = new File(zipPath);
ZipFile zip = null;
InputStream in = null;
OutputStream out = null;
try {
zip = new ZipFile(zipFile, Charset.forName("gbk"));//防止中文目录,乱码
for(Enumeration entries = zip.entries(); entries.hasMoreElements();){
ZipEntry entry = (ZipEntry)entries.nextElement();
String zipEntryName = entry.getName();
in = zip.getInputStream(entry);
//指定解压后的文件夹+当前zip文件的名称
String outPath = (descDir+zipEntryName).replace("/", File.separator);
//判断路径是否存在,不存在则创建文件路径
File file = new File(outPath.substring(0, outPath.lastIndexOf(File.separator)));
if(!file.exists()){
file.mkdirs();
}
//判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压
if(new File(outPath).isDirectory()){
continue;
}
//保存文件路径信息(可利用md5.zip名称的唯一性,来判断是否已经解压)
out = new FileOutputStream(outPath);
byte[] buf1 = new byte[2048];
int len;
while((len=in.read(buf1))>0){
out.write(buf1,0,len);
}
in.close();
out.close();
}
//必须关闭,要不然这个zip文件一直被占用着,要删删不掉,改名也不可以,移动也不行,整多了,系统还崩了。
zip.close();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(zip != null){
try {
zip.close();
} catch (IOException e) {
e.printStackTrace();
}
}
zipFile.delete();
}
}
希望对大家有帮助,如其他童鞋有疑问,欢迎留言