Java ZIP 文件从一个 zip 复制并粘贴到另一个 [英] Java ZIP File copy and paste from one zip to another

查看:80
本文介绍了Java ZIP 文件从一个 zip 复制并粘贴到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发 Minecraft Mod Installer 并考虑将引擎从 C# 移至 Java 想现在是否可以将一些文件从一个 zip 复制到另一个 zip 而不将它们解压缩到临时文件夹,如果可能的话如何我会去吗?

I'm working on Minecraft Mod Installer and thinking about moving the engine from C# to Java would like to now if it is possible to copy some files from one zip to another without extracting them to a temporary folder and if this possible how would I go about this?

在旧引擎中,它将文件解压缩到临时文件夹,然后将它们添加到 Minecraft.jar

In the old engine it decompressed the files to a temp folder then added them to the Minecraft.jar

推荐答案

jre 中的原生 zip 支持可以做到.试试这个:

The native zip support in jre can do it. Try this:

void substitute(ZipInputStream zis, ZipOutputStream zos) {
  for (ZipEntry ze = zis.getNextEntry(); ze != null; ze = zis.getNextEntry()) {
    if (ze.getName() is what you want to copy) {
      zos.putNextEntry(ze)
      Array[Byte] buffer = new Array[Byte](1024)
      for (int read = zis.read(buffer); read != -1; read = zis.read(buffer)) {
        zos.write(buffer, 0, read)
      }
      zos.closeEntry
    }
  }
  zos.close()
  zis.close()
}

注意:zip文件里面的数据是解压后再次压缩的.

Note: the data inside zip file is decompressed and compressed again.

这篇关于Java ZIP 文件从一个 zip 复制并粘贴到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆