java zip文件复制_[Java] 遍历zip内的数据,逐项复制流来生成新的zip文件的范例(可用于替换zip内的文件)...

一、缘由

有些时候需要替换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,而是改为根据自己需要对流数据进行处理。

参考文献

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值