【问题标题】:Modifying the file contents of a zipfile entry修改 zipfile 条目的文件内容
【发布时间】:2014-10-04 10:26:17
【问题描述】:

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

我不知道怎么做,下面的代码也不能正常工作。

感谢您的帮助!

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()

}

【问题讨论】:

    标签: groovy zipfile


    【解决方案1】:

    只是对@Opal 的回答稍作修改,我刚刚:

    • 尽可能使用常规方法
    • 封装在一个方法中

    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>')
    

    【讨论】:

      【解决方案2】:

      究竟是什么不工作?有没有抛出异常?

      据我所知,原位修改 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 文件,但您的解决方案将省略其他已压缩的文件。输出文件将只包含一个文件 - 您要修改的文件。我还假设您的文件已损坏,因为没有在 out 上调用 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()
      }
      

      【讨论】:

      • 感谢您的帮助欧泊。您的第一个解决方案看起来不错,但由于某种原因没有任何反应,zipfile 似乎根本没有更改。
      • 压缩文件不会被删除。临时文件看起来没问题。
      • 我已经在 Mac OS 上进行了测试。请让我知道它是否解决了问题。我可能会再检查一次。
      • zipIn.delete()返回false,不知道为什么不能删除这个文件。
      • 在调用 delete() 之前,文件仍然被代码锁定。为什么?
      猜你喜欢
      • 2010-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-08
      • 2016-08-16
      • 1970-01-01
      • 2012-11-24
      • 1970-01-01
      相关资源
      最近更新 更多