import org.springframework.util.ResourceUtils;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* description 压缩文件处理类
* 2021/6/29$
* @author
**/
public class CompressUntil {
/**
* 创建ZIP文件
* @param //sourcePath 目标文件或文件夹路径
* @param //filePathAndName 生成的zip文件存在路径(包括文件名)
*/
public static void createZip(HttpServletResponse response,String name) throws IOException {
//获取项目路径
File path = new File(ResourceUtils.getURL("tmpl").getPath());
String sourcePath= path+ File.separator+"report"+File.separator+name;
String deletePath= path+ File.separator+"report";
System.out.println(sourcePath);
File file =new File(sourcePath);
if(!file.exists()){
file .mkdir();
}
String filePathAndName=sourcePath+".zip";
ZipOutputStream zos = null;
try {
//实际上此压缩包并不存在,只是目标压缩包文件
FileOutputStream fos = new FileOutputStream(filePathAndName);
zos = new ZipOutputStream(fos);
writeZip(new File(sourcePath), "", zos);
} catch (FileNotFoundException e) {
System.out.println("创建ZIP文件失败"+e);
} finally {
if (zos != null) {
zos.close();
}else {
System.out.println("创建ZIP文件失败");
}
}
System.out.println("压缩包响应给客户浏览器******start");
fileResponse(sourcePath,response,name,deletePath);
System.out.println("压缩包响应给客户浏览器******end");
}
/**
* 压缩zip,循环压缩子目录文件
*/
private static void writeZip(File file, String parentPath, ZipOutputStream zos) throws IOException {
if(file.exists()){
if(file.isDirectory()){//处理文件夹
parentPath+=file.getName()+File.separator;
File [] files=file.listFiles();
if(files.length != 0)
{
for(File f:files){
writeZip(f, parentPath, zos);
}
}
else
{ //空目录则创建当前目录
zos.putNextEntry(new ZipEntry(parentPath));
}
}else{
FileInputStream fis=null;
try {
fis=new FileInputStream(file);
ZipEntry ze = new ZipEntry(parentPath + file.getName());//创建压缩文件
zos.putNextEntry(ze);//添加压缩文件
byte [] content=new byte[1024];
int len;
while((len=fis.read(content))!=-1){
zos.write(content,0,len);
zos.flush();
}
} catch (FileNotFoundException e) {
System.out.println("创建ZIP文件失败"+e);
} finally{
if(fis!=null){
fis.close();
}else {
System.out.println("创建ZIP文件失败");
}
}
}
}
}
//将压缩包响应给客户浏览器
public static void fileResponse(String path, HttpServletResponse response, String fileName, String deletePath) throws IOException {
File file = new File(path+".zip");
if(!file.exists()){
System.out.println("没有找到相应文件");
}
FileInputStream fileInputStream = new FileInputStream(file);
//设置Http响应头告诉浏览器下载这个附件,下载的文件名也是在这里设置的
response.setHeader("Content-Disposition", "attachment;Filename=" + URLEncoder.encode(fileName+".zip", "UTF-8"));
OutputStream outputStream = response.getOutputStream();
byte[] bytes = new byte[2048];
int len = 0;
while ((len = fileInputStream.read(bytes))>0){
outputStream.write(bytes,0,len);
}
fileInputStream.close();
outputStream.close();
//删除下载至本地文件夹及文件
deletefile(deletePath);
}
//删除下载操作本地生成文件夹及文件
public static void deletefile(String delpath) {
File file = new File(delpath);
if (!file.isDirectory()) {
file.delete();
}
else if (file.isDirectory()) {
String[] filelist = file.list();
for (int i = 0; i < filelist.length; i++) {
File delfile = new File(delpath +File.separator+ filelist[i]);
if (!delfile.isDirectory()) {
delfile.delete();
}
else if (delfile.isDirectory()) {
deletefile(delpath +File.separator + filelist[i]);
}
}
file.delete();
}
}
}
02-13
305
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交