remove a duplicate part from zipping

This commit is contained in:
tibbi 2017-09-01 23:20:40 +02:00
parent b604af77ab
commit 9fa228d989

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,41 +242,51 @@ 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 {
try {
out?.close() 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))
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) out.putNextEntry(entry)
val data = ByteArray(BUFFER)
val origin = BufferedInputStream(fis, BUFFER)
origin.use {
var count = origin.read(data, 0, BUFFER) var count = origin.read(data, 0, BUFFER)
while (count != -1) { while (count != -1) {
out.write(data, 0, count) out.write(data, 0, count)
@ -285,14 +294,6 @@ class ItemsAdapter(val activity: SimpleActivity, var mItems: MutableList<FileDir
} }
} }
} }
} catch (e: Exception) {
activity.showErrorToast(e.toString())
return false
} finally {
origin?.close()
}
return true
}
fun selectAll() { fun selectAll() {
val cnt = mItems.size val cnt = mItems.size