Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add backup and restore configuration functionality
- Loading branch information
Showing
16 changed files
with
363 additions
and
17 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
102 changes: 102 additions & 0 deletions
102
V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/ZipUtil.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,102 @@ | ||
package com.v2ray.ang.util | ||
|
||
import java.io.BufferedOutputStream | ||
import java.io.File | ||
import java.io.FileInputStream | ||
import java.io.FileOutputStream | ||
import java.io.IOException | ||
import java.io.InputStream | ||
import java.util.zip.ZipEntry | ||
import java.util.zip.ZipFile | ||
import java.util.zip.ZipOutputStream | ||
|
||
object ZipUtil { | ||
private const val BUFFER_SIZE = 4096 | ||
|
||
@Throws(IOException::class) | ||
fun zipFromFolder(folderPath: String, outputZipFilePath: String): Boolean { | ||
val buffer = ByteArray(BUFFER_SIZE) | ||
|
||
try { | ||
if (folderPath.isEmpty() || outputZipFilePath.isEmpty()) { | ||
return false | ||
} | ||
|
||
val filesToCompress = ArrayList<String>() | ||
val directory = File(folderPath) | ||
if (directory.isDirectory) { | ||
directory.listFiles()?.forEach { | ||
if (it.isFile) { | ||
filesToCompress.add(it.absolutePath) | ||
} | ||
} | ||
} | ||
if (filesToCompress.isEmpty()) { | ||
return false | ||
} | ||
|
||
val zos = ZipOutputStream(FileOutputStream(outputZipFilePath)) | ||
|
||
filesToCompress.forEach { file -> | ||
val ze = ZipEntry(File(file).name) | ||
zos.putNextEntry(ze) | ||
val inputStream = FileInputStream(file) | ||
while (true) { | ||
val len = inputStream.read(buffer) | ||
if (len <= 0) break | ||
zos.write(buffer, 0, len) | ||
} | ||
|
||
inputStream.close() | ||
} | ||
|
||
zos.closeEntry() | ||
zos.close() | ||
} catch (e: Exception) { | ||
e.printStackTrace() | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
@Throws(IOException::class) | ||
fun unzipToFolder(zipFile: File, destDirectory: String): Boolean { | ||
File(destDirectory).run { | ||
if (!exists()) { | ||
mkdirs() | ||
} | ||
} | ||
try { | ||
ZipFile(zipFile).use { zip -> | ||
zip.entries().asSequence().forEach { entry -> | ||
zip.getInputStream(entry).use { input -> | ||
val filePath = destDirectory + File.separator + entry.name | ||
if (!entry.isDirectory) { | ||
// if the entry is a file, extracts it | ||
extractFile(input, filePath) | ||
} else { | ||
// if the entry is a directory, make the directory | ||
val dir = File(filePath) | ||
dir.mkdir() | ||
} | ||
} | ||
} | ||
} | ||
} catch (e: Exception) { | ||
e.printStackTrace() | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
@Throws(IOException::class) | ||
private fun extractFile(inputStream: InputStream, destFilePath: String) { | ||
val bos = BufferedOutputStream(FileOutputStream(destFilePath)) | ||
val bytesIn = ByteArray(BUFFER_SIZE) | ||
var read: Int | ||
while (inputStream.read(bytesIn).also { read = it } != -1) { | ||
bos.write(bytesIn, 0, read) | ||
} | ||
bos.close() | ||
} | ||
} |
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="1024" | ||
android:viewportHeight="1024"> | ||
<path | ||
android:fillColor="#FFFFFFFF" | ||
android:pathData="M864,192L704,192L704,96c0,-17.7 -14.3,-32 -32,-32L352,64c-9,0 -17.2,3.7 -22.9,9.7L137.7,265.1c-6,5.8 -9.7,14 -9.7,22.9v512c0,17.7 14.3,32 32,32h160v96c0,17.7 14.3,32 32,32h512c17.7,0 32,-14.3 32,-32L896,224c0,-17.7 -14.3,-32 -32,-32zM320,173.2L320,256h-82.8l82.8,-82.8zM192,768L192,320h160c17.7,0 32,-14.3 32,-32L384,128h256v64h-96c-9,0 -17.2,3.7 -22.9,9.7L329.7,393.1c-6,5.8 -9.7,14 -9.7,22.9v352L192,768zM512,301.2L512,384h-82.8l82.8,-82.8zM832,896L384,896L384,448h160c17.7,0 32,-14.3 32,-32L576,256h256v640z" /> | ||
</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="1024" | ||
android:viewportHeight="1024"> | ||
<path | ||
android:fillColor="#FFFFFFFF" | ||
android:pathData="M938.7,512a384,384 0,0 1,-384 384,379.3 379.3,0 0,1 -220.2,-69.5 21.8,21.8 0,0 1,-9 -15.8,21.3 21.3,0 0,1 6,-16.6l30.7,-31.1a21.3,21.3 0,0 1,26.9 -2.6A294.8,294.8 0,0 0,554.7 810.7a298.7,298.7 0,1 0,-298.7 -298.7h100.7a20.9,20.9 0,0 1,15.4 6.4l8.5,8.5a21.3,21.3 0,0 1,0 30.3L230,708.3a21.8,21.8 0,0 1,-30.3 0l-150.6,-151a21.3,21.3 0,0 1,0 -30.3l8.5,-8.5a20.9,20.9 0,0 1,15.4 -6.4H170.7a384,384 0,0 1,768 0z" /> | ||
</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="1024" | ||
android:viewportHeight="1024"> | ||
<path | ||
android:fillColor="#FF000000" | ||
android:pathData="M864,192L704,192L704,96c0,-17.7 -14.3,-32 -32,-32L352,64c-9,0 -17.2,3.7 -22.9,9.7L137.7,265.1c-6,5.8 -9.7,14 -9.7,22.9v512c0,17.7 14.3,32 32,32h160v96c0,17.7 14.3,32 32,32h512c17.7,0 32,-14.3 32,-32L896,224c0,-17.7 -14.3,-32 -32,-32zM320,173.2L320,256h-82.8l82.8,-82.8zM192,768L192,320h160c17.7,0 32,-14.3 32,-32L384,128h256v64h-96c-9,0 -17.2,3.7 -22.9,9.7L329.7,393.1c-6,5.8 -9.7,14 -9.7,22.9v352L192,768zM512,301.2L512,384h-82.8l82.8,-82.8zM832,896L384,896L384,448h160c17.7,0 32,-14.3 32,-32L576,256h256v640z" /> | ||
</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="1024" | ||
android:viewportHeight="1024"> | ||
<path | ||
android:fillColor="#FF000000" | ||
android:pathData="M938.7,512a384,384 0,0 1,-384 384,379.3 379.3,0 0,1 -220.2,-69.5 21.8,21.8 0,0 1,-9 -15.8,21.3 21.3,0 0,1 6,-16.6l30.7,-31.1a21.3,21.3 0,0 1,26.9 -2.6A294.8,294.8 0,0 0,554.7 810.7a298.7,298.7 0,1 0,-298.7 -298.7h100.7a20.9,20.9 0,0 1,15.4 6.4l8.5,8.5a21.3,21.3 0,0 1,0 30.3L230,708.3a21.8,21.8 0,0 1,-30.3 0l-150.6,-151a21.3,21.3 0,0 1,0 -30.3l8.5,-8.5a20.9,20.9 0,0 1,15.4 -6.4H170.7a384,384 0,0 1,768 0z" /> | ||
</vector> |
Oops, something went wrong.