打成zip压缩包代码

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipUtil {
    public static void main(String[] args) throws IOException {
        byte[] buf = new byte[1024 * 2];
        FileOutputStream fos = new FileOutputStream("D:\\ziputils\\demo.zip");
        ZipOutputStream zos = new ZipOutputStream(fos);
        for (int i = 0; i < 7; i++) {
            zos.putNextEntry(new ZipEntry("a\\b\\c\\c" + i + ".txt"));
            FileInputStream fis = new FileInputStream("D:\\ziputils\\c" + i + ".txt");
            //使用字节缓冲输入流
            BufferedInputStream bis = new BufferedInputStream(fis);
            int len;
            while ((len = bis.read(buf)) != -1) {
                zos.write(buf, 0, len);
            }
            zos.closeEntry();
            bis.close();
        }
        // 空文件夹的处理, 最后不加 / 会将c当成文件处理
        zos.putNextEntry(new ZipEntry("a/d" + "/"));
        // 没有文件,不需要文件的copy
        zos.closeEntry();
        //必须要执行 zos.finish(); close()时内部会调用finish()
        zos.close();
    }
}

上面的代码执行完毕的话, 在D盘的ziputils下面就会有一个demo.zip的压缩包
解压之后, 就会解压出来一个文件夹a, a文件夹下面有b和d文件夹
其中d是空文件夹, b下面有c文件夹, c下面有c0.txt ~ c6.txt文件

生成zip压缩包并直接返回给前端

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

@RestController
@RequestMapping("test")
public class ZipUtil {
    @GetMapping("/downloadZip")
    public void downloadZip(HttpServletResponse response) throws IOException {
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "attachment;fileName=" +
                new String("文件.zip".getBytes("gbk"), "iso8859-1"));
        ServletOutputStream os = response.getOutputStream();
        ZipOutputStream zos = new ZipOutputStream(os);

        byte[] buf = new byte[1024 * 2];
        for (int i = 0; i < 7; i++) {
            zos.putNextEntry(new ZipEntry("a\\b\\c\\c" + i + ".txt"));
            FileInputStream fis = new FileInputStream("D:\\ziputils\\c" + i + ".txt");
            //使用字节缓冲输入流
            BufferedInputStream bis = new BufferedInputStream(fis);
            int len;
            while ((len = bis.read(buf)) != -1) {
                zos.write(buf, 0, len);
            }
            zos.closeEntry();
            bis.close();
        }
        // 空文件夹的处理, 最后不加 / 会将c当成文件处理
        zos.putNextEntry(new ZipEntry("a/d" + "/"));
        // 没有文件,不需要文件的copy
        zos.closeEntry();
        //必须要执行 zos.finish(); close()时内部会调用finish()
        zos.close();
    }
}

上面的代码执行完毕的话, 会在浏览器直接下载一个文件.zip的压缩包
解压之后, 就会解压出来一个文件夹a, a文件夹下面有b和d文件夹
其中d是空文件夹, b下面有c文件夹, c下面有c0.txt ~ c6.txt文件

提供前端下载zip压缩包代码

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;

public class ZipUtil {
    public void downloadZip(HttpServletResponse response) {
        //zip压缩文件保存的路径
        String zipPath = PropertiesUtils.UPLOAD_FOLDER + "文件名称" + ".zip";
        BufferedInputStream bis = null;
        ServletOutputStream os = null;
        try {
            response.setContentType("application/octet-stream");
            response.setHeader("Content-Disposition", "attachment;fileName=" +
                    new String("文件.zip".getBytes("gbk"), "iso8859-1"));
            bis = new BufferedInputStream(new FileInputStream(zipPath));
            os = response.getOutputStream();
            int len;
            byte[] bytes = new byte[1024 * 2];
            while ((len = bis.read(bytes)) != -1) {
                os.write(bytes, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("下载异常!");
        } finally {
            try {
                if (bis != null) {
                    bis.close();
                }
                if (os != null) {
                    os.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

删除zip压缩文件

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class ZipUtil {
    public void deleteZip(String fileName) { //压缩文件名称
        try {
            //压缩文件路径地址
            String zipPath = PropertiesUtils.UPLOAD_FOLDER + fileName + ".zip";
            if (new File(zipPath).exists()) Files.delete(Paths.get(zipPath));
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("删除异常!");
        }
    }
}

递归删除文件/文件夹

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class ZipUtil {
    //删除文件夹以及下面的内容
    public static void deleteFiles(File file) throws IOException {
        if (!file.exists()) return; //若文件不存在直接返回
        if (file.isDirectory()) { //目录的情况下, 遍历所有的子级文件
            for (File f : file.listFiles()) {
                deleteFiles(f);
            }
        }
        Files.delete(file.toPath()); //不管是文件还是目录, 最终都要删除
    }
}

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐