This repository has been archived by the owner on Jul 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
203 additions
and
48 deletions.
There are no files selected for viewing
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
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
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
107 changes: 107 additions & 0 deletions
107
app/src/main/java/org/y20k/transistor/helpers/BackupHelper.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
* BackupHelper.kt | ||
* Implements the BackupHelper object | ||
* A BackupHelper provides helper methods for backing up and restoring the radio station collection | ||
* | ||
* This file is part of | ||
* TRANSISTOR - Radio App for Android | ||
* | ||
* Copyright (c) 2015-22 - Y20K.org | ||
* Licensed under the MIT-License | ||
* http://opensource.org/licenses/MIT | ||
*/ | ||
|
||
|
||
package org.y20k.transistor.helpers | ||
|
||
import android.content.ContentResolver | ||
import android.content.Context | ||
import android.net.Uri | ||
import java.io.* | ||
import java.util.zip.ZipEntry | ||
import java.util.zip.ZipOutputStream | ||
|
||
object BackupHelper { | ||
|
||
/* Define log tag */ | ||
private val TAG: String = LogHelper.makeLogTag(BackupHelper::class.java) | ||
|
||
|
||
/* xyz */ | ||
fun backup(context: Context, destinationUri: Uri) { | ||
val sourceFolder: File? = context.getExternalFilesDir("") | ||
if (sourceFolder != null && sourceFolder.isDirectory) { | ||
val resolver: ContentResolver = context.contentResolver | ||
val outputStream: OutputStream? = resolver.openOutputStream(destinationUri) | ||
ZipOutputStream(BufferedOutputStream(outputStream)).use { zipOutputStream -> | ||
zipOutputStream.use { | ||
zipFolder(it, sourceFolder, "") | ||
} | ||
} | ||
} else { | ||
LogHelper.e(TAG, "Unable to access External Storage.") | ||
} | ||
} | ||
|
||
|
||
/* Compresses folder into ZIP file - Credit: https://stackoverflow.com/a/52216574 */ | ||
private fun zipFolder(zipOutputStream: ZipOutputStream, source: File, parentDirPath: String) { | ||
// source.listFiles() will return null, if source is not a directory | ||
if (source.isDirectory) { | ||
val data = ByteArray(2048) | ||
// get all File objects in folder | ||
for (file in source.listFiles()!!) { | ||
val path = parentDirPath + File.separator + file.name | ||
when (file.isDirectory) { | ||
// CASE: Folder | ||
true -> { | ||
// val entry = ZipEntry(path + File.separator) // add separator to make entry a folder | ||
// entry.time = file.lastModified() | ||
// entry.size = file.length() | ||
// zipOutputStream.putNextEntry(entry) | ||
// call zipFolder recursively to add files within this folder | ||
zipFolder(zipOutputStream, file, path) | ||
} | ||
// CASE: File | ||
false -> { | ||
FileInputStream(file).use { fileInputStream -> | ||
BufferedInputStream(fileInputStream).use { bufferedInputStream -> | ||
val entry = ZipEntry(path) | ||
entry.time = file.lastModified() | ||
entry.size = file.length() | ||
zipOutputStream.putNextEntry(entry) | ||
while (true) { | ||
val readBytes = bufferedInputStream.read(data) | ||
if (readBytes == -1) { | ||
break | ||
} | ||
zipOutputStream.write(data, 0, readBytes) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
} | ||
} | ||
|
||
|
||
/* Normalize file path - protects against zip slip attack */ | ||
@Throws(IOException::class) | ||
private fun getFile(destinationFolder: File, zipEntry: ZipEntry): File { | ||
val destinationFile = File(destinationFolder, zipEntry.name) | ||
val destinationFolderPath = destinationFolder.canonicalPath | ||
val destinationFilePath = destinationFile.canonicalPath | ||
// make sure that zipEntry path is in the destination folder | ||
if (!destinationFilePath.startsWith(destinationFolderPath + File.separator)) { | ||
throw IOException("ZIP entry is not within of the destination folder: " + zipEntry.name) | ||
} | ||
return destinationFile | ||
} | ||
|
||
|
||
} | ||
|
||
|
||
|
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:width="24dp" | ||
android:height="24dp" | ||
android:viewportWidth="24" | ||
android:viewportHeight="24"> | ||
<path | ||
android:fillColor="@color/icon_default" | ||
android:pathData="M15,6H3v2h12V6zM15,10H3v2h12V10zM3,16h8v-2H3V16zM17,6v8.18C16.69,14.07 16.35,14 16,14c-1.66,0 -3,1.34 -3,3s1.34,3 3,3s3,-1.34 3,-3V8h3V6H17z"/> | ||
</vector> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:width="24dp" | ||
android:height="24dp" | ||
android:viewportWidth="24" | ||
android:viewportHeight="24"> | ||
<path | ||
android:fillColor="@color/icon_default" | ||
android:pathData="M14,12c0,-1.1 -0.9,-2 -2,-2s-2,0.9 -2,2 0.9,2 2,2 2,-0.9 2,-2zM12,3c-4.97,0 -9,4.03 -9,9L0,12l4,4 4,-4L5,12c0,-3.87 3.13,-7 7,-7s7,3.13 7,7 -3.13,7 -7,7c-1.51,0 -2.91,-0.49 -4.06,-1.3l-1.42,1.44C8.04,20.3 9.94,21 12,21c4.97,0 9,-4.03 9,-9s-4.03,-9 -9,-9z"/> | ||
</vector> |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
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
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
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