zyl910

优化技巧、硬件体系、图像处理、图形学、游戏编程、国际化与文本信息处理。

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  215 随笔 :: 0 文章 :: 145 评论 :: 111万 阅读
< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

作者: zyl910

一、缘由

有些时候需要替换zip内的文件。
网上的办法大多是——先解压,然后对解压目录替换文件,最后再重新压缩。该办法需要比较繁琐,且需要一个临时目录。
于是想找无需解压的方案。
后来找到利用 ZipInputStream、ZipOutputStream 实现该功能的办法。

二、源码

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

public class ZipCopyTest {
    public static void main(String[] args) {
        String srcPath = "target/classes/static/test.docx";
        String outPath = "E:\\test\\export\\test_copy.docx";
        try(FileInputStream is = new FileInputStream(srcPath)) {
            try(FileOutputStream os = new FileOutputStream(outPath)) {
                copyZipStream(os, is);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("ZipCopyTest done.");
    }

    private static void copyZipStream(OutputStream os, InputStream is) throws IOException {
        try(ZipInputStream zis = new ZipInputStream(is)) {
            try(ZipOutputStream zos = new ZipOutputStream(os)) {
                ZipEntry se;
                while ((se = zis.getNextEntry()) != null) {
                    if (null==se) continue;
                    String line = String.format("ZipEntry(%s, isDirectory=%d, size=%d, compressedSize=%d, time=%d, crc=%d, method=%d, comment=%s)",
                            se.getName(), (se.isDirectory())?1:0, se.getSize(), se.getCompressedSize(), se.getTime(), se.getCrc(), se.getMethod(), se.getComment());
                    System.out.println(line);
                    ZipEntry de = new ZipEntry(se);
                    zos.putNextEntry(de);
                    copyStream(zos, zis);
                    zos.closeEntry();
                }
            }
        }
    }

    private static void copyStream(OutputStream os, InputStream is) throws IOException {
        copyStream(os, is, 0);
    }

    private static void copyStream(OutputStream os, InputStream is, int bufsize) throws IOException {
        if (bufsize<=0) bufsize= 4096;
        int len;
        byte[] bytes = new byte[bufsize];
        while ((len = is.read(bytes)) != -1) {
            os.write(bytes, 0, len);
        }
    }
}

现在已经实现zip内项目的逐项复制了。只要稍微改造一下,便能实现“替换zip内的文件”的功能了。
具体办法是在循环内根据 ZipEntry.getName() 判断当前是哪一个文件,随后不是简单的调用 copyStream,而是改为根据自己需要对流数据进行处理。

参考文献

posted on   zyl910  阅读(1609)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
阅读排行:
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 推荐几款开源且免费的 .NET MAUI 组件库
· 实操Deepseek接入个人知识库
· 易语言 —— 开山篇
· Trae初体验
点击右上角即可分享
微信分享提示