Android DeCompress String giving java.util.zip.ZipException: Not in GZIP format

68 Views Asked by At

I am compressing my large string with below approach and i am geeting "java.util.zip.ZipException: Not in GZIP format" exception while decompress my string. please help me out.

   @kotlin.Throws(IOException::class)
    fun compress(data: String): String? {
        val charset = StandardCharsets.UTF_16
        val bos = ByteArrayOutputStream(data.length)
        val gzip = GZIPOutputStream(bos)
        gzip.write(data.toByteArray())
        gzip.close()
        val compressed = bos.toByteArray()
        bos.close()
        return String(compressed, charset)
    }

    @kotlin.Throws(IOException::class)
    fun decompress(compressed: String?): String {

        val charset: Charset = StandardCharsets.UTF_16
        val bytes = compressed!!.toByteArray(charset)
        val bis = ByteArrayInputStream(bytes)
        val gis = GZIPInputStream(bis)
        val br = BufferedReader(InputStreamReader(gis, "UTF-8"))
        val sb = StringBuilder()
        var line: String?
        while (br.readLine().also { line = it } != null) {
            sb.append(line)
        }
        br.close()
        gis.close()
        bis.close()
        return sb.toString()
    } 
0

There are 0 best solutions below