ZipInputSream在文件末尾写[] [] [] [] [] [] [] [] [] [] ....?

时间:2014-09-02 09:34:45

标签: java zip inputstream

在Eclipse中,我试图在每个文件的末尾显示zip文件的内容是[] [] [] [] [] [] [] [] [] []。有人可以解释一下吗?

def uploadTheme(String themeName, def inputStream) {

    def themeNameUp = themeName.toUpperCase()
    def themeKey = this.prefix + themeNameUp

    if (redisService.exists(themeKey) && inputStream) {
        ZipInputStream zipFile = new ZipInputStream(inputStream)
        ZipEntry zipEntry = zipFile.nextEntry

        while (zipEntry) {
            def key = zipEntry.name

            if (key.startsWith(this.widgetPrefix) || key.startsWith(this.templatePrefix)) {
                byte[] data = new byte[4096]

                StringBuffer zipContent = new StringBuffer()
                int length = zipFile.read(data, 0, data.size())                                     

                while (length != -1) {
                    zipContent << new String(data)

                    length = zipFile.read(data, 0, data.size())
                }

                if (key.endsWith(":data")) {
                    def prevData = redisService.hget(themeKey, key)

                    if (prevData) {
                        historyService.addHistory(themeNameUp + ":" + key[0..-6], prevData)
                    }
                }

                redisService.hset(themeKey, key, zipContent.toString())
            }

            zipFile.closeEntry()
            zipEntry = zipFile.nextEntry
        }

        zipFile.close()
    }

1 个答案:

答案 0 :(得分:0)

zipContent << new String(data)

应该是

zipContent << new String(data, 0, length)

长度是实际读取的数量。最后长度小于数据的大小。

请注意,创建的字符串:

  • 使用默认平台编码,因为您没有为编码传递额外参数,
  • 仅适用于单字节编码,因为您传递任意剪切字节序列。