java处理文件压缩及删除

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();

        }
    }

}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值