4

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);}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Tacitus
  • 41
  • 3
  • I get the error.description: "The file “bgb.xml” couldn’t be opened because you don’t have permission to view it." – Tacitus Nov 18 '15 at 15:13

1 Answers1

0

Try creating NSURL from local path:

NSData(contentsOfURL: NSURL(fileURLWithPath: filePath))
katleta3000
  • 2,484
  • 1
  • 18
  • 23