properly handle folder traversing inside a zip file

This commit is contained in:
tibbi 2020-05-28 11:09:31 +02:00
parent a89aa0aa0c
commit 031904283f
3 changed files with 29 additions and 10 deletions

View file

@ -20,24 +20,32 @@ import java.util.zip.ZipInputStream
class DecompressActivity : SimpleActivity() {
private val allFiles = ArrayList<ListItem>()
private var currentPath = ""
private var uri: Uri? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_decompress)
val uri = intent.data
uri = intent.data
if (uri == null) {
toast(R.string.unknown_error_occurred)
return
}
val realPath = getRealPathFromURI(uri)
val realPath = getRealPathFromURI(uri!!)
title = realPath?.getFilenameFromPath() ?: uri.toString().getFilenameFromPath()
fillAllListItems(uri!!)
updateCurrentPath("")
}
private fun updateCurrentPath(path: String) {
currentPath = path
try {
fillAllListItems(uri)
val listItems = getFolderItems("")
val listItems = getFolderItems(currentPath)
DecompressItemsAdapter(this, listItems, decompress_list) {
if ((it as ListItem).isDirectory) {
updateCurrentPath(it.path)
}
}.apply {
decompress_list.adapter = this
}
@ -60,8 +68,17 @@ class DecompressActivity : SimpleActivity() {
return true
}
override fun onBackPressed() {
if (currentPath.isEmpty()) {
super.onBackPressed()
} else {
val newPath = if (currentPath.contains("/")) currentPath.getParentPath() else ""
updateCurrentPath(newPath)
}
}
private fun decompressFiles() {
val defaultFolder = getRealPathFromURI(intent.data!!) ?: internalStoragePath
val defaultFolder = getRealPathFromURI(uri!!) ?: internalStoragePath
FilePickerDialog(this, defaultFolder, false, config.showHidden, true, true) { destination ->
handleSAFDialog(destination) {
if (it) {
@ -75,7 +92,7 @@ class DecompressActivity : SimpleActivity() {
private fun decompressTo(destination: String) {
try {
val inputStream = contentResolver.openInputStream(intent.data!!)
val inputStream = contentResolver.openInputStream(uri!!)
val zipInputStream = ZipInputStream(BufferedInputStream(inputStream!!))
val buffer = ByteArray(1024)
@ -114,7 +131,7 @@ class DecompressActivity : SimpleActivity() {
}
fileParent == parent
}.toMutableList() as ArrayList<ListItem>
}.sortedWith(compareBy({ !it.isDirectory }, { it.mName })).toMutableList() as ArrayList<ListItem>
}
@SuppressLint("NewApi")
@ -130,7 +147,8 @@ class DecompressActivity : SimpleActivity() {
}
val lastModified = if (isOreoPlus()) zipEntry.lastModifiedTime.toMillis() else 0
val listItem = ListItem(zipEntry.name.removeSuffix("/"), zipEntry.name.removeSuffix("/").getFilenameFromPath(), zipEntry.isDirectory, 0, 0L, lastModified, false)
val filename = zipEntry.name.removeSuffix("/")
val listItem = ListItem(filename, filename.getFilenameFromPath(), zipEntry.isDirectory, 0, 0L, lastModified, false)
allFiles.add(listItem)
}
}

View file

@ -65,7 +65,7 @@ class DecompressItemsAdapter(activity: SimpleActivity, var listItems: MutableLis
override fun onBindViewHolder(holder: MyRecyclerViewAdapter.ViewHolder, position: Int) {
val fileDirItem = listItems[position]
holder.bindView(fileDirItem, false, false) { itemView, layoutPosition ->
holder.bindView(fileDirItem, true, false) { itemView, layoutPosition ->
setupView(itemView, fileDirItem)
}
bindViewHolder(holder)

View file

@ -4,6 +4,7 @@
android:id="@+id/item_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:paddingEnd="@dimen/activity_margin">
<ImageView