Skip to content

Instantly share code, notes, and snippets.

@arindamxd
Created August 8, 2019 10:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arindamxd/ecbdfc59662ae711d2c83d3591c6d796 to your computer and use it in GitHub Desktop.
Save arindamxd/ecbdfc59662ae711d2c83d3591c6d796 to your computer and use it in GitHub Desktop.
Create Zip File
/**
* 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