remove a duplicate part from zipping

This commit is contained in:
tibbi 2017-09-01 23:20:40 +02:00
parent b604af77ab
commit 9fa228d989
1 changed files with 25 additions and 24 deletions

View File

@ -232,7 +232,6 @@ class ItemsAdapter(val activity: SimpleActivity, var mItems: MutableList<FileDir
private fun zipFileAtPath(sourcePaths: List<String>, targetPath: String): Boolean { private fun zipFileAtPath(sourcePaths: List<String>, targetPath: String): Boolean {
var out: ZipOutputStream? = null var out: ZipOutputStream? = null
try { try {
var origin: BufferedInputStream?
val fos = FileOutputStream(targetPath) val fos = FileOutputStream(targetPath)
out = ZipOutputStream(BufferedOutputStream(fos)) out = ZipOutputStream(BufferedOutputStream(fos))
@ -243,57 +242,59 @@ class ItemsAdapter(val activity: SimpleActivity, var mItems: MutableList<FileDir
return false return false
} }
} else { } else {
val data = ByteArray(BUFFER)
val fis = FileInputStream(it)
origin = BufferedInputStream(fis, BUFFER)
val entry = ZipEntry(it.getFilenameFromPath()) val entry = ZipEntry(it.getFilenameFromPath())
out!!.putNextEntry(entry) val fis = FileInputStream(it)
var count = origin!!.read(data, 0, BUFFER) addZipEntry(entry, fis, out!!)
while (count != -1) {
out!!.write(data, 0, count)
count = origin!!.read(data, 0, BUFFER)
}
} }
} }
} catch (e: Exception) { } catch (e: Exception) {
activity.showErrorToast(e.toString()) activity.showErrorToast(e.toString())
return false return false
} finally { } finally {
out?.close() try {
out?.close()
} catch (e: Exception) {
activity.showErrorToast(e.toString())
}
} }
return true return true
} }
private fun zipSubFolder(out: ZipOutputStream, folder: File): Boolean { private fun zipSubFolder(out: ZipOutputStream, folder: File): Boolean {
var origin: BufferedInputStream? = null
try { try {
val fileList = folder.listFiles() val fileList = folder.listFiles() ?: return true
for (file in fileList) { for (file in fileList) {
if (file.isDirectory) { if (file.isDirectory) {
zipSubFolder(out, file) zipSubFolder(out, file)
} else { } else {
val data = ByteArray(BUFFER)
origin = BufferedInputStream(FileInputStream(file.path), BUFFER)
val entry = ZipEntry(file.path.substring(folder.parent.length)) val entry = ZipEntry(file.path.substring(folder.parent.length))
out.putNextEntry(entry) val fis = FileInputStream(file.path)
var count = origin.read(data, 0, BUFFER) addZipEntry(entry, fis, out)
while (count != -1) {
out.write(data, 0, count)
count = origin.read(data, 0, BUFFER)
}
} }
} }
} catch (e: Exception) { } catch (e: Exception) {
activity.showErrorToast(e.toString()) activity.showErrorToast(e.toString())
return false return false
} finally {
origin?.close()
} }
return true 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) {