Skip to content

Instantly share code, notes, and snippets.

@Apsaliya
Last active June 1, 2020 17:22
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 Apsaliya/e48717b7c46864d71e8f035845c9540b to your computer and use it in GitHub Desktop.
Save Apsaliya/e48717b7c46864d71e8f035845c9540b to your computer and use it in GitHub Desktop.
backup_zip
suspend fun packZipFileForBackup(context: Context): File? {
return withContext(Dispatchers.IO) {
val dbFile = context.getDatabasePath("dbName.db")
val dbParentDirectory = dbFile.parentFile
val zipFilePath = context.filesDir.path + "backup.zip" // create zip file for backup
val zipFile = File(zipFilePath)
val daraDir = context.filesDir.parentFile
if (daraDir != null) {
val sharedPrefDirectoryPath = daraDir.absolutePath + "/shared_prefs"
val encZipFile = ZipFile(zipFile.absolutePath, "password".toCharArray())
val zipParameters = ZipParameters()
zipParameters.isEncryptFiles = true
zipParameters.encryptionMethod = EncryptionMethod.AES
encZipFile.addFolder(File(sharedPrefDirectoryPath), zipParameters) // add shared pref directory
encZipFile.addFolder(context.filesDir, zipParameters) // add files directory
encZipFile.addFolder(dbParentDirectory, zipParameters) // add database directory
}
return@withContext zipFile
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment