java.util.zip.zipexception invalid entry compressed size (expected 36586 but got 37005 bytes)

这篇博客讨论了在Gradle中遇到的一个问题,即在处理Zip文件时遇到的错误。原始代码尝试修改ZipEntry的注释,但在新代码中,创建了一个新的ZipEntry对象来实现这一目标。博主通过对比新旧代码,展示了如何更正问题,确保能正确设置ZipEntry的注释,并详细输出了判断和处理过程。这对于理解Gradle中操作Zip文件的方法和处理类似问题具有参考价值。

在gradle中使用的时候碰到了上边的错误,原本的代码如下

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
                println("isReplaced->"+isReplaced)
                if(isReplaced) {
                    entry.setComment(newContent)
                }
                zos.putNextEntry(entry)
                zos << (zin.getInputStream(entry).bytes)
                zos.closeEntry()
            }
            zos.close()
        }
        zin.close()

新代码如下

def tmp = File.createTempFile("temp_${System.nanoTime()}", '.zip')
        tmp.withOutputStream { os ->
            def zos = new ZipOutputStream(os)
            zin.entries().each { entry ->
                ZipEntry newZipEntry = new ZipEntry(entry.getName())
                def isReplaced = newZipEntry.name == zipEntry
                println("isReplaced->"+isReplaced)
                if(isReplaced) {
                    newZipEntry.setComment(newContent)
                }
                zos.putNextEntry(newZipEntry)
                zos << (zin.getInputStream(newZipEntry).bytes)
                zos.closeEntry()
            }
            zos.close()
        }
        zin.close()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值