Java多文件压缩下载

直接上代码:

1.工具类:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipUtils {
    
    private ZipUtils(){
    }
    
    public static void doCompress(String srcFile, String zipFile) throws IOException {
        doCompress(new File(srcFile), new File(zipFile));
    }
    
    /**
     * 文件压缩
     * @param srcFile 目录或者单个文件
     * @param zipFile 压缩后的ZIP文件
     */
    public static void doCompress(File srcFile, File zipFile) throws IOException {
        ZipOutputStream out = null;
        try {
            out = new ZipOutputStream(new FileOutputStream(zipFile));
            doCompress(srcFile, out);
        } catch (Exception e) {
            throw e;
        } finally {
            out.close();//记得关闭资源
        }
    }
    
    public static void doCompress(String filelName, ZipOutputStream out) throws IOException{
        doCompress(new File(filelName), out);
    }
    
    public static void doCompress(File file, ZipOutputStream out) throws IOException{
        doCompress(file, out, "");
    }
    
    public static void doCompress(File inFile, ZipOutputStream out, String dir) throws IOException {
        if ( inFile.isDirectory() ) {
            File[] files = inFile.listFiles();
            if (files!=null && files.length>0) {
                for (File file : files) {
                    String name = inFile.getName();
                    if (!"".equals(dir)) {
                        name = dir + "/" + name;
                    }
                    ZipUtils.doCompress(file, out, name);
                }
            }
        } else {
             ZipUtils.doZip(inFile, out, dir);
        }
    }
    
    public static void doZip(File inFile, ZipOutputStream out, String dir) throws IOException {
        String entryName = null;
        if (!"".equals(dir)) {
            entryName = dir + "/" + inFile.getName();
        } else {
            entryName = inFile.getName();
        }
        ZipEntry entry = new ZipEntry(entryName);
        out.putNextEntry(entry);
        
        int len = 0 ;
        byte[] buffer = new byte[1024];
        FileInputStream fis = new FileInputStream(inFile);
        while ((len = fis.read(buffer)) > 0) {
            out.write(buffer, 0, len);
            out.flush();
        }
        out.closeEntry();
        fis.close();
    }
    
    public static void main(String[] args) throws IOException {
        doCompress("D:/java/", "D:/java.zip");
    }
    
}

2.Controller代码:

@RequestMapping(value = "/downLoadZip")
public void downLoadZip(HttpServletResponse response) throws IOException{
    String zipName = "myZipFile.zip";
    List<FileBean> fileList = fileService.getFileList();//查询数据库中记录
    response.setContentType("APPLICATION/OCTET-STREAM");  
    response.setHeader("Content-Disposition","attachment; filename="+zipName);
    ZipOutputStream out = new ZipOutputStream(response.getOutputStream());
    try {
        for(Iterator<FileBean> it = fileList.iterator();it.hasNext();){
            FileBean file = it.next();
            ZipUtils.doCompress(file.getFilePath()+file.getFileName(), out);
            response.flushBuffer();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }finally{
        out.close();
    }
}

3.实体类(根据项目自定义):

public class FileBean implements Serializable{
    
    private static final long serialVersionUID = -5452801884470115159L;
    
    private Integer fileId;//主键
    
    private String filePath;//文件保存路径
    
    private String fileName;//文件保存名称
    
    public FileBean(){
        
    }
       
    //getter and setter...
}    
发布了2653 篇原创文章 · 获赞 5024 · 访问量 46万+

猜你喜欢

转载自blog.csdn.net/qq_20042935/article/details/104552134