updating the code compression code

This commit is contained in:
tibbi 2017-09-02 17:47:21 +02:00
parent 026283f33d
commit 5a0f6adff8
1 changed files with 37 additions and 55 deletions

View File

@ -26,11 +26,15 @@ import com.simplemobiletools.filemanager.activities.SimpleActivity
import com.simplemobiletools.filemanager.dialogs.CompressAsDialog import com.simplemobiletools.filemanager.dialogs.CompressAsDialog
import com.simplemobiletools.filemanager.extensions.config import com.simplemobiletools.filemanager.extensions.config
import kotlinx.android.synthetic.main.list_item.view.* import kotlinx.android.synthetic.main.list_item.view.*
import java.io.* import java.io.Closeable
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import java.util.* import java.util.*
import java.util.zip.ZipEntry import java.util.zip.ZipEntry
import java.util.zip.ZipOutputStream import java.util.zip.ZipOutputStream
class ItemsAdapter(val activity: SimpleActivity, var mItems: MutableList<FileDirItem>, val listener: ItemOperationsListener?, val itemClick: (FileDirItem) -> Unit) : class ItemsAdapter(val activity: SimpleActivity, var mItems: MutableList<FileDirItem>, val listener: ItemOperationsListener?, val itemClick: (FileDirItem) -> Unit) :
RecyclerView.Adapter<ItemsAdapter.ViewHolder>() { RecyclerView.Adapter<ItemsAdapter.ViewHolder>() {
private val BUFFER = 2048 private val BUFFER = 2048
@ -215,7 +219,7 @@ class ItemsAdapter(val activity: SimpleActivity, var mItems: MutableList<FileDir
activity.toast(R.string.compressing) activity.toast(R.string.compressing)
val paths = selectedPositions.map { mItems[it].path } val paths = selectedPositions.map { mItems[it].path }
Thread({ Thread({
if (zipFileAtPath(paths, it)) { if (zipPaths(paths, it)) {
activity.toast(R.string.compression_successful) activity.toast(R.string.compression_successful)
activity.runOnUiThread { activity.runOnUiThread {
listener?.refreshItems() listener?.refreshItems()
@ -229,72 +233,50 @@ class ItemsAdapter(val activity: SimpleActivity, var mItems: MutableList<FileDir
} }
} }
private fun zipFileAtPath(sourcePaths: List<String>, targetPath: String): Boolean { fun zipPaths(sourcePaths: List<String>, targetPath: String): Boolean {
var out: ZipOutputStream? = null val queue = LinkedList<File>()
try { val out = FileOutputStream(File(targetPath))
val fos = FileOutputStream(targetPath) val zout = ZipOutputStream(out)
out = ZipOutputStream(BufferedOutputStream(fos)) var res: Closeable = out
try {
sourcePaths.forEach { sourcePaths.forEach {
val sourceFile = File(it) val base = File(it).toURI()
if (sourceFile.isDirectory) { var mainFile = File(it)
if (!zipSubFolder(out!!, sourceFile)) { queue.push(mainFile)
return false res = zout
while (!queue.isEmpty()) {
mainFile = queue.pop()
if (mainFile.isDirectory) {
for (file in mainFile.listFiles()) {
var name = base.relativize(file.toURI()).path
if (file.isDirectory) {
queue.push(file)
name = if (name.endsWith("/")) name else "$name/"
zout.putNextEntry(ZipEntry(name))
} else {
zout.putNextEntry(ZipEntry(name))
FileInputStream(file).copyTo(zout)
zout.closeEntry()
}
}
} else {
val name = if (base.path == it) it.getFilenameFromPath() else base.relativize(mainFile.toURI()).path
zout.putNextEntry(ZipEntry(name))
FileInputStream(mainFile).copyTo(zout)
zout.closeEntry()
} }
} else {
val entry = ZipEntry(it.getFilenameFromPath())
val fis = FileInputStream(it)
addZipEntry(entry, fis, out!!)
} }
} }
} catch (e: Exception) { } catch (e: Exception) {
activity.showErrorToast(e.toString()) activity.showErrorToast(e.toString())
return false return false
} finally { } finally {
try { res.close()
out?.close()
} catch (e: Exception) {
activity.showErrorToast(e.toString())
}
} }
return true return true
} }
private fun zipSubFolder(out: ZipOutputStream, folder: File): Boolean {
try {
val fileList = folder.listFiles() ?: return true
for (file in fileList) {
if (file.isDirectory) {
zipSubFolder(out, file)
} else {
val entry = ZipEntry(file.path.substring(folder.parent.length))
val fis = FileInputStream(file.path)
addZipEntry(entry, fis, out)
}
}
} catch (e: Exception) {
activity.showErrorToast(e.toString())
return false
}
return true
}
private fun addZipEntry(entry: ZipEntry, fis: FileInputStream, out: ZipOutputStream) {
out.putNextEntry(entry)
val data = ByteArray(BUFFER)
val origin = BufferedInputStream(fis, BUFFER)
origin.use {
var count = origin.read(data, 0, BUFFER)
while (count != -1) {
out.write(data, 0, count)
count = origin.read(data, 0, BUFFER)
}
}
}
fun selectAll() { fun selectAll() {
val cnt = mItems.size val cnt = mItems.size
for (i in 0 until cnt) { for (i in 0 until cnt) {