Skip to content

Instantly share code, notes, and snippets.

@kota1921
Created April 20, 2019 01:19
Show Gist options
  • Save kota1921/ad08437b97cfa97d441d85c189040cb3 to your computer and use it in GitHub Desktop.
Save kota1921/ad08437b97cfa97d441d85c189040cb3 to your computer and use it in GitHub Desktop.
Create Gzip Archive Android kotlin
//this is zip function for new standards, if you don't
private fun zipFromRaw(rawTextId: Int, resultFileName: String) {
val inputStream = context.resources.openRawResource(rawTextId)
val stringBuilder = StringBuilder()
try {
val reader = BufferedReader(InputStreamReader(inputStream))
var line = reader.readLine()
while (line != null) {
stringBuilder.append(line)
line = reader.readLine()
}
} catch (e: Throwable) {
Timber.e(e)
stringBuilder.clear()
return
} finally {
inputStream.close()
}
val file = File(context.filesDir, resultFileName)
val fileOutputStream = FileOutputStream(file)
val gzip = GZIPOutputStream(fileOutputStream)
val sw = OutputStreamWriter(gzip)
sw.write(stringBuilder.toString())
sw.close()
gzip.close()
fileOutputStream.close()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment