不解压修改压缩文件内容

简介: 在java中,可以不解压压缩包就修改压缩包中文件的内容。


image.png

在java中,可以不解压压缩包就修改压缩包中文件的内容。

/**
     * 图片输出使用(现在earlying中模板使用)
     *
     * @param fileMessage
     * @return
     * @throws UnsupportedEncodingException
     */
    @GetMapping("/downloadApplication")
    public String downloadApplication(FileMessage fileMessage) throws Exception {
        // 获取HttpServletResponse
        HttpServletResponse response =
            ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getResponse();
        // 下载路径
        String downUrl = fileMessage.getFileDownloadUri();
        if (downUrl != null) {
            // 复制模板文件 生成uuid服务名
            UUID uuid = UUID.randomUUID();
            String newRarFile = "/home/common_files/" + uuid + ".zip";
            // 开始复制
            updateZipFile(downUrl, newRarFile, fileMessage.getId());
            // 设置文件路径
            File file = new File(newRarFile);
            if (file.exists()) {
                byte[] buffer = new byte[1024];
                FileInputStream fis = null;
                BufferedInputStream bis = null;
                try {
                    fis = new FileInputStream(file);
                    bis = new BufferedInputStream(fis);
                    OutputStream os = response.getOutputStream();
                    int i = bis.read(buffer);
                    while (i != -1) {
                        os.write(buffer, 0, i);
                        i = bis.read(buffer);
                    }
                    return "";
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (bis != null) {
                        try {
                            bis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (fis != null) {
                        try {
                            fis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
        return "";
    }
    public void updateZipFile(String inputName, String outPutName, String id) throws IOException {
        ZipFile zipFile = new ZipFile(inputName);
        // 复制为新zip
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(outPutName));
        // 遍历所有文件复制
        for (Enumeration e = zipFile.entries(); e.hasMoreElements();) {
            ZipEntry entryIn = (ZipEntry)e.nextElement();
            if (!entryIn.getName().equalsIgnoreCase("myMapApp/config.js")) {
                // zos.putNextEntry(entryIn);
                zos.putNextEntry(new ZipEntry(entryIn.getName()));
                InputStream is = zipFile.getInputStream(entryIn);
                byte[] buf = new byte[1024];
                int len;
                while ((len = is.read(buf)) > 0) {
                    zos.write(buf, 0, (len < buf.length) ? len : buf.length);
                }
            }
            // 当前文件需要复写
            else {
                zos.putNextEntry(new ZipEntry("myMapApp/config.js"));
                InputStream is = zipFile.getInputStream(entryIn);
                byte[] buf = new byte[1024 * 5];
                int len;
                while ((len = (is.read(buf))) > 0) {
                    String s = new String(buf);
                    if (s.contains("let appid=")) {
                        buf = s.replaceAll("let appid=", "let appid=\"" + id + "\"").getBytes();
                        s = s.replaceAll("let appid=", "let appid=\"" + id + "\"");
                    }
                    if (s.contains("let service=")) {
                        buf = s.replaceAll("let service=", "let service=\"" + instanceId + "\"").getBytes();
                    }
                    zos.write(buf, 0, (len < buf.length) ? len : buf.length);
                }
            }
            zos.closeEntry();
        }
        zos.close();
    }

这里有一个问题。

1. zos.putNextEntry(entryIn); 这行代码会报错 --java.util.zip.ZipException: invalid entry compressed size (expected 1466196 but got 1499165 bytes)
2. 原因是复制过程中 压缩方式不同导致流的字节对应不上 所以需要用以下代码。
3. zos.putNextEntry(new ZipEntry(entryIn.getName()));


相关文章
|
11月前
|
人工智能 弹性计算
阿里云权益中心2026年最新:个人、学生及企业用户上云补贴及活动政策解读
2026年阿里云优惠汇总:个人38元起/年,企业199元/年起,学生领300元券免费用。超160款产品免费试用,开通百炼享7000万tokens,企业迁云最高补5亿算力。先领券再下单更划算!
1212 13
|
机器学习/深度学习 大数据 数据挖掘
我的2017云栖之行
2017云栖大会参会感想
|
JavaScript 安全 开发者
|
缓存 Java
JVM解读-性能调优实例
JVM性能调优 1 堆设置调优 年轻代大小选择 响应时间优先的应用:尽可能设大,直到接近系统的最低响应时间限制(根据实际情况选择)。在此种情况下,年轻代收集发生的频率也是最小的。
1287 0
|
1天前
|
监控 网络协议 算法
2026年起SSL证书有效期逐年缩短:企业如何避免“续期风暴”!
SSL证书有效期将从2026年起分三阶段逐步缩短至47天,人工续期难以为继。阿里云“WoSign SSL证书+自动化服务”,支持域名监控、自动托管、云产品/服务器一键部署及智能提醒,助力企业高效应对高频续期挑战。
8058 2
|
10天前
|
域名解析 人工智能 Linux
|
20天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
18292 63
|
18天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
4天前
|
人工智能 安全 网络安全
|
2天前
|
人工智能 弹性计算 安全
阿里云 码上2027 AI coding 嘉年华
阿里云“码上2027”AI Coding嘉年华于2026年12月2—3日在上海西岸艺术中心举行,聚焦AI Coding从单点提效迈向企业级研发体系升级。发布Qwen4-max模型、QBot智能协作、AgentCore平台等重磅成果,并推出ECS云服务器特惠(200Mbps流量月付40元起)。
775 0