First I created 6 records with files using the Cloud Kit Dashbord. 1 rtf and 5 rtfd files.
The code below is my attempt to read the files from the records and write them to the document directory.
the rtf file works. The rtfd files fail either in the decompression code my attempt to read the files after they wer written to the Document directoy.
the rtfd files either
This is the print out from the failed attempt to create a attributedSting from files written to the Document directory
failed to read file: Bowman Cousins.rtfd
failed to read file: Chaffee Cousins.rtfd
failed to read file: Blasser Cousins.rtfd
the following files failed in the decompression code.
failed to read File: Babson Cousins.rtfd
writtn Data Cout: 0
failed to read File: Abel Belote.rtfd
This is the print out of reading the 6 CkAssets and writing them to the document folder.
Bowman Cousins.rtfd
20753 0x0000000108370000
writtn Data Cout: 20753
Bowman Cousins.rtfd ok This file decompressed and was written to the Document directory but failed to read
Axtell Cousins.rtf
Axtell Cousins.rtf ok
Babson Cousins.rtfd
0 0x00000001083c8000 This file failed to decompress.
writtn Data Cout: 0
failed to read File: Babson Cousins.rtfd
Blasser Cousins.rtfd
36701 0x000000010983c000
writtn Data Cout: 36701
Blasser Cousins.rtfd ok
Abel Belote.rtfd
0 0x0000000109b88000
writtn Data Cout: 0
failed to read File: Abel Belote.rtfd
Chaffee Cousins.rtfd
1635487 0x0000000109b88000
writtn Data Cout: 1635487
Chaffee Cousins.rtfd ok
This is the code that reads the files that were written to the Document directory.
This code works correctly when reading rtfd files from the Bundle or from the Document directory but fails if the filesare written to the document directory from the Cloud.
attributedStringWithRtf = try NSAttributedString( // Reads the RTF file from the Bundle
url: rtfPath!,
options: [NSAttributedString.DocumentReadingOptionKey.documentType : NSAttributedString.DocumentType.rtfd],
//options: [NSDocumentTypeDocumentAttribute : NSRTFDTextDocumentType],
documentAttributes: &d)
} catch{
print("failed to read file: " + aFileName)
return (false,nil)
}
This is the code That reads from the CKAsset and writes to the document directory.
func writeFile(record: CKRecord){
let fileAsset = record.value(forKey: "FileData") as! CKAsset
var fileName = record.value(forKey: "FileName") as! String
print(fileName)
let fileNameWords = fileName.split(separator: ".")
var fileData = NSData(contentsOfFile: fileAsset.fileURL.path)
if fileNameWords[fileNameWords.count - 1] == "rtfd"{
//fileName += ".zip"
let fileData3 = fileData! as? Data
// do {
//let fileDataUncompressed = try fileData3?.decompress(algorithm: .zlib)
// let fileDataUncompressed = try fileData3?.decompress()
let fileDataUncompressed = uncompress(data: fileData3!)
fileData = fileDataUncompressed as NSData
/*} catch {
print(error)
}
*/
}
if fileData?.length == 0 {
print("failed to read File: " + fileName)
return
}
let filePath = File.getPath(fileName)
if let aFilePath = filePath{
let ok = fileData?.write(toFile: aFilePath, atomically: true)
// let fileString = String(data: fileData! as Data,encoding:String.Encoding.utf8)
// let ok = File.write( aFilePath, content: fileString!)
//self.xmlString = String(data: xmlData as! Data, encoding: String.Encoding.utf8)
//let ok = fileData.write
if ok! {
print(fileName + " ok")
}
else {
print(fileName + " Not ok")
}
}
}
func uncompress(data: Data) -> Data{
//if let asset = records?.first?["asset"] as? CKAsset {
//print(asset)
//if let data = try? Data(contentsOf: asset.fileURL){
var writtenData: Data?
data.withUnsafeBytes(
{ (fromBytes: UnsafePointer<UInt8>) -> Void in
var toData = Data(capacity: data.count)
toData.withUnsafeMutableBytes({ (toBytes: UnsafeMutablePointer<UInt8>) -> Void in
let algo = compression_decode_buffer(toBytes, data.count, fromBytes, data.count, nil, COMPRESSION_ZLIB)
print(algo, toBytes)
let a = UnsafeMutableBufferPointer(start: toBytes, count: algo)
writtenData = Data(buffer: a)
print("writtn Data Cout: " + String(writtenData!.count))
})
})
return writtenData!
}