Reputation: 41
I download a ZIP file containing an XML file. I unzip the file with SSZipArchive and safe the file in the app sandbox under the directory /library.
For instance:
/var/mobile/Containers/Data/Application/0A0091C2-7BC5-484F-BDBB-B2ABF305BB09/
The full absolute file path is:
/var/mobile/Containers/Data/Application/0A0091C2-7BC5-484F-BDBB-B2ABF305BB09/Libary/bgb.xml
The problem is strange, because if I change the file path (as absolute path) in the simulator to my /Download/bgb.xml directory, I can load the file.
Here is my code:
var filepath = IOOPs().getDirectoryPath().stringByAppendingString("/bgb.xml")
if(NSFileManager.defaultManager().fileExistsAtPath(filepath)){
print(" File xml existiert")
var filedata : NSData
if let tmpData = NSData(contentsOfFile: filepath){
parser = NSXMLParser(data: NSData(contentsOfFile: filepath)!)
}
}
parser.delegate = self
parser.parse()
fileExistsAtPath(file path)
-> evaluates true
NSData(contentsOfFile: filepath)
-> evaluates Null/Nil
The problem only occurs with a with path from the simulator or the mobile.
Here is the code for downloading and unzipping:
let datei = NSData(contentsOfURL: NSURL(string: "http://www.gesetze-im-internet.de/bgb/xml.zip")!)!
print ( getDirectoryPath())
print("bgb.zip")
speicherGesetzAufMobilemEndgerät(getDirectoryPath().stringByAppendingString("/bgb.zip"), datei: datei )
unzipFile(getDirectoryPath().stringByAppendingString("/bgb.zip"), destinationPath: getDirectoryPath().stringByAppendingString("/bgb.xml") )
func unzipFile(myZipFile: NSString, destinationPath : NSString){
let unZipped = SSZipArchive.unzipFileAtPath(myZipFile as! String, toDestination: destinationPath as! String);}
Upvotes: 4
Views: 1291
Reputation: 2494
Try creating NSURL
from local path:
NSData(contentsOfURL: NSURL(fileURLWithPath: filePath))
Upvotes: 0