第三方接口返回ResponseEntity<byte[]>文件压缩包,解压处理过程

本文介绍了如何使用Java的HttpRequest库从指定URL下载压缩包,然后通过ZipInputStream逐个解压文件并根据不同情况处理文件和目录。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

byte[]  bytes = HttpRequest.get(getDownloadArchivesUrl())
                    .body(getDownLoadParam(requestId, requestCreator))
                    .execute().bodyBytes();

// 将压缩包字节数组包装成输入流对象
// 创建ZipInputStream对象
try (ZipInputStream zipInputStream = new ZipInputStream(new ByteArrayInputStream(bytes), Charset.forName("GBK"))) {
    // 遍历压缩包中的每个文件
    ZipEntry entry;
    while ((entry = zipInputStream.getNextEntry()) != null) {
        try {
            // 获取文件名
            String filename = entry.getName();
            if (!entry.isDirectory()) {
                //对文件进行业务处理
            } else {
            	//对目录进行处理
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            // 关闭当前文件的ZipEntry
            try {
                zipInputStream.closeEntry();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
} catch (IOException e) {
    throw new RuntimeException(e);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值