Skip to content

Instantly share code, notes, and snippets.

@NitinPraksash9911
Last active May 6, 2024 15:31
Show Gist options
  • Save NitinPraksash9911/dea21ec4b8ae7df068f8f891187b6d1e to your computer and use it in GitHub Desktop.
Save NitinPraksash9911/dea21ec4b8ae7df068f8f891187b6d1e to your computer and use it in GitHub Desktop.
Unzipping file in android/kotlin
import java.io.*
import java.util.zip.ZipFile
/**
* UnzipUtils class extracts files and sub-directories of a standard zip file to
* a destination directory.
*
*/
object UnzipUtils {
/**
* @param zipFilePath
* @param destDirectory
* @throws IOException
*/
@Throws(IOException::class)
fun unzip(zipFilePath: File, destDirectory: String) {
File(destDirectory).run {
if (!exists()) {
mkdirs()
}
}
ZipFile(zipFilePath).use { zip ->
zip.entries().asSequence().forEach { entry ->
zip.getInputStream(entry).use { input ->
val filePath = destDirectory + File.separator + entry.name
if (!entry.isDirectory) {
// if the entry is a file, extracts it
extractFile(input, filePath)
} else {
// if the entry is a directory, make the directory
val dir = File(filePath)
dir.mkdir()
}
}
}
}
}
/**
* Extracts a zip entry (file entry)
* @param inputStream
* @param destFilePath
* @throws IOException
*/
@Throws(IOException::class)
private fun extractFile(inputStream: InputStream, destFilePath: String) {
val bos = BufferedOutputStream(FileOutputStream(destFilePath))
val bytesIn = ByteArray(BUFFER_SIZE)
var read: Int
while (inputStream.read(bytesIn).also { read = it } != -1) {
bos.write(bytesIn, 0, read)
}
bos.close()
}
/**
* Size of the buffer to read/write data
*/
private const val BUFFER_SIZE = 4096
}
/**
Copyright 2020 Nitin Prakash
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
@FoamyGuy
Copy link

🎉 Thank you!

@Wb11G
Copy link

Wb11G commented Dec 19, 2023

val folder = applicationContext.getExternalFilesDir(test.zip)
val destinationPath = "/storage/emulated/0/advertisments/unzip"
SystemUtils().unzip(folder!!, destinationPath)

why always get error Caused by: java.util.zip.ZipException: error in opening zip file?

@elect86
Copy link

elect86 commented Apr 17, 2024

Ps: you may want to change it as

    @Throws(IOException::class)
    infix fun File.unzip(destDirectory: String) {
        File(destDirectory).run {
            if (!exists())
                mkdirs()
        }
        ZipFile(this).use { zip ->
            zip.entries().asSequence().forEach { entry ->
                zip.getInputStream(entry).use { input ->
                    val filePath = destDirectory / entry.name
                    if (entry.isDirectory) // if the entry is a directory, make the directory
                        File(filePath).mkdir()
                    else // if the entry is a file, extracts it
                        input.copyTo(FileOutputStream(filePath))
                }
            }
        }
    }

@Bahir
Copy link

Bahir commented May 6, 2024

                val filePath = destDirectory / entry.name

To avoid ambiguity of String and URI
val filePath: String = "${destDirectory}/${entry.name}"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment