Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Kotlin gzip解压缩失败_Java_Android_Kotlin_Gzip - Fatal编程技术网

Java Kotlin gzip解压缩失败

Java Kotlin gzip解压缩失败,java,android,kotlin,gzip,Java,Android,Kotlin,Gzip,我试图将java gzip解压缩代码简化为kotlin。但我换了衣服后,它也坏了 下面是java代码 公共静态字节[]解压缩(字节[]压缩字节){ if(null==compressedBytes | | compressedBytes.length==0){ 返回null; } ByteArrayOutputStream out=null; ByteArrayInputStream in=null; gzip输入流gzip输入流=null; 试一试{ out=新的ByteArrayOutput

我试图将java gzip解压缩代码简化为kotlin。但我换了衣服后,它也坏了

下面是java代码

公共静态字节[]解压缩(字节[]压缩字节){
if(null==compressedBytes | | compressedBytes.length==0){
返回null;
}
ByteArrayOutputStream out=null;
ByteArrayInputStream in=null;
gzip输入流gzip输入流=null;
试一试{
out=新的ByteArrayOutputStream();
in=新的字节数输入流(压缩字节);
gzip输入流=新的gzip输入流(in);
字节[]缓冲区=新字节[256];
int n=0;
而((n=gzipInputStream.read(buffer))>=0){
out.write(缓冲区,0,n);
}
return out.toByteArray();
}捕获(IOException忽略){
}最后{
CloseableUtils.closequilly(gzip输入流);
可关闭的utils.安静地关闭(in);
安静地关上;
}
返回null;
}
这是我的kotlin代码

payload=gzip输入流(payload.inputStream())
.bufferedReader()
.使用{it.readText()}
.toByteArray()文件
我犯了这个错误

com.google.protobuf.nano.InvalidProtocolBufferNanoException: While parsing a protocol message, the input ended unexpectedly in the middle of a field.  This could mean either than the input has been truncated or that an embedded message misreported its own length.
解压过程似乎被读卡器中断了?

将字节解码为UTF-8字符集,这就是为什么它说“这可能意味着输入被截断”,它可能试图将8位转换为字符,并从中生成字符串

使用获取
ByteArray
,它与JVM平台中的
byte[]
表示相同

例如:

gzip输入流(payload.inputStream())
.bufferedReader()
.使用{it.readBytes()}
编辑: 对于读取字节,您不应该使用
读取器
,它用于读取Kotlin的
InputStream.bufferedReader
中定义的UTF-8格式的文本:

public inlinefun InputStream.bufferedReader(charset:charset=charset.UTF_8):bufferedReader=reader(charset.buffered())
将以8KB的缓冲区读取字节

public fun InputStream.readBytes():ByteArray{
val buffer=ByteArrayOutputStream(maxOf(默认缓冲区大小,this.available())
复制到(缓冲区)
返回buffer.toByteArray()
}
//这将自动使用8KB缓冲区进行复制
//默认缓冲区大小=8*1024
public fun InputStream.copyTo(out:OutputStream,bufferSize:Int=DEFAULT\u BUFFER\u SIZE):长{
var bytescomped:Long=0
val buffer=字节数组(bufferSize)
变量字节=读取(缓冲区)
而(字节>=0){
out.write(缓冲区,0,字节)
字节复制+=字节
字节=读取(缓冲区)
}
返回字节复制
}
所以你只需要做:

gzip输入流(payload.inputStream())。使用{it.readBytes()}

使用以下功能:

    fun File.unzip(unzipLocationRoot: File? = null) {

    val rootFolder = unzipLocationRoot
        ?: File(parentFile.absolutePath + File.separator + nameWithoutExtension)
    if (!rootFolder.exists()) {
        rootFolder.mkdirs()
    }

    ZipFile(this).use { zip ->
        zip
            .entries()
            .asSequence()
            .map {
                val outputFile = File(rootFolder.absolutePath + File.separator + it.name)
                ZipIO(it, outputFile)
            }
            .map {
                it.output.parentFile?.run {
                    if (!exists()) mkdirs()
                }
                it
            }
            .filter { !it.entry.isDirectory }
            .forEach { (entry, output) ->
                zip.getInputStream(entry).use { input ->
                    output.outputStream().use { output ->
                        input.copyTo(output)
                    }
                }
            }
    }

}
将文件作为参数传递,如下所示:

val zipFile = File(your file directory, your file name)
            zipFile.unzip()

希望这会有帮助是的,你是对的,但是“BufferReader”sames没有“readBytes”方法。@Aquarids ah被记录为
从字符输入流中读取文本,缓冲字符,以便有效读取字符、数组和行。
它实际上是为了读取字符而定义的,你应该在没有读卡器的情况下加载它。检查更新。