在FileHandle(或Outputstream)中快速合并文件块

时间:2019-04-10 18:59:14

标签: swift merge chunks filehandle

我想通过合并从输出流中从远程服务器下载的文件块来下载文件。我使用了以下代码块:

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)
    }
}

似乎我可以合并大块并下载文件。但是尝试时无法打开文件。我在做什么错了?

0 个答案:

没有答案