package com.example.vastlib.utils

import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.util.zip.GZIPInputStream
import java.util.zip.GZIPOutputStream

object Gzip {
    fun ByteArray.compress(): ByteArray {
        val out = ByteArrayOutputStream()
        val gzip = GZIPOutputStream(out)
        return try {
            gzip.use {
                it.write(this)
            }
            out.use {
                it.toByteArray()
            }
        } catch (e: Exception) {
           byteArrayOf()
        }
    }

    fun ByteArray.unCompress(): ByteArray {
        val out = ByteArrayOutputStream()
        val inputStream = ByteArrayInputStream(this)
        val gzipInputStream = GZIPInputStream(inputStream)
        return try {
            gzipInputStream.use { gzip ->
                out.use {
                    it.write(gzip.readBytes())
                    it.toByteArray()
                }
            }
        } catch (e: Exception) {
            byteArrayOf()
        }
    }
}