修改zip文件条目的文件内容

3

我想更新位于zip文件中的文本文件内容。

我无法找到如何实现此操作,而下面的代码也不能正常工作。

非常感谢任何帮助!!

import java.util.zip.ZipFile
import java.util.zip.ZipEntry
import java.util.zip.ZipOutputStream

String zipFileFullPath = "C:/path/to/myzipfile/test.zip"

ZipFile zipFile = new ZipFile(zipFileFullPath) 
ZipEntry entry = zipFile.getEntry ( "someFile.txt" )

if(entry){
    InputStream input = zipFile.getInputStream(entry)
    BufferedReader br = new BufferedReader(new InputStreamReader(input, "UTF-8"))

    String s = null
    StringBuffer sb = new StringBuffer()

    while ((s=br.readLine())!=null){
         sb.append(s)
    }

    sb.append("adding some text..")


     ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileFullPath))
     out.putNextEntry(new ZipEntry("someFile.txt"));

     int length


     InputStream fin = new ByteArrayInputStream(sb.toString().getBytes("UTF8"))

     while((length = fin.read(sb)) > 0)
     {
            out.write(sb, 0, length)
     }             

     out.closeEntry()

}
2个回答

4

对@Opal的回答进行了一些微小的修改,我已经:

  • 在可能的情况下使用Groovy方法
  • 封装成一个方法

Groovy代码段

void updateZipEntry(String zipFile, String zipEntry, String newContent){
    def zin = new ZipFile(zipFile)
    def tmp = File.createTempFile("temp_${System.nanoTime()}", '.zip')
    tmp.withOutputStream { os ->
        def zos = new ZipOutputStream(os)
        zin.entries().each { entry ->
            def isReplaced = entry.name == zipEntry
            zos.putNextEntry(isReplaced ? new ZipEntry(zipEntry) : entry)
            zos << (isReplaced ? newContent.getBytes('UTF8') : zin.getInputStream(entry).bytes )
            zos.closeEntry()
        }
        zos.close()
    }
    zin.close()
    assert new File(zipFile).delete()
    tmp.renameTo(zipFile)
}

使用方法

updateZipEntry('/tmp/file.zip', 'META-INF/web.xml', '<foobar>new content!</foobar>')

3

具体是什么出了问题?有没有抛出任何异常?

据我所知,不可能在原地修改zip文件。以下脚本将重写文件,并且如果需要处理的条目被修改,则会进行修改。

import java.util.zip.*

def zipIn = new File('lol.zip')
def zip = new ZipFile(zipIn)
def zipTemp = File.createTempFile('out', 'zip')
zipTemp.deleteOnExit()
def zos = new ZipOutputStream(new FileOutputStream(zipTemp))
def toModify = 'lol.txt'

for(e in zip.entries()) {
    if(!e.name.equalsIgnoreCase(toModify)) {
        zos.putNextEntry(e)
        zos << zip.getInputStream(e).bytes
    } else {
        zos.putNextEntry(new ZipEntry(toModify))
        zos << 'lollol\n'.bytes
    }
    zos.closeEntry()
}

zos.close()
zipIn.delete()
zipTemp.renameTo(zipIn)

更新

我之前的说法不正确。可以直接修改zip文件,但是您的解决方案将会忽略其他已被压缩的文件。输出文件只包含一个文件 - 您想要修改的文件。我还猜测您的文件因为没有调用close()而导致损坏。

以下是稍微修改过的脚本(更加高效):

import java.util.zip.*

def zipFileFullPath = 'lol.zip'
def zipFile = new ZipFile(zipFileFullPath) 
def entry = zipFile.getEntry('lol.txt')

if(entry) {
   def input = zipFile.getInputStream(entry)
   def br = new BufferedReader(new InputStreamReader(input, 'UTF-8'))
   def sb = new StringBuffer()

   sb << br.text
   sb << 'adding some text..'

   def out = new ZipOutputStream(new FileOutputStream(zipFileFullPath))
   out.putNextEntry(new ZipEntry('lol.txt'))

   out << sb.toString().getBytes('UTF8')
   out.closeEntry()
   out.close()
}

谢谢你的帮助,Opal。你的第一个解决方案看起来很不错,但出于某种原因,什么也没有发生,zip文件似乎根本没有改变。 - user955732
压缩文件没有被删除。临时文件看起来没问题。 - user955732
我已在Mac OS上进行了测试。如果问题得到解决,请告诉我。我可以再次检查。 - Opal
zipIn.delete() 返回 false,不知道为什么无法删除此文件。 - user955732
为什么在调用delete()之前的代码仍然锁定了文件? - user955732
我看到你正在使用Windows操作系统,删除文件有问题。请参考以下链接:stackoverflow.com/questions/991489/i-cant-delete-a-file-in-java 或 stackoverflow.com/questions/13685592/... 或 stackoverflow.com/questions/16985453/...。关于这个问题,Stack Overflow上有很多相关的问题和答案,似乎与最初提出的问题不同。 - Opal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接