Kotlin中的压缩文件夹

问题描述

我正在尝试创建文件夹的存档,然后删除文件夹:

GlobalScope.launch {
   withContext(dispatchers.IO) {
       val folder = File("./folder/")
       zip(folder,File("./folder.zip")) 
       folder.deleteRecursively()
   }
}

邮政编码有效,但是我无法删除已存档的文件夹。 folder.deleteRecursively()返回false,没有任何错误

这是我正在使用的邮政编码:

fun zip(unzipped: File,zipped: File) {
    if (!unzipped.exists() || !unzipped.isDirectory) return
    ZipOutputStream(bufferedoutputstream(FileOutputStream(zipped))).use { zos ->
        unzipped.walkTopDown().filter { it.absolutePath != unzipped.absolutePath }.forEach { file ->
            val zipFileName = file.absolutePath.removePrefix(unzipped.absolutePath).removePrefix(File.separator)
            val entry = ZipEntry("$zipFileName${(if (file.isDirectory) "/" else "" )}")
            zos.putNextEntry(entry)
            if (file.isFile) file.inputStream().use { it.copyTo(zos) }
        }
    }
}

我能想到此代码为什么失败的唯一原因是未关闭的流,但是我的代码中找不到任何内容

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)