-
-
Save NitinPraksash9911/dea21ec4b8ae7df068f8f891187b6d1e to your computer and use it in GitHub Desktop.
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. | |
*/ |
i made progress work!
Im using this on coroutines and works smoothly, this is my approach.
`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
* @param updateProgress interface to update progress on UI
* @throws IOException
*/
@throws(IOException::class)
fun unzip(
zipFilePath: File,
destDirectory: String,
updateProgress: (progress: Int) -> Unit
) {
File(destDirectory).run {
if (!exists()) {
mkdirs()
}
}
ZipFile(zipFilePath).use { zip ->
var entryCount = 0
val totalEntries = zip.entries().toList().size // files to unzip
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()
}
entryCount++
updateProgress(((entryCount.toDouble() / totalEntries) * 100).toInt())
}
}
}
}
/**
* Extracts a zip entry (file entry)
* @param inputStream
* @param destFilePath
* @throws IOException
*/
@Throws(IOException::class)
private fun extractFile(inputStream: InputStream, destFilePath: String) {
//log("Descomprimiendo archivo: $destFilePath")
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
}`
Using Unit lambda as a parameter you can directly interface both process and UI
UnzipUtils.unzip(file, fileFolder) { progress -> print("$progress") }
This does not work for zip files with sub folders. SO i modified the code.
if (!entry.isDirectory) {
// if the entry is a file, extracts it
val parentFolder = File(getParentDirPath(filePath))
if (!parentFolder.exists()) {
parentFolder.mkdirs()
}
extractFile(input, filePath)
} else {
// if the entry is a directory, make the directory
val dir = File(filePath)
dir.mkdir()
}
Method
fun getParentDirPath(fileOrDirPath: String): String? {
val endsWithSlash = fileOrDirPath.endsWith(File.separator)
return fileOrDirPath.substring(
0, fileOrDirPath.lastIndexOf(
File.separatorChar,
if (endsWithSlash) fileOrDirPath.length - 2 else fileOrDirPath.length - 1
)
)
}
[Mac zip compress without __MACOSX folder?]
(https://stackoverflow.com/questions/10924236/mac-zip-compress-without-macosx-folder) @NitinPraksash9911 ?
@NitinPraksash9911 would you consider specifying a license for this code? I know you published it publicly here and an the medium blog post but without a license included anywhere it's difficult to know what kind of re-use is permissible and under what conditions.
updated @FoamyGuy
🎉 Thank you!
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?
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}"
I had some trouble with the current example, I modified it to work for me. You can find it here https://github.com/AmibeSkyfy16/MariaDBServerFabricMC/blob/2.0/src/main/kotlin/ch/skyfy/mariadbserverfabricmc/utils/UnzipUtils.kt