handle the compressing functionality
This commit is contained in:
parent
1ef349384d
commit
c89fcb8692
|
@ -19,10 +19,7 @@ import com.simplemobiletools.commons.dialogs.ConfirmationDialog
|
|||
import com.simplemobiletools.commons.dialogs.FilePickerDialog
|
||||
import com.simplemobiletools.commons.dialogs.PropertiesDialog
|
||||
import com.simplemobiletools.commons.dialogs.RenameItemDialog
|
||||
import com.simplemobiletools.commons.extensions.formatSize
|
||||
import com.simplemobiletools.commons.extensions.getColoredDrawableWithColor
|
||||
import com.simplemobiletools.commons.extensions.getMimeTypeFromPath
|
||||
import com.simplemobiletools.commons.extensions.toast
|
||||
import com.simplemobiletools.commons.extensions.*
|
||||
import com.simplemobiletools.commons.models.FileDirItem
|
||||
import com.simplemobiletools.filemanager.R
|
||||
import com.simplemobiletools.filemanager.activities.SimpleActivity
|
||||
|
@ -215,17 +212,29 @@ class ItemsAdapter(val activity: SimpleActivity, var mItems: MutableList<FileDir
|
|||
val firstPath = mItems[selectedPositions.first()].path
|
||||
CompressAsDialog(activity, firstPath) {
|
||||
activity.handleSAFDialog(File(firstPath)) {
|
||||
activity.toast(R.string.compressing)
|
||||
val paths = selectedPositions.map { mItems[it].path }.toTypedArray()
|
||||
compress(paths, it)
|
||||
Thread({
|
||||
if (compress(paths, it)) {
|
||||
activity.toast(R.string.compression_successful)
|
||||
activity.runOnUiThread {
|
||||
listener?.refreshItems()
|
||||
actMode?.finish()
|
||||
}
|
||||
} else {
|
||||
activity.toast(R.string.compressing_failed)
|
||||
}
|
||||
}).start()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun compress(paths: Array<String>, targetPath: String) {
|
||||
private fun compress(paths: Array<String>, targetPath: String): Boolean {
|
||||
val BUFFER_SIZE = 8192
|
||||
var origin: BufferedInputStream?
|
||||
val out = ZipOutputStream(BufferedOutputStream(FileOutputStream(targetPath)))
|
||||
var out: ZipOutputStream? = null
|
||||
try {
|
||||
out = ZipOutputStream(BufferedOutputStream(FileOutputStream(targetPath)))
|
||||
var origin: BufferedInputStream?
|
||||
val data = ByteArray(BUFFER_SIZE)
|
||||
|
||||
for (i in paths.indices) {
|
||||
|
@ -243,9 +252,13 @@ class ItemsAdapter(val activity: SimpleActivity, var mItems: MutableList<FileDir
|
|||
origin.close()
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
activity.showErrorToast(e.toString())
|
||||
return false
|
||||
} finally {
|
||||
out.close()
|
||||
out?.close()
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
fun selectAll() {
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
package com.simplemobiletools.filemanager.dialogs
|
||||
|
||||
import android.support.v7.app.AlertDialog
|
||||
import android.view.View
|
||||
import android.view.WindowManager
|
||||
import com.simplemobiletools.commons.extensions.getFilenameFromPath
|
||||
import com.simplemobiletools.commons.extensions.setupDialogStuff
|
||||
import com.simplemobiletools.commons.extensions.*
|
||||
import com.simplemobiletools.filemanager.R
|
||||
import com.simplemobiletools.filemanager.activities.SimpleActivity
|
||||
import kotlinx.android.synthetic.main.dialog_compress_as.view.*
|
||||
|
@ -13,8 +13,9 @@ class CompressAsDialog(val activity: SimpleActivity, val path: String, val callb
|
|||
private val view = activity.layoutInflater.inflate(R.layout.dialog_compress_as, null)
|
||||
|
||||
init {
|
||||
val file = File(path)
|
||||
val filename = path.getFilenameFromPath()
|
||||
val indexOfDot = if (filename.contains('.') && File(path).isFile) filename.lastIndexOf(".") else filename.length
|
||||
val indexOfDot = if (filename.contains('.') && file.isFile) filename.lastIndexOf(".") else filename.length
|
||||
val baseFilename = filename.substring(0, indexOfDot)
|
||||
view.file_name.setText(baseFilename)
|
||||
|
||||
|
@ -24,8 +25,22 @@ class CompressAsDialog(val activity: SimpleActivity, val path: String, val callb
|
|||
.create().apply {
|
||||
activity.setupDialogStuff(view, this, R.string.compress_as)
|
||||
window!!.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
|
||||
getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener({
|
||||
dismiss()
|
||||
getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(View.OnClickListener {
|
||||
val name = view.file_name.value
|
||||
when {
|
||||
name.isEmpty() -> activity.toast(R.string.empty_name)
|
||||
name.isAValidFilename() -> {
|
||||
val newFile = File(file.parent, "$name.zip")
|
||||
if (newFile.exists()) {
|
||||
activity.toast(R.string.name_taken)
|
||||
return@OnClickListener
|
||||
}
|
||||
|
||||
dismiss()
|
||||
callback(newFile.absolutePath)
|
||||
}
|
||||
else -> activity.toast(R.string.invalid_name)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue