springboot解压文件流zip压缩包

springboot解压文件流zip压缩包

原始文件存储的地方:
在这里插入图片描述
需要在当前目录下解压该文件,如下图:
在这里插入图片描述

代码示例:

private Result<String> getLocationGuideLayerName(YbYstbtqTaskResolveParam params, String fishnetLayerName) throws IOException {
        // 获取文件(这里的方法是系统的根据文件id获取文件相关的信息)
        FileDownloadVo fileDownloadVo = uploadService.getFile(params.getFileId());
        List<AnnexPO> fileInfo = uploadService.getFileInfo(new ArrayList<>(Collections.singletonList(params.getFileId())));

        if (fileInfo.size() < 1) {
            throw new RuntimeException("shp文件无效,请重试!");
        }

        ZipInputStream zis = new ZipInputStream(fileDownloadVo.getFileStream());
        ZipEntry zipEntry = zis.getNextEntry();

        // 文件存储目录+文件名字
        String filePathTemp = removeLastSlash(uploadConfig.getLocalStorageDirectory()) + fileInfo.get(0).getUploadPath(); // F:\temp\20240708\acbcac4038da45dfa77a3142e9a46501\测试数据2023SAR.zip
        // 文件存储目录不加文件名
        String newFilePath = formatFilePath(filePathTemp); // F:\temp\20240708\acbcac4038da45dfa77a3142e9a46501
        try {
            // 遍历ZIP文件中的每个条目
            while (zipEntry != null) {
                String filePath = newFilePath + File.separator + zipEntry.getName();
                if (!zipEntry.isDirectory()) {
                    // 提取文件-遍历提取整个zip的所有文件
                    extractFile(zis, filePath);
                    // 关闭当前条目以读取下一个条目
                    zis.closeEntry();
                }
                zipEntry = zis.getNextEntry();
            }
            // 关闭ZIP输入流
            zis.close();
        } catch (IOException e) {
            throw new RuntimeException("提取shpe文件失败: " + fileDownloadVo.getFileName() + ". 请重试!", e);
        }
    }


	/**
     * 辅助方法,用于从ZIP输入流中提取文件
     *
     * @param zis ZIP输入流
     * @param filePath 文件的完整路径
     * @throws IOException 如果发生I/O错误
     */
    private void extractFile(ZipInputStream zis, String filePath) throws IOException {
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
        byte[] bytesIn = new byte[4096];
        int read = 0;
        while ((read = zis.read(bytesIn)) != -1) {
            bos.write(bytesIn, 0, read);
        }
        bos.close();
    }

	/**
     * @Description: 格式化文件路径,返回文件存储的路径
     *
     * @Param:  [path]
     * @Return: java.lang.String
     * @Author yanghaoxing
     * @Date 2024/7/8 11:27
     */
    public String formatFilePath(String path) {
        String newPath = path;
        // 找到最后一个'/'的索引位置
        int lastIndex = path.lastIndexOf('/');
        if (lastIndex != -1) { // 确保lastIndexOf找到了'/'
            newPath = path.substring(0, lastIndex);
        }
        return newPath;
    }

	/**
     * @Description: 去掉字符串最后一个 /
     *
     * @Param:  [str]
     * @Return: java.lang.String
     * @Author yanghaoxing
     * @Date 2024/7/8 11:17
     */
    public static String removeLastSlash(String str) {
        if (str != null && str.endsWith("/")) {
            return str.substring(0, str.length() - 1);
        }
        return str; // 不改变原字符串,如果它不以'/'结尾
    }


  • 7
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring Boot 中实现分片下载 Zip 压缩包文件可以采用以下步骤: 1. 在 Controller 中定义一个用于下载 Zip 压缩包的接口方法,并在方法上添加注解 @GetMapping("/download"),其中 "/download" 是下载接口的路径。 2. 在方法中获取 Zip 压缩包文件路径,并使用 java.io.File 类创建一个文件对象。 3. 使用 java.util.zip.ZipOutputStream 类创建一个 Zip 压缩对象,并使用 java.io.FileInputStream 类创建一个文件输入对象。 4. 使用 Zip 压缩对象将文件输入对象写入 Zip 压缩包中。 5. 关闭文件输入Zip 压缩对象。 6. 在方法中使用 javax.servlet.http.HttpServletResponse 类设置下载文件的响应头,包括 Content-Disposition、Content-Type 和 Content-Length。 7. 使用 java.io.BufferedInputStream 类和 java.io.BufferedOutputStream 类创建一个缓冲输入对象和一个缓冲输出对象。 8. 使用缓冲输入对象将 Zip 压缩包写入缓冲输出对象。 9. 关闭缓冲输入和缓冲输出对象。 10. 返回 null,结束方法。 以下是一个示例代码: ```java @GetMapping("/download") public ResponseEntity<byte[]> download() throws IOException { String filePath = "path/to/zip/file.zip"; File file = new File(filePath); FileInputStream fis = new FileInputStream(file); ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(response.getOutputStream())); // 将文件写入 Zip 压缩包ZipEntry zipEntry = new ZipEntry(file.getName()); zos.putNextEntry(zipEntry); byte[] buffer = new byte[1024]; int len = 0; while ((len = fis.read(buffer)) != -1) { zos.write(buffer, 0, len); } fis.close(); zos.closeEntry(); zos.close(); // 设置响应头 HttpHeaders headers = new HttpHeaders(); headers.setContentDispositionFormData("attachment", file.getName()); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); headers.setContentLength(file.length()); // 返回响应体 return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.OK); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

星月前端

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值