我想通过合并从输出流中从远程服务器下载的文件块来下载文件。我使用了以下代码块:
if FileManager.default.createFile(atPath: filePath.path, contents: Data(), attributes: nil) {
merge(files: urlArray, to: filePath)
}
func merge(files: [URL], to destination: URL) {
do {
let file = try FileHandle(forWritingTo: filePath)
for partLocation in files {
let chunkSize = try! partLocation.resourceValues(forKeys: [URLResourceKey.fileSizeKey]).allValues.first?.value as! UInt64
file.seek(toFileOffset: UInt64(chunkSize))
file.write(try Data(contentsOf: partLocation, options: .mappedIfSafe))
}
file.closeFile()
}
catch {
print(error)
}
}
似乎我可以合并大块并下载文件。但是尝试时无法打开文件。我在做什么错了?