How to read file from ZIP using InputStream?

后端 未结 7 849
旧巷少年郎
旧巷少年郎 2020-11-29 09:11

I must get file content from ZIP archive (only one file, I know its name) using SFTP. The only thing I\'m having is ZIP\'s InputStream. Most examples show how g

7条回答
  •  情歌与酒
    2020-11-29 09:51

    If content of your ZIP consist of 1 file (for example, zipped content of HTTP response), you can read text content using Kotlin as follows:

    @Throws(IOException::class)
    fun InputStream.readZippedContent() = ZipInputStream(this).use { stream ->
         stream.nextEntry?.let { stream.bufferedReader().readText() } ?: String()
    }
    

    This extension function unzips first ZIP entry of Zip file and read content as plain text.

    Usage:

    val inputStream: InputStream = ... // your zipped InputStream
    val textContent = inputStream.readZippedContent()
    

提交回复
热议问题