Last active
September 21, 2018 13:56
-
-
Save thuytrinh/cb79bc9e156d0cdf1def13aa1aab44ef to your computer and use it in GitHub Desktop.
A sample usage of GzipSink & GzipSource from Okio
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
import okio.GzipSink | |
import okio.GzipSource | |
import okio.Okio | |
import org.assertj.core.api.Assertions.assertThat | |
import org.junit.Test | |
import java.io.File | |
class OkioTest { | |
@Test | |
fun `should compress file properly`() { | |
val zipFile = File("(ʘ‿ʘ).gz") | |
// Write & zip the content to the file. | |
Okio.sink(zipFile).use { fileSink -> | |
Okio.buffer(GzipSink(fileSink)).use { bufferedSink -> | |
bufferedSink.writeUtf8("。◕‿◕。\n") | |
bufferedSink.writeUtf8("ಠoಠ\n") | |
bufferedSink.writeUtf8("♥‿♥\n") | |
} | |
} | |
// Decompress the file and read its content. | |
val content: String? = GzipSource(Okio.source(zipFile)).use { fileSource -> | |
Okio.buffer(fileSource).use { bufferedSource -> bufferedSource.readUtf8() } | |
} | |
assertThat(content).isEqualTo("。◕‿◕。\n" + "ಠoಠ\n" + "♥‿♥\n") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment