Skip to content

Instantly share code, notes, and snippets.

@oguzhanaslann
Created November 26, 2023 16:18
Show Gist options
  • Select an option

  • Save oguzhanaslann/5a18429720303fa5de21d144ea356958 to your computer and use it in GitHub Desktop.

Select an option

Save oguzhanaslann/5a18429720303fa5de21d144ea356958 to your computer and use it in GitHub Desktop.
Zip helper for Android
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import java.util.zip.ZipEntry
import java.util.zip.ZipFile
import java.util.zip.ZipInputStream
import java.util.zip.ZipOutputStream
object ZipFacade {
fun zip(filesToCompress: List<String>, outputZipFilePath: String) {
val buffer = ByteArray(1024)
try {
val fos = FileOutputStream(outputZipFilePath)
val zos = ZipOutputStream(fos)
filesToCompress.forEach { file ->
val ze = ZipEntry(File(file).name)
zos.putNextEntry(ze)
val `in` = FileInputStream(file)
var len: Int
while (`in`.read(buffer)
.also { len = it } > 0
) {
zos.write(buffer, 0, len)
}
`in`.close()
}
zos.closeEntry()
zos.close()
} catch (e: Exception) {
e.printStackTrace()
}
}
fun unZip(
zipFilePath: String,
unzipDirectoryPath: String
) {
val unzipDir = File(unzipDirectoryPath)
if (!unzipDir.exists()) {
unzipDir.mkdir()
}
val zipFile = File(zipFilePath)
ZipFile(zipFile).unzip(unzipDir)
}
private fun ZipFile.unzip(
unzipDir: File
) {
val enum = entries()
while (enum.hasMoreElements()) {
val entry = enum.nextElement()
val entryName = entry.name
val fis = FileInputStream(this.name)
val zis = ZipInputStream(fis)
while (true) {
val nextEntry = zis.nextEntry ?: break
if (nextEntry.name == entryName) {
val fout = FileOutputStream(File(unzipDir, nextEntry.name))
var c = zis.read()
while (c != -1) {
fout.write(c)
c = zis.read()
}
zis.closeEntry()
fout.close()
}
}
zis.close()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment