Я решил, вот полное решение:
fun zipAll(directory: String, zipFile: String) {
val sourceFile = File(directory)
ZipOutputStream(BufferedOutputStream(FileOutputStream(zipFile))).use {
it.use {
zipFiles(it, sourceFile, "")
}
}
}
private fun zipFiles(zipOut: ZipOutputStream, sourceFile: File, parentDirPath: String) {
val data = ByteArray(2048)
for (f in sourceFile.listFiles()) {
if (f.isDirectory) {
val entry = ZipEntry(f.name + File.separator)
entry.time = f.lastModified()
entry.isDirectory
entry.size = f.length()
Log.i("zip", "Adding Directory: " + f.name)
zipOut.putNextEntry(entry)
//Call recursively to add files within this directory
zipFiles(zipOut, f, f.name)
} else {
if (!f.name.contains(".zip")) { //If folder contains a file with extension ".zip", skip it
FileInputStream(f).use { fi ->
BufferedInputStream(fi).use { origin ->
val path = parentDirPath + File.separator + f.name
Log.i("zip", "Adding file: $path")
val entry = ZipEntry(path)
entry.time = f.lastModified()
entry.isDirectory
entry.size = f.length()
zipOut.putNextEntry(entry)
while (true) {
val readBytes = origin.read(data)
if (readBytes == -1) {
break
}
zipOut.write(data, 0, readBytes)
}
}
}
} else {
zipOut.closeEntry()
zipOut.close()
}
}
}
}
Использование:
zipAll("Path of source files to Zip", "Path for Zip to Export")