improve the compression functionality
This commit is contained in:
parent
c89fcb8692
commit
b604af77ab
|
@ -31,9 +31,9 @@ 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
|
||||||
|
|
||||||
val multiSelector = MultiSelector()
|
val multiSelector = MultiSelector()
|
||||||
val config = activity.config
|
val config = activity.config
|
||||||
|
@ -213,9 +213,9 @@ class ItemsAdapter(val activity: SimpleActivity, var mItems: MutableList<FileDir
|
||||||
CompressAsDialog(activity, firstPath) {
|
CompressAsDialog(activity, firstPath) {
|
||||||
activity.handleSAFDialog(File(firstPath)) {
|
activity.handleSAFDialog(File(firstPath)) {
|
||||||
activity.toast(R.string.compressing)
|
activity.toast(R.string.compressing)
|
||||||
val paths = selectedPositions.map { mItems[it].path }.toTypedArray()
|
val paths = selectedPositions.map { mItems[it].path }
|
||||||
Thread({
|
Thread({
|
||||||
if (compress(paths, it)) {
|
if (zipFileAtPath(paths, it)) {
|
||||||
activity.toast(R.string.compression_successful)
|
activity.toast(R.string.compression_successful)
|
||||||
activity.runOnUiThread {
|
activity.runOnUiThread {
|
||||||
listener?.refreshItems()
|
listener?.refreshItems()
|
||||||
|
@ -229,27 +229,30 @@ class ItemsAdapter(val activity: SimpleActivity, var mItems: MutableList<FileDir
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun compress(paths: Array<String>, targetPath: String): Boolean {
|
private fun zipFileAtPath(sourcePaths: List<String>, targetPath: String): Boolean {
|
||||||
val BUFFER_SIZE = 8192
|
|
||||||
var out: ZipOutputStream? = null
|
var out: ZipOutputStream? = null
|
||||||
try {
|
try {
|
||||||
out = ZipOutputStream(BufferedOutputStream(FileOutputStream(targetPath)))
|
|
||||||
var origin: BufferedInputStream?
|
var origin: BufferedInputStream?
|
||||||
val data = ByteArray(BUFFER_SIZE)
|
val fos = FileOutputStream(targetPath)
|
||||||
|
out = ZipOutputStream(BufferedOutputStream(fos))
|
||||||
|
|
||||||
for (i in paths.indices) {
|
sourcePaths.forEach {
|
||||||
val fi = FileInputStream(paths[i])
|
val sourceFile = File(it)
|
||||||
origin = BufferedInputStream(fi, BUFFER_SIZE)
|
if (sourceFile.isDirectory) {
|
||||||
try {
|
if (!zipSubFolder(out!!, sourceFile)) {
|
||||||
val entry = ZipEntry(paths[i].substring(paths[i].lastIndexOf("/") + 1))
|
return false
|
||||||
out.putNextEntry(entry)
|
}
|
||||||
var count = origin.read(data, 0, BUFFER_SIZE)
|
} else {
|
||||||
while (count != -1) {
|
val data = ByteArray(BUFFER)
|
||||||
out.write(data, 0, count)
|
val fis = FileInputStream(it)
|
||||||
count = origin.read(data, 0, BUFFER_SIZE)
|
origin = BufferedInputStream(fis, BUFFER)
|
||||||
|
val entry = ZipEntry(it.getFilenameFromPath())
|
||||||
|
out!!.putNextEntry(entry)
|
||||||
|
var count = origin!!.read(data, 0, BUFFER)
|
||||||
|
while (count != -1) {
|
||||||
|
out!!.write(data, 0, count)
|
||||||
|
count = origin!!.read(data, 0, BUFFER)
|
||||||
}
|
}
|
||||||
} finally {
|
|
||||||
origin.close()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
|
@ -258,6 +261,36 @@ class ItemsAdapter(val activity: SimpleActivity, var mItems: MutableList<FileDir
|
||||||
} finally {
|
} finally {
|
||||||
out?.close()
|
out?.close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun zipSubFolder(out: ZipOutputStream, folder: File): Boolean {
|
||||||
|
var origin: BufferedInputStream? = null
|
||||||
|
|
||||||
|
try {
|
||||||
|
val fileList = folder.listFiles()
|
||||||
|
for (file in fileList) {
|
||||||
|
if (file.isDirectory) {
|
||||||
|
zipSubFolder(out, file)
|
||||||
|
} else {
|
||||||
|
val data = ByteArray(BUFFER)
|
||||||
|
origin = BufferedInputStream(FileInputStream(file.path), BUFFER)
|
||||||
|
val entry = ZipEntry(file.path.substring(folder.parent.length))
|
||||||
|
out.putNextEntry(entry)
|
||||||
|
var count = origin.read(data, 0, BUFFER)
|
||||||
|
while (count != -1) {
|
||||||
|
out.write(data, 0, count)
|
||||||
|
count = origin.read(data, 0, BUFFER)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
activity.showErrorToast(e.toString())
|
||||||
|
return false
|
||||||
|
} finally {
|
||||||
|
origin?.close()
|
||||||
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue