Skip to content

Commit 155c599

Browse files
feat: attempt to fix crash while reading components assets
1 parent fba7f7d commit 155c599

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

library/src/main/kotlin/dev/jahir/kuper/data/viewmodels/ComponentsViewModel.kt

+7-4
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
package dev.jahir.kuper.data.viewmodels
44

55
import android.app.Application
6+
import android.os.Build
67
import android.util.Log
78
import androidx.lifecycle.AndroidViewModel
89
import androidx.lifecycle.LifecycleOwner
910
import androidx.lifecycle.MutableLiveData
1011
import androidx.lifecycle.viewModelScope
12+
import dalvik.system.ZipPathValidator
1113
import dev.jahir.frames.extensions.resources.createIfDidNotExist
1214
import dev.jahir.frames.extensions.resources.deleteEverything
1315
import dev.jahir.frames.extensions.resources.hasContent
@@ -130,12 +132,13 @@ class ComponentsViewModel(application: Application) : AndroidViewModel(applicati
130132
ins.close()
131133

132134
if (file.exists() && file.length() > 0) {
133-
val zipFile = try {
134-
ZipFile(file)
135+
try {
136+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
137+
ZipPathValidator.clearCallback()
138+
}
135139
} catch (_: Exception) {
136-
null
137140
}
138-
if (zipFile !== null) {
141+
ZipFile(file).let { zipFile ->
139142
val entries = zipFile.entries()
140143
while (entries.hasMoreElements()) {
141144
val entry = entries.nextElement()

library/src/main/kotlin/dev/jahir/kuper/extensions/File.kt

+18-11
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,17 @@ import java.util.zip.ZipFile
99

1010
private fun InputStream.copyFilesTo(os: OutputStream) {
1111
try {
12-
val buffer = ByteArray(2048)
13-
var readInt = 0
14-
while ({ readInt = read(buffer);readInt }() != -1) os.write(buffer, 0, readInt)
15-
} catch (e: Exception) {
12+
val buffer = ByteArray(4096)
13+
var bytes = 0
14+
while (read(buffer).also { bytes = it } != -1)
15+
os.write(buffer, 0, bytes)
16+
} catch (_: Exception) {
1617
} finally {
17-
os.flush()
18-
os.close()
18+
try {
19+
os.flush()
20+
os.close()
21+
} catch (_: Exception) {
22+
}
1923
}
2024
}
2125

@@ -27,10 +31,13 @@ fun ZipFile.copyFromTo(from: ZipEntry, to: File?) {
2731
zipOut = FileOutputStream(to)
2832
zipIn = getInputStream(from)
2933
zipIn.copyFilesTo(zipOut)
30-
} catch (e: Exception) {
34+
} catch (_: Exception) {
3135
} finally {
32-
zipIn?.close()
33-
zipOut?.flush()
34-
zipOut?.close()
36+
try {
37+
zipIn?.close()
38+
zipOut?.flush()
39+
zipOut?.close()
40+
} catch (_: Exception) {
41+
}
3542
}
36-
}
43+
}

0 commit comments

Comments
 (0)