不解压修改压缩文件内容

简介: 在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()));


相关文章
|
3月前
|
数据安全/隐私保护 Python
Pyzipper解压文件和压缩文件夹方法
Pyzipper解压文件和压缩文件夹方法
43 1
|
5月前
|
算法 编译器 API
C++ MiniZip实现目录压缩与解压
Zlib是一个开源的数据压缩库,提供了一种通用的数据压缩和解压缩算法。它最初由`Jean-Loup Gailly`和`Mark Adler`开发,旨在成为一个高效、轻量级的压缩库,其被广泛应用于许多领域,包括网络通信、文件压缩、数据库系统等。其压缩算法是基于`DEFLATE`算法,这是一种无损数据压缩算法,通常能够提供相当高的压缩比。在Zlib项目中的`contrib`目录下有一个`minizip`子项目,minizip实际上不是`zlib`库的一部分,而是一个独立的开源库,用于处理ZIP压缩文件格式。它提供了对ZIP文件的创建和解压的简单接口。minizip在很多情况下与`zlib`一起使用
154 0
C++ MiniZip实现目录压缩与解压
|
6月前
|
Java
Java实现zip文件压缩:单个文件、文件夹以及文件和文件夹的压缩
Java实现zip文件压缩:单个文件、文件夹以及文件和文件夹的压缩
|
Java 程序员
批量压缩16万个文件夹为压缩包(.zip格式)
🍅程序员小王的博客:程序员小王的博客 🍅 欢迎点赞 👍 收藏 ⭐留言 📝 🍅 如有编辑错误联系作者,如果有比较好的文章欢迎分享给我,我会取其精华去其糟粕 🍅java自学的学习路线:java自学的学习路线
203 0
批量压缩16万个文件夹为压缩包(.zip格式)
|
Linux
解压 .solitairetheme8 文件
解压 .solitairetheme8 文件
76 0
|
SQL IDE Shell
zip包自动解压缩脚本 | 学习笔记
快速学习zip包自动解压缩脚本
490 0
|
SQL Shell Linux
zip 包自动解压缩脚本 | 学习笔记
快速学习 zip 包自动解压缩脚本
159 0