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

View File

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

View File

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