Created
July 20, 2018 14:56
-
-
Save trfiladelfo/cb6491eeab9e26c9419bd3834c07cd08 to your computer and use it in GitHub Desktop.
ZIP Android in Kotlin - Retrofit
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
override fun onResponse(call: Call<ResponseBody>?, response: Response<ResponseBody>?) { | |
val etag = headers["Etag"] | |
val body = response.body()!! | |
val location = App.storageDir(App.Storage.TEMP) | |
val zip = File(location, "$etag.zip") | |
/*if (!zip.exists()) { | |
zip.createNewFile() | |
} | |
var inputStream: InputStream? = null | |
var outputStream: OutputStream? = null | |
val fileSize = body.contentLength() | |
val fileReader = ByteArray(fileSize.toInt()) //ByteArray(4096) | |
var fileSizeDownloaded: Long = 0 | |
inputStream = body.byteStream() | |
outputStream = FileOutputStream(zip) | |
while (true) { | |
val read = inputStream.read(fileReader) | |
if (read == -1) { | |
break | |
} | |
outputStream.write(fileReader, 0, read) | |
fileSizeDownloaded += read.toLong() | |
} | |
outputStream.flush()*/ | |
//FileInputStream(zip).use { fin -> | |
//ZipInputStream(fin).use { zin -> | |
ZipInputStream(body.byteStream()).use { zin -> | |
var ze: ZipEntry? = zin.nextEntry | |
while (ze != null) { | |
if(ze.isDirectory) { | |
val f = File(location, ze.name) | |
if(!f.exists()) if(!f.isDirectory) f.mkdirs() | |
} else { | |
FileOutputStream(File(location, ze.name)).use { fout -> | |
val buffer = ByteArray(8192) | |
var len = zin.read(buffer) | |
while (len != -1) { | |
fout.write(buffer, 0, len) | |
len = zin.read(buffer) | |
} | |
zin.closeEntry() | |
} | |
} | |
ze = zin.nextEntry | |
} | |
} | |
//} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment