Last active
October 17, 2024 13:46
-
-
Save NitinPraksash9911/dea21ec4b8ae7df068f8f891187b6d1e to your computer and use it in GitHub Desktop.
Unzipping file in android/kotlin
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
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. | |
*/ |
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))
}
}
}
}
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
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?