From 764e5b31bf33f43e2940e5f7f0a0dadcbf31a226 Mon Sep 17 00:00:00 2001 From: luyao Date: Fri, 26 Jul 2019 16:53:53 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=A4=8D=E5=88=B6=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=20overwrite=20=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/luyao/box/FileExt.kt | 7 +++---- app/src/main/java/luyao/box/FileUtils.kt | 19 ++++++++++++++----- .../java/luyao/box/ui/file/FileViewModel.kt | 2 +- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/luyao/box/FileExt.kt b/app/src/main/java/luyao/box/FileExt.kt index 38e533b..0555f2c 100644 --- a/app/src/main/java/luyao/box/FileExt.kt +++ b/app/src/main/java/luyao/box/FileExt.kt @@ -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) } diff --git a/app/src/main/java/luyao/box/FileUtils.kt b/app/src/main/java/luyao/box/FileUtils.kt index 77a5da0..6f57802 100644 --- a/app/src/main/java/luyao/box/FileUtils.kt +++ b/app/src/main/java/luyao/box/FileUtils.kt @@ -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) @@ -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()) { @@ -54,9 +63,9 @@ 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) } } } @@ -64,7 +73,7 @@ fun copyFolder(sourceFolder: File, destFolder: File, func: (file: File, i: Int) 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()) } diff --git a/app/src/main/java/luyao/box/ui/file/FileViewModel.kt b/app/src/main/java/luyao/box/ui/file/FileViewModel.kt index e463a5e..4ede031 100644 --- a/app/src/main/java/luyao/box/ui/file/FileViewModel.kt +++ b/app/src/main/java/luyao/box/ui/file/FileViewModel.kt @@ -32,7 +32,7 @@ class FileViewModel : BaseViewModel() { fun pasteAsync(fileList: List, 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