修改zipfile条目的文件内容

时间:2014-10-04 10:26:18

标签: groovy zipfile

我想更新位于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()

}

2 个答案:

答案 0 :(得分:4)

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

  • 尽可能使用groovy方法
  • 打包方法

Groovy Snippet

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

答案 1 :(得分: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文件 in situ ,但您的解决方案将省略其他压缩文件。输出文件将只包含一个文件 - 您要修改的文件。我还假设您的文件已损坏,因为未在close()上调用out

下面是你稍微修改过的脚本(更加更加流畅):

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