Skip to content

Commit

Permalink
文件复制添加 overwrite 参数
Browse files Browse the repository at this point in the history
  • Loading branch information
lulululbj committed Jul 26, 2019
1 parent ac8bb08 commit 764e5b3
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
7 changes: 3 additions & 4 deletions app/src/main/java/luyao/box/FileExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,12 @@ fun File.moveTo(destFile: File, override: Boolean, reserve: Boolean): Boolean {
/**
* [destFolder] dest folder
* [override] whether to override dest file/folder if exist
* [reserve] Whether to reserve source file/folder
* [func] progress callback (from 0 to 100)
*/
fun File.moveToWithProgress(destFolder: File, override: Boolean, reserve: Boolean, func: (file:File,i: Int) -> Unit) {
fun File.moveToWithProgress(destFolder: File, overwrite: Boolean, func: (file:File,i: Int) -> Unit) {

if (isDirectory) copyFolder(this, File(destFolder,name), func)
else copyFile(this, File(destFolder,name), func)
if (isDirectory) copyFolder(this, File(destFolder,name),overwrite, func)
else copyFile(this, File(destFolder,name),overwrite, func)
}


Expand Down
19 changes: 14 additions & 5 deletions app/src/main/java/luyao/box/FileUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,18 @@ import java.nio.ByteBuffer
* on 2019/7/23 9:29
*/

fun copyFile(sourceFile: File, destFile: File, func: (file: File, i: Int) -> Unit) {
fun copyFile(sourceFile: File, destFile: File, overwrite: Boolean, func: (file: File, i: Int) -> Unit) {

if (!sourceFile.exists()) return

if (destFile.exists()) {
val stillExists = if (!overwrite) true else !destFile.delete()

if (stillExists) {
return
}
}

if (!destFile.exists()) destFile.createNewFile()

val inputStream = FileInputStream(sourceFile)
Expand Down Expand Up @@ -44,7 +53,7 @@ fun copyFile(sourceFile: File, destFile: File, func: (file: File, i: Int) -> Uni
outputStream.close()
}

fun copyFolder(sourceFolder: File, destFolder: File, func: (file: File, i: Int) -> Unit) {
fun copyFolder(sourceFolder: File, destFolder: File, overwrite: Boolean, func: (file: File, i: Int) -> Unit) {
if (!sourceFolder.exists()) return

if (!destFolder.exists()) {
Expand All @@ -54,17 +63,17 @@ fun copyFolder(sourceFolder: File, destFolder: File, func: (file: File, i: Int)

for (subFile in sourceFolder.listFiles()) {
if (subFile.isDirectory) {
copyFolder(subFile, File("${destFolder.path}${File.separator}${subFile.name}"), func)
copyFolder(subFile, File("${destFolder.path}${File.separator}${subFile.name}"), overwrite, func)
} else {
copyFile(subFile, File(destFolder, subFile.name), func)
copyFile(subFile, File(destFolder, subFile.name), overwrite, func)
}
}
}

fun main() {
val sourceFile = File("D://src.zip")
val destFile = File("D://src2.zip")
copyFile(sourceFile, destFile) { _, progress ->
copyFile(sourceFile, destFile, true) { _, progress ->
run {
println(progress.toString())
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/luyao/box/ui/file/FileViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class FileViewModel : BaseViewModel() {
fun pasteAsync(fileList: List<IFile>, destFolder: File, reserved: Boolean) {
CoroutineScope(Dispatchers.IO).launch {
for (file in fileList) {
file.getFile().moveToWithProgress(destFolder, true, reserved) { file, progress ->
file.getFile().moveToWithProgress(destFolder, true) { file, progress ->
launchOnUITryCatch({
mCurrentFileName.value = file.name
mProgress.value = progress
Expand Down

0 comments on commit 764e5b3

Please sign in to comment.