Last active
June 1, 2020 17:22
-
-
Save Apsaliya/e48717b7c46864d71e8f035845c9540b to your computer and use it in GitHub Desktop.
backup_zip
This file contains 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
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