zip压缩与解压缩,文件下载与删除

package cn.com.zip.webapp.Util;

import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants;
/**
 * 
 * @desc 文件压缩成zip
 * @date 2020年4月28日上午10:10:45
 * @version 1.0
 */
public class FileZipUtil {
    

    /**
     * 
     * @desc zip解压缩
     * @date 2020年5月16日下午4:35:40
     * @param source 压缩文件全路径
     * @param target 要解压路径
     * @param targetName 解压文件夹名称
     * @throws Exception
     */
    public static void unzip (String source,String target,String targetName) throws Exception{
        try {
            File file = new File(source);
            if(!file.exists() || file.isDirectory()){
                throw new Exception("将要解压文件不存在或路径填写不正确!");
            }

            file = new File(target+File.separator+targetName);
            if(!file.exists()){
                file.mkdirs();
            }
            ZipFile zipfile = new ZipFile(source);
            if (!zipfile.isValidZipFile()) {  
                throw new Exception("压缩文件不合法,可能被损坏.");  
            } 
            zipfile.setFileNameCharset("GBK");
            zipfile.extractAll(target+File.separator+targetName);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    /**
     * 
     * @desc 生成zip压缩
     * @date 2020年5月16日下午4:36:18
     * @param source 要压缩文件全路径
     * @param target 压缩文件存放路径
     * @param targetName 解压文件名称
     * @throws Exception
     */
    public static void zip (String source,String target,String targetName) throws Exception{
        try {           
            File file = new File(target);
            if(!file.exists()){
                file.mkdirs();
            }
            file = new File(source);
            if(!file.exists()){
                throw new Exception("将要解压文件不存在或路径填写不正确!");
            }

            ZipFile zipfile = new ZipFile(target+File.separator+targetName);
            zipfile.setFileNameCharset("GBK");
            ZipParameters params = new ZipParameters();
            params.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);           // 压缩方式  
            params.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);    // 压缩级别  
            if(file.isFile()){
                zipfile.addFile(file, params);
            }else{
                zipfile.addFolder(source, params);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }


    }
    
    /**
     * 
     * @desc 文件下载
     * @date 2020年4月28日上午10:25:21
     * @param files
     * @param createTime
     * @param response
     * @throws Exception
     */
    public static void downLoad(String files,String formFileName, HttpServletResponse response,HttpServletRequest request) throws Exception {
        FileInputStream in = null;
        OutputStream out = null;
        try {
              String userAgent = request.getHeader("User-Agent");
              // 针对IE或者以IE为内核的浏览器:
                if (userAgent.contains("MSIE") || userAgent.contains("Trident")) {
                    formFileName = java.net.URLEncoder.encode(formFileName, "UTF-8");
                } else {
                    // 非IE浏览器的处理:
                    formFileName = new String(formFileName.getBytes("UTF-8"), "ISO-8859-1");
                }
            // 设置响应头,控制浏览器下载该文件
            response.setContentType("multipart/form-data");
            // 下载不打开
            response.setContentType("application/x-msdownload;charset=utf-8");
            response.addHeader("Content-Disposition", String.format("attachment;filename=\"%s\"", formFileName));
            response.setCharacterEncoding("UTF-8");
            // 读取要下载的文件,保存到文件输入流
            in = new FileInputStream(files);
            // 创建输出流
            out = response.getOutputStream();
            // 创建缓冲区
            byte buffer[] = new byte[1024];
            int len = 0;
            // 循环将输入流中的内容读取到缓冲区当中
            while ((len = in.read(buffer)) > 0) {
                // 输出缓冲区的内容到浏览器,实现文件下载
                out.write(buffer, 0, len);
            }
            
        } catch (Exception e) {
              e.printStackTrace();
        }finally {
            // 关闭文件输入流
            in.close();
            // 关闭输出流
            out.close();
        }
    }
    
    /**
     * 
     * @desc 文件删除
     * @date 2020年5月16日下午3:07:28
     * @param file
     */
    public static void deleteFiles(File file) {
        if(!file.exists()) { return;}
        if(file.isFile() || file.list()==null) {
                file.delete();
            }else {
                File[] files = file.listFiles();
                for(File a:files) {
                    deleteFiles(a);                    
                }
                file.delete();
            }
        }
}

 

posted @ 2021-06-10 14:48  艾瑞科斯  阅读(519)  评论(0)    收藏  举报