java.util.zip.ZipException: attempt to write past end of STORED entry

    private def saveJarEntry(JarOutputStream jarOutputStream, File saveFile, JarEntry jarEntry) {
        jarOutputStream.putNextEntry(jarEntry)
        def inputStream = new FileInputStream(saveFile)
        def buffer = new byte[8192]
        def readSize = -1
        while ((readSize = inputStream.read(buffer)) != -1) {
            jarOutputStream.write(buffer, 0, readSize)
        }
        jarOutputStream.flush()
        jarOutputStream.closeEntry()
        inputStream.close()
    }

此段代码报错:Execution failed for task ':storage:modifyRParentTask'.
> java.util.zip.ZipException: attempt to write past end of STORED entry

原因:不应使用已有的JarEntry,应该新建一个JarEntry用作写入。

    private def saveJarEntry(JarOutputStream jarOutputStream, File saveFile, JarEntry jarEntry) {
        JarEntry newEntry = new JarEntry(jarEntry.name)
        jarOutputStream.putNextEntry(jarEntry)
        def inputStream = new FileInputStream(saveFile)
        def buffer = new byte[8192]
        def readSize = -1
        while ((readSize = inputStream.read(buffer)) != -1) {
            jarOutputStream.write(buffer, 0, readSize)
        }
        jarOutputStream.flush()
        jarOutputStream.closeEntry()
        inputStream.close()
    }

你可能感兴趣的:(android,android)