Skip to content

Commit

Permalink
feat: attempt to fix crash while reading components assets
Browse files Browse the repository at this point in the history
  • Loading branch information
jahirfiquitiva committed Jan 5, 2024
1 parent fba7f7d commit 155c599
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
package dev.jahir.kuper.data.viewmodels

import android.app.Application
import android.os.Build
import android.util.Log
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import dalvik.system.ZipPathValidator
import dev.jahir.frames.extensions.resources.createIfDidNotExist
import dev.jahir.frames.extensions.resources.deleteEverything
import dev.jahir.frames.extensions.resources.hasContent
Expand Down Expand Up @@ -130,12 +132,13 @@ class ComponentsViewModel(application: Application) : AndroidViewModel(applicati
ins.close()

if (file.exists() && file.length() > 0) {
val zipFile = try {
ZipFile(file)
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
ZipPathValidator.clearCallback()
}
} catch (_: Exception) {
null
}
if (zipFile !== null) {
ZipFile(file).let { zipFile ->
val entries = zipFile.entries()
while (entries.hasMoreElements()) {
val entry = entries.nextElement()
Expand Down
29 changes: 18 additions & 11 deletions library/src/main/kotlin/dev/jahir/kuper/extensions/File.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ import java.util.zip.ZipFile

private fun InputStream.copyFilesTo(os: OutputStream) {
try {
val buffer = ByteArray(2048)
var readInt = 0
while ({ readInt = read(buffer);readInt }() != -1) os.write(buffer, 0, readInt)
} catch (e: Exception) {
val buffer = ByteArray(4096)
var bytes = 0
while (read(buffer).also { bytes = it } != -1)
os.write(buffer, 0, bytes)
} catch (_: Exception) {
} finally {
os.flush()
os.close()
try {
os.flush()
os.close()
} catch (_: Exception) {
}
}
}

Expand All @@ -27,10 +31,13 @@ fun ZipFile.copyFromTo(from: ZipEntry, to: File?) {
zipOut = FileOutputStream(to)
zipIn = getInputStream(from)
zipIn.copyFilesTo(zipOut)
} catch (e: Exception) {
} catch (_: Exception) {
} finally {
zipIn?.close()
zipOut?.flush()
zipOut?.close()
try {
zipIn?.close()
zipOut?.flush()
zipOut?.close()
} catch (_: Exception) {
}
}
}
}

0 comments on commit 155c599

Please sign in to comment.