@@ -31,31 +31,37 @@ object ZipUtil {
3131 return false
3232 }
3333
34- val filesToCompress = ArrayList <String >()
3534 val directory = File (folderPath)
36- if (directory.isDirectory) {
37- directory.listFiles()?.forEach {
38- if (it.isFile) {
39- filesToCompress.add(it.absolutePath)
35+ if (! directory.isDirectory) return false
36+
37+ // Collect all files recursively, preserving relative paths for zip entries
38+ val filesToCompress = ArrayList <Pair <String , File >>() // <entryName, file>
39+ fun collectFiles (dir : File , prefix : String ) {
40+ dir.listFiles()?.forEach { f ->
41+ if (f.isFile) {
42+ filesToCompress.add(Pair (if (prefix.isEmpty()) f.name else " $prefix /${f.name} " , f))
43+ } else if (f.isDirectory) {
44+ collectFiles(f, if (prefix.isEmpty()) f.name else " $prefix /${f.name} " )
4045 }
4146 }
4247 }
48+ collectFiles(directory, " " )
49+
4350 if (filesToCompress.isEmpty()) {
4451 return false
4552 }
4653
4754 val zos = ZipOutputStream (FileOutputStream (outputZipFilePath))
4855
49- filesToCompress.forEach { file ->
50- val ze = ZipEntry (File (file).name )
56+ filesToCompress.forEach { (entryName, file) ->
57+ val ze = ZipEntry (entryName )
5158 zos.putNextEntry(ze)
5259 val inputStream = FileInputStream (file)
5360 while (true ) {
5461 val len = inputStream.read(buffer)
5562 if (len <= 0 ) break
5663 zos.write(buffer, 0 , len)
5764 }
58-
5965 inputStream.close()
6066 }
6167
@@ -113,6 +119,7 @@ object ZipUtil {
113119 */
114120 @Throws(IOException ::class )
115121 private fun extractFile (inputStream : InputStream , destFilePath : String ) {
122+ File (destFilePath).parentFile?.mkdirs()
116123 val bos = BufferedOutputStream (FileOutputStream (destFilePath))
117124 val bytesIn = ByteArray (BUFFER_SIZE )
118125 var read: Int
0 commit comments