Java导出zip格式压缩包 apache

1.引用的包

        <!-- https://mvnrepository.com/artifact/org.apache.ant/ant -->
       <!--       zip -->
        <dependency>
            <groupId>org.apache.ant</groupId>
            <artifactId>ant</artifactId>
            <version>1.10.11</version>
        </dependency>

2.工具类,生成到本地

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author:lcc
 * @DateTime:
 * @DESC ZIP导出工具类
 */
public class ApacheZipUtils {

    public static void main(String[] args) throws Exception{
//        doCompress("D:\\port","D:\\文件夹1.zip");
        ZipOutputStream bizOut = new ZipOutputStream(new FileOutputStream("D:\\文件夹2.zip"));
        doCompress1(new ArrayList<String>(){{
            add("D:\\port\\000990指引单.pdf");
            add("D:\\port\\000991report.pdf");
        }},   bizOut);
        //不要忘记调用
        bizOut.close();
    }

    public static void doCompress(String srcFile, String zipFile) throws Exception {
        doCompress(new File(srcFile), new File(zipFile));
    }

    /**
     * 文件压缩
     * @param srcFile  目录或者单个文件
     * @param destFile 压缩后的ZIP文件
     */
    public static void doCompress(File srcFile, File destFile) throws Exception {
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(destFile));

        if(srcFile.isDirectory()){
            File[] files = srcFile.listFiles();
            for(File file : files){
                doCompress(file, out);
            }
        }else {
            doCompress(srcFile, out);
        }

        out.close();
    }

    public static void doCompress1(List<String> pathnames, ZipOutputStream out) throws IOException {
        for(String path:pathnames){
            doCompress(new File(path), out);
        }
    }

    public static void doCompress(String pathname, ZipOutputStream out) throws IOException{
        doCompress(new File(pathname), out);
    }

    public static void doCompress(File file, ZipOutputStream zipOut) throws IOException{
        if( file.exists() ){
            //读取相关的文件
            FileInputStream fis = new FileInputStream(file);
            zipOut.putNextEntry(new ZipEntry(file.getName()));

            int len = 0 ;
            // 读取文件的内容,打包到zip文件
            byte[] buffer = new byte[1024];
            while ((len = fis.read(buffer)) > 0) {
                zipOut.write(buffer, 0, len);
            }
            zipOut.setEncoding("gbk");//如果不加此句,压缩文件依然可以生成,只是在打开和解压的时候,会显示乱码,但是还是会解压出来
            zipOut.flush();
            zipOut.closeEntry();
            fis.close();
        }
    }
}

3.下载

   @GetMapping("/downloadFile")
    public void downloadFile(HttpServletRequest request, HttpServletResponse response)throws Exception {
        List<String> ls=
        new ArrayList<String>(){{
            add("D:\\port\\000990指引单.pdf");
            add("D:\\port\\000991report.pdf");
        }};


//        OutputStream zipOutputStream = new ZipOutputStream(response.getOutputStream());
        String downloadFileName = URLEncoder.encode( String.valueOf(new Date().getTime())+".zip", "UTF-8");
        response.reset();
        response.setContentType("application/x-download");
        response.setHeader("Content-Disposition", "attachment;filename=" + new String((downloadFileName).getBytes(), "UTF-8"));

        ZipOutputStream outZ = null;
        try {
            outZ = new ZipOutputStream(response.getOutputStream());
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        ZipUtils.doCompress1(ls,outZ);
        //不要忘记调用
        outZ.close();
    }

 

 参考:

https://blog.csdn.net/xueyepiaoling/article/details/6119924

https://blog.csdn.net/qq_45222558/article/details/124186344

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值