@@ -44,13 +44,14 @@ import kotlinx.android.synthetic.main.item_file_dir_list.view.item_icon
4444import kotlinx.android.synthetic.main.item_file_dir_list.view.item_name
4545import kotlinx.android.synthetic.main.item_file_grid.view.*
4646import kotlinx.android.synthetic.main.item_section.view.*
47+ import net.lingala.zip4j.io.outputstream.ZipOutputStream
48+ import net.lingala.zip4j.model.ZipParameters
4749import java.io.BufferedInputStream
4850import java.io.Closeable
4951import java.io.File
5052import java.util.*
5153import java.util.zip.ZipEntry
5254import java.util.zip.ZipInputStream
53- import java.util.zip.ZipOutputStream
5455
5556class ItemsAdapter (
5657 activity : SimpleActivity , var listItems : MutableList <ListItem >, val listener : ItemOperationsListener ? , recyclerView : MyRecyclerView ,
@@ -482,8 +483,7 @@ class ItemsAdapter(
482483 return
483484 }
484485
485- CompressAsDialog (activity, firstPath) {
486- val destination = it
486+ CompressAsDialog (activity, firstPath) { destination, password ->
487487 activity.handleAndroidSAFDialog(firstPath) { granted ->
488488 if (! granted) {
489489 return @handleAndroidSAFDialog
@@ -496,7 +496,7 @@ class ItemsAdapter(
496496 activity.toast(R .string.compressing)
497497 val paths = getSelectedFileDirItems().map { it.path }
498498 ensureBackgroundThread {
499- if (compressPaths(paths, destination)) {
499+ if (compressPaths(paths, destination, password )) {
500500 activity.runOnUiThread {
501501 activity.toast(R .string.compression_successful)
502502 listener?.refreshFragment()
@@ -643,13 +643,17 @@ class ItemsAdapter(
643643 }
644644
645645 @SuppressLint(" NewApi" )
646- private fun compressPaths (sourcePaths : List <String >, targetPath : String ): Boolean {
646+ private fun compressPaths (sourcePaths : List <String >, targetPath : String , password : String? = null ): Boolean {
647647 val queue = LinkedList <String >()
648648 val fos = activity.getFileOutputStreamSync(targetPath, " application/zip" ) ? : return false
649649
650- val zout = ZipOutputStream (fos)
650+ val zout = password?. let { ZipOutputStream (fos, password.toCharArray()) } ? : ZipOutputStream (fos)
651651 var res: Closeable = fos
652652
653+ fun zipEntry (name : String ) = ZipParameters ().also {
654+ it.fileNameInZip = name
655+ }
656+
653657 try {
654658 sourcePaths.forEach { currentPath ->
655659 var name: String
@@ -659,7 +663,11 @@ class ItemsAdapter(
659663 queue.push(mainFilePath)
660664 if (activity.getIsPathDirectory(mainFilePath)) {
661665 name = " ${mainFilePath.getFilenameFromPath()} /"
662- zout.putNextEntry(ZipEntry (name))
666+ zout.putNextEntry(
667+ ZipParameters ().also {
668+ it.fileNameInZip = name
669+ }
670+ )
663671 }
664672
665673 while (! queue.isEmpty()) {
@@ -672,9 +680,9 @@ class ItemsAdapter(
672680 if (activity.getIsPathDirectory(file.path)) {
673681 queue.push(file.path)
674682 name = " ${name.trimEnd(' /' )} /"
675- zout.putNextEntry(ZipEntry (name))
683+ zout.putNextEntry(zipEntry (name))
676684 } else {
677- zout.putNextEntry(ZipEntry (name))
685+ zout.putNextEntry(zipEntry (name))
678686 activity.getFileInputStreamSync(file.path)!! .copyTo(zout)
679687 zout.closeEntry()
680688 }
@@ -687,9 +695,9 @@ class ItemsAdapter(
687695 if (activity.getIsPathDirectory(file.absolutePath)) {
688696 queue.push(file.absolutePath)
689697 name = " ${name.trimEnd(' /' )} /"
690- zout.putNextEntry(ZipEntry (name))
698+ zout.putNextEntry(zipEntry (name))
691699 } else {
692- zout.putNextEntry(ZipEntry (name))
700+ zout.putNextEntry(zipEntry (name))
693701 activity.getFileInputStreamSync(file.path)!! .copyTo(zout)
694702 zout.closeEntry()
695703 }
@@ -698,7 +706,7 @@ class ItemsAdapter(
698706
699707 } else {
700708 name = if (base == currentPath) currentPath.getFilenameFromPath() else mainFilePath.relativizeWith(base)
701- zout.putNextEntry(ZipEntry (name))
709+ zout.putNextEntry(zipEntry (name))
702710 activity.getFileInputStreamSync(mainFilePath)!! .copyTo(zout)
703711 zout.closeEntry()
704712 }
0 commit comments