Created
August 8, 2019 10:32
-
-
Save arindamxd/ecbdfc59662ae711d2c83d3591c6d796 to your computer and use it in GitHub Desktop.
Create Zip File
This file contains hidden or 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
/** | |
* Create ZIP file and returns the Uri for the zip file | |
* | |
* @param context Application Context | |
* @param files File Paths to be Zipped | |
* @param fileName ZIP File Name | |
* | |
* @return Uri for zip file | |
* @throws FileNotFoundException Throws if zip file cannot be found | |
*/ | |
@Throws(FileNotFoundException::class) | |
internal fun createZipFile(context: Context, files: List<Uri>, directoryName: String, fileName: String): Uri { | |
val outputDir = File(context.filesDir, "$OUTPUT_PATH/$directoryName") | |
val outputFile = File(outputDir, fileName) | |
try { | |
ZipOutputStream(BufferedOutputStream(FileOutputStream(outputFile))).use { out -> | |
for (file in files) { | |
val path = file.path | |
val fi = FileInputStream(path) | |
BufferedInputStream(fi).use { | |
val entry = ZipEntry(path.substring(path.lastIndexOf("/"))) | |
out.putNextEntry(entry) | |
it.copyTo(out, BUFFER) | |
} | |
} | |
} | |
} catch (e: Exception) { | |
logStackTrace(e) | |
} | |
return Uri.fromFile(outputFile) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment