Created
September 30, 2022 06:09
-
-
Save cp-hardik-p/fa75c1e0a8d083deea54324fa6d8421b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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