fix decompressing folders on sd cards

This commit is contained in:
tibbi 2017-09-03 10:23:32 +02:00
parent 21b8202fbc
commit ce63adf6f1

View file

@ -241,6 +241,7 @@ class ItemsAdapter(val activity: SimpleActivity, var mItems: MutableList<FileDir
activity.handleSAFDialog(File(firstPath)) { activity.handleSAFDialog(File(firstPath)) {
activity.toast(R.string.decompressing) activity.toast(R.string.decompressing)
val paths = selectedPositions.map { mItems[it].path }.filter { it.isZipFile() } val paths = selectedPositions.map { mItems[it].path }.filter { it.isZipFile() }
Thread({
if (unzipPaths(paths)) { if (unzipPaths(paths)) {
activity.toast(R.string.decompression_successful) activity.toast(R.string.decompression_successful)
activity.runOnUiThread { activity.runOnUiThread {
@ -250,6 +251,7 @@ class ItemsAdapter(val activity: SimpleActivity, var mItems: MutableList<FileDir
} else { } else {
activity.toast(R.string.decompressing_failed) activity.toast(R.string.decompressing_failed)
} }
}).start()
} }
} }
@ -263,17 +265,12 @@ class ItemsAdapter(val activity: SimpleActivity, var mItems: MutableList<FileDir
val entry = entries.nextElement() val entry = entries.nextElement()
val file = File(it.parent, entry.name) val file = File(it.parent, entry.name)
if (entry.isDirectory) { if (entry.isDirectory) {
createDirectory(file) { if (!createDirectorySync(file)) {
val error = String.format(activity.getString(R.string.could_not_create_file), file.absolutePath) val error = String.format(activity.getString(R.string.could_not_create_file), file.absolutePath)
activity.showErrorToast(error) activity.showErrorToast(error)
return false
} }
} else { } else {
createDirectory(file.parentFile) {
if (!it) {
val error = String.format(activity.getString(R.string.could_not_create_file), file.parentFile.absolutePath)
activity.showErrorToast(error)
}
}
val ins = zipFile.getInputStream(entry) val ins = zipFile.getInputStream(entry)
ins.use { ins.use {
ins.copyTo(getFileOutputStream(file.absolutePath, file.getMimeType())) ins.copyTo(getFileOutputStream(file.absolutePath, file.getMimeType()))
@ -288,20 +285,13 @@ class ItemsAdapter(val activity: SimpleActivity, var mItems: MutableList<FileDir
return true return true
} }
private fun createDirectory(file: File, callback: (Boolean) -> Unit) { private fun createDirectorySync(file: File): Boolean {
if (activity.needsStupidWritePermissions(file.absolutePath)) { if (activity.needsStupidWritePermissions(file.absolutePath)) {
activity.handleSAFDialog(file) { val documentFile = activity.getFileDocument(file.absolutePath) ?: return false
val documentFile = activity.getFileDocument(file.absolutePath)
if (documentFile == null) {
callback(false)
return@handleSAFDialog
}
val newDir = documentFile.createDirectory(file.name) val newDir = documentFile.createDirectory(file.name)
callback(newDir != null) return newDir != null
}
} else {
callback(file.mkdirs())
} }
return file.mkdirs()
} }
fun zipPaths(sourcePaths: List<String>, targetPath: String): Boolean { fun zipPaths(sourcePaths: List<String>, targetPath: String): Boolean {