Swift - 合并文件
Swift - merge files
如何合并 Swift / iOS 中的文件? FileManager
可以移动和复制项目,但我没有看到任何关于合并文件的信息。我想要像
这样的东西
FileManager.default.merge(files: [URL], to location: URL) throws
文件可能很大,所以我宁愿避免在内存中传递它们的数据。
===这里是我自己的内存合并:
let data = NSMutableData()
files.forEach({ partLocation in
guard let partData = NSData(contentsOf: partLocation) else { return }
data.append(partData as Data)
do {
try FileManager.default.removeItem(at: partLocation)
} catch {
print("error \(error)")
}
})
data.write(to: destination, atomically: true)
这是我自己的解决方案(感谢@Alexander 的指导)
extension FileManager {
func merge(files: [URL], to destination: URL, chunkSize: Int = 1000000) throws {
try FileManager.default.createFile(atPath: destination.path, contents: nil, attributes: nil)
let writer = try FileHandle(forWritingTo: destination)
try files.forEach({ partLocation in
let reader = try FileHandle(forReadingFrom: partLocation)
var data = reader.readData(ofLength: chunkSize)
while data.count > 0 {
writer.write(data)
data = reader.readData(ofLength: chunkSize)
}
reader.closeFile()
})
writer.closeFile()
}
}
func merge(files: [URL], to destination: URL, chunkSize: Int = 100000000) {
for partLocation in files {
// create a stream that reads the data above
let stream: InputStream
stream = InputStream.init(url: partLocation)!
// begin reading
stream.open()
let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: chunkSize)
// var writeData : Data = Data()
while stream.hasBytesAvailable {
let read = stream.read(buffer, maxLength: chunkSize)
var writeData:Data = Data()
writeData.append(buffer, count: read) enter code here
if let outputStream = OutputStream(url: destination, append: true) {
outputStream.open()
writeData.withUnsafeBytes { outputStream.write([=10=], maxLength: writeData.count) }
outputStream.close()
writeData.removeAll()
}
}
stream.close()
buffer.deallocate(capacity: chunkSize)
}
}
如何合并 Swift / iOS 中的文件? FileManager
可以移动和复制项目,但我没有看到任何关于合并文件的信息。我想要像
FileManager.default.merge(files: [URL], to location: URL) throws
文件可能很大,所以我宁愿避免在内存中传递它们的数据。
===这里是我自己的内存合并:
let data = NSMutableData()
files.forEach({ partLocation in
guard let partData = NSData(contentsOf: partLocation) else { return }
data.append(partData as Data)
do {
try FileManager.default.removeItem(at: partLocation)
} catch {
print("error \(error)")
}
})
data.write(to: destination, atomically: true)
这是我自己的解决方案(感谢@Alexander 的指导)
extension FileManager {
func merge(files: [URL], to destination: URL, chunkSize: Int = 1000000) throws {
try FileManager.default.createFile(atPath: destination.path, contents: nil, attributes: nil)
let writer = try FileHandle(forWritingTo: destination)
try files.forEach({ partLocation in
let reader = try FileHandle(forReadingFrom: partLocation)
var data = reader.readData(ofLength: chunkSize)
while data.count > 0 {
writer.write(data)
data = reader.readData(ofLength: chunkSize)
}
reader.closeFile()
})
writer.closeFile()
}
}
func merge(files: [URL], to destination: URL, chunkSize: Int = 100000000) {
for partLocation in files {
// create a stream that reads the data above
let stream: InputStream
stream = InputStream.init(url: partLocation)!
// begin reading
stream.open()
let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: chunkSize)
// var writeData : Data = Data()
while stream.hasBytesAvailable {
let read = stream.read(buffer, maxLength: chunkSize)
var writeData:Data = Data()
writeData.append(buffer, count: read) enter code here
if let outputStream = OutputStream(url: destination, append: true) {
outputStream.open()
writeData.withUnsafeBytes { outputStream.write([=10=], maxLength: writeData.count) }
outputStream.close()
writeData.removeAll()
}
}
stream.close()
buffer.deallocate(capacity: chunkSize)
}
}