Skip to content

Instantly share code, notes, and snippets.

@MrPowerGamerBR
Created December 26, 2024 19:17
Show Gist options
  • Save MrPowerGamerBR/ca8c3fec16ce225ca46db1519ed71d67 to your computer and use it in GitHub Desktop.
Save MrPowerGamerBR/ca8c3fec16ce225ca46db1519ed71d67 to your computer and use it in GitHub Desktop.
import java.io.BufferedInputStream
import java.io.BufferedOutputStream
import java.io.File
import java.io.FileOutputStream
import java.util.*
import java.util.zip.ZipEntry
import java.util.zip.ZipFile
private fun extractFolder(zipFile: String, extractFolder: String) {
try {
val BUFFER = 2048
val file = File(zipFile)
val zip = ZipFile(file)
val newPath = extractFolder
File(newPath).mkdir()
val zipFileEntries: Enumeration<*> = zip.entries()
// Process each entry
while (zipFileEntries.hasMoreElements()) {
// grab a zip file entry
val entry = zipFileEntries.nextElement() as ZipEntry
val currentEntry = entry.name
val destFile = File(newPath, currentEntry)
//destFile = new File(newPath, destFile.getName());
val destinationParent = destFile.parentFile
// create the parent directory structure if needed
destinationParent.mkdirs()
if (!entry.isDirectory) {
val `is` = BufferedInputStream(
zip
.getInputStream(entry)
)
var currentByte: Int
// establish buffer for writing file
val data = ByteArray(BUFFER)
// write the current file to disk
val fos = FileOutputStream(destFile)
val dest = BufferedOutputStream(
fos,
BUFFER
)
// read and write until last byte is encountered
while ((`is`.read(data, 0, BUFFER).also { currentByte = it }) != -1) {
dest.write(data, 0, currentByte)
}
dest.flush()
dest.close()
`is`.close()
}
}
} catch (e: Exception) {
println("ERROR: " + e.message)
}
}
fun main() {
val zipFilePath = "input_here.zip"
val destDirectory = "output_folder_here"
extractFolder(zipFilePath, destDirectory)
println("Unzipping completed!")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment