@@ -46,13 +46,14 @@ import kotlinx.android.synthetic.main.item_file_grid.view.*
4646import kotlinx.android.synthetic.main.item_section.view.*
4747import net.lingala.zip4j.exception.ZipException
4848import net.lingala.zip4j.io.inputstream.ZipInputStream
49+ import net.lingala.zip4j.io.outputstream.ZipOutputStream
4950import net.lingala.zip4j.model.LocalFileHeader
51+ import net.lingala.zip4j.model.ZipParameters
52+ import net.lingala.zip4j.model.enums.EncryptionMethod
5053import java.io.BufferedInputStream
5154import java.io.Closeable
5255import java.io.File
5356import java.util.*
54- import java.util.zip.ZipEntry
55- import java.util.zip.ZipOutputStream
5657
5758class ItemsAdapter (
5859 activity : SimpleActivity , var listItems : MutableList <ListItem >, val listener : ItemOperationsListener ? , recyclerView : MyRecyclerView ,
@@ -484,8 +485,7 @@ class ItemsAdapter(
484485 return
485486 }
486487
487- CompressAsDialog (activity, firstPath) {
488- val destination = it
488+ CompressAsDialog (activity, firstPath) { destination, password ->
489489 activity.handleAndroidSAFDialog(firstPath) { granted ->
490490 if (! granted) {
491491 return @handleAndroidSAFDialog
@@ -498,7 +498,7 @@ class ItemsAdapter(
498498 activity.toast(R .string.compressing)
499499 val paths = getSelectedFileDirItems().map { it.path }
500500 ensureBackgroundThread {
501- if (compressPaths(paths, destination)) {
501+ if (compressPaths(paths, destination, password )) {
502502 activity.runOnUiThread {
503503 activity.toast(R .string.compression_successful)
504504 listener?.refreshFragment()
@@ -648,13 +648,21 @@ class ItemsAdapter(
648648 }
649649
650650 @SuppressLint(" NewApi" )
651- private fun compressPaths (sourcePaths : List <String >, targetPath : String ): Boolean {
651+ private fun compressPaths (sourcePaths : List <String >, targetPath : String , password : String? = null ): Boolean {
652652 val queue = LinkedList <String >()
653653 val fos = activity.getFileOutputStreamSync(targetPath, " application/zip" ) ? : return false
654654
655- val zout = ZipOutputStream (fos)
655+ val zout = password?. let { ZipOutputStream (fos, password.toCharArray()) } ? : ZipOutputStream (fos)
656656 var res: Closeable = fos
657657
658+ fun zipEntry (name : String ) = ZipParameters ().also {
659+ it.fileNameInZip = name
660+ if (password != null ) {
661+ it.isEncryptFiles = true
662+ it.encryptionMethod = EncryptionMethod .AES
663+ }
664+ }
665+
658666 try {
659667 sourcePaths.forEach { currentPath ->
660668 var name: String
@@ -664,7 +672,11 @@ class ItemsAdapter(
664672 queue.push(mainFilePath)
665673 if (activity.getIsPathDirectory(mainFilePath)) {
666674 name = " ${mainFilePath.getFilenameFromPath()} /"
667- zout.putNextEntry(ZipEntry (name))
675+ zout.putNextEntry(
676+ ZipParameters ().also {
677+ it.fileNameInZip = name
678+ }
679+ )
668680 }
669681
670682 while (! queue.isEmpty()) {
@@ -677,9 +689,9 @@ class ItemsAdapter(
677689 if (activity.getIsPathDirectory(file.path)) {
678690 queue.push(file.path)
679691 name = " ${name.trimEnd(' /' )} /"
680- zout.putNextEntry(ZipEntry (name))
692+ zout.putNextEntry(zipEntry (name))
681693 } else {
682- zout.putNextEntry(ZipEntry (name))
694+ zout.putNextEntry(zipEntry (name))
683695 activity.getFileInputStreamSync(file.path)!! .copyTo(zout)
684696 zout.closeEntry()
685697 }
@@ -692,9 +704,9 @@ class ItemsAdapter(
692704 if (activity.getIsPathDirectory(file.absolutePath)) {
693705 queue.push(file.absolutePath)
694706 name = " ${name.trimEnd(' /' )} /"
695- zout.putNextEntry(ZipEntry (name))
707+ zout.putNextEntry(zipEntry (name))
696708 } else {
697- zout.putNextEntry(ZipEntry (name))
709+ zout.putNextEntry(zipEntry (name))
698710 activity.getFileInputStreamSync(file.path)!! .copyTo(zout)
699711 zout.closeEntry()
700712 }
@@ -703,7 +715,7 @@ class ItemsAdapter(
703715
704716 } else {
705717 name = if (base == currentPath) currentPath.getFilenameFromPath() else mainFilePath.relativizeWith(base)
706- zout.putNextEntry(ZipEntry (name))
718+ zout.putNextEntry(zipEntry (name))
707719 activity.getFileInputStreamSync(mainFilePath)!! .copyTo(zout)
708720 zout.closeEntry()
709721 }
0 commit comments