-
-
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. | |
*/ |
[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}"
This does not work for zip files with sub folders. SO i modified the code.
Method