Created
April 20, 2019 01:19
-
-
Save kota1921/ad08437b97cfa97d441d85c189040cb3 to your computer and use it in GitHub Desktop.
Create Gzip Archive Android kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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