Skip to content

Commit

Permalink
Utils: add function that copies a directory from assets
Browse files Browse the repository at this point in the history
re #6
  • Loading branch information
bk138 committed Aug 15, 2024
1 parent 3e83277 commit 33d6af7
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions app/src/main/java/net/christianbeier/droidvnc_ng/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import android.os.Build
import android.provider.Settings
import android.util.DisplayMetrics
import java.io.BufferedReader
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.io.InputStream
import java.io.InputStreamReader

object Utils {
Expand Down Expand Up @@ -40,4 +44,46 @@ object Utils {
}
}

@JvmStatic
fun Context.copyAssetsToDir(assetDir: String, outDir: String) {
val assetManager = assets
val outDirFile = File(outDir)

try {
val files = assetManager.list(assetDir) ?: return

// Ensure the output directory exists
if (!outDirFile.exists()) {
outDirFile.mkdirs()
}

for (filename in files) {
val assetPath = "$assetDir/$filename"
val outFile = File(outDirFile, filename)

// Check if this is a directory or a file
if (assetManager.list(assetPath)?.isNotEmpty() == true) {
// If it's a directory, recursively copy its contents
copyAssetsToDir(assetPath, outFile.absolutePath)
} else {
// If it's a file, copy it
val inStream: InputStream = assetManager.open(assetPath)
val outStream = FileOutputStream(outFile)

val buffer = ByteArray(1024)
var bytesRead: Int
while (inStream.read(buffer).also { bytesRead = it } != -1) {
outStream.write(buffer, 0, bytesRead)
}

inStream.close()
outStream.flush()
outStream.close()
}
}
} catch (e: IOException) {
e.printStackTrace()
}
}

}

0 comments on commit 33d6af7

Please sign in to comment.