Groovy 修改zipfile条目的文件内容
我想更新zipfile中文本文件的内容 我无法找到如何做到这一点,下面的代码工作不正常 谢谢你的帮助Groovy 修改zipfile条目的文件内容,groovy,zipfile,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(zipFileFullPat
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()
}
到底是什么不起作用?是否引发了任何异常 据我所知,不可能在原地修改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)
对@Opal的答案稍作修改,我只是:
- 尽可能使用groovy方法
- 打包在方法中
void updateZipEntry(字符串zipFile、字符串zipEntry、字符串newContent){
def zin=新ZipFile(ZipFile)
def tmp=File.createTempFile(“temp_${System.nanoTime()},'.zip'))
tmp.withOutputStream{os->
def zos=新ZipoutStream(操作系统)
zin.entries()。每个{entry->
def isReplaced=entry.name==zipEntry
zos.Putnextry(被替换?新ZipEntry(ZipEntry):条目)
zos感谢您对Opal的帮助。您的第一个解决方案看起来很棒,但由于某些原因,没有发生任何变化,zipfile似乎根本没有被更改。zip文件没有被删除。临时文件看起来不错。我已经在Mac OS上进行了测试。请让我知道它是否解决了问题。我可以再次检查。zipIn.delete()返回false,不知道为什么无法删除此文件。在调用delete()之前,该文件仍被代码锁定。为什么?
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()
}
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>')