Skip to content

Instantly share code, notes, and snippets.

@cp-hardik-p
Created September 30, 2022 06:09
Show Gist options
  • Save cp-hardik-p/fa75c1e0a8d083deea54324fa6d8421b to your computer and use it in GitHub Desktop.
Save cp-hardik-p/fa75c1e0a8d083deea54324fa6d8421b to your computer and use it in GitHub Desktop.
@Throws(IOException::class)
fun zip(files: List<String>, zipFile: File) {
var origin: BufferedInputStream
val outputStream = ZipOutputStream(BufferedOutputStream(FileOutputStream(zipFile)))
outputStream.use { out ->
val data = ByteArray(4096)
for (i in files.indices) {
val fi = FileInputStream(files[i])
origin = BufferedInputStream(fi)
try {
val entry = ZipEntry(files[i].substring(files[i].lastIndexOf("/") + 1))
out.putNextEntry(entry)
var count: Int
while (origin.read(data).also { count = it } != -1) {
out.write(data, 0, count)
}
} finally {
origin.close()
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment