SSZipArchive "failed to open zip file" -- url.path is not the solution
<p>Edit 2:</p>
<p>I tried Zip from marmelroy (<a href="https://github.com/marmelroy/Zip" rel="nofollow noreferrer">https://github.com/marmelroy/Zip</a>) and it failed on the zip-file created witch <code>ZipArchive</code> too.</p>
<p>Then I created another zip-file with Zip and this one worked fine for unzipping.</p>
<p>There seems to be a problem with <code>ZipArchive</code> for me. I will use Zip instead and thats it for me ...</p>
<p>Thx for the answers !!</p>
<p>End edit 2.</p>
<p>I need to unzip certain files so I manually installed <code>SSZipArchive</code>.
Copied all 3 folders (<code>SSZipArchive</code>, <code>minizip</code>, <code>aes</code>) to project, added <code>#import &quot;ZipArchive.h&quot;</code> in my bridging.h and all builds nicely.</p>
<p>Edit:</p>
<p>Used Carthage as recommended, but same behavior.</p>
<p>End edit.</p>
<p>I use <code>Xcode 8 / Swift 3</code></p>
<p>After a few tests with no unzipping any file, I created my own zip using <code>SSZipArchive</code>.</p>
<pre><code>    let file = &quot;file.txt&quot;

    let text = &quot;some text&quot; //just a text
    
    let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first
    
    let path = dir?.appendingPathComponent(file)
        
    //writing
    do {
        try text.write(to: path!, atomically: false, encoding: String.Encoding.utf8)
    }
    catch {
        print(&quot;Failed writing&quot;)
    }
</code></pre>
<p>Here I print() the Documentsdirectory:   [&quot;file.txt&quot;]</p>
<pre><code>    let zipPath = tempZipPath() //tempZipPath is taken from the SSZipArchiveExample except it uses DocumentDirectory instead of Cache...
    
    print(&quot;zipPath: \(zipPath)&quot;)

print zipPath: /var/mobile/Containers/Data/Application/EB439A61-07B9-4910-BF28-03E85C50B292/Documents/93428A1B-E5B6-434F-B049-632B7519B126.zip
    
    let success = SSZipArchive.createZipFile(atPath: zipPath, withContentsOfDirectory: (dir?.absoluteString)!)
    if success {
        print(&quot;zipped&quot;)
        
    } else {
        print(&quot; NOT zipped&quot;)
    }
    
</code></pre>
<p>Again the Documentsdirectories entries: [&quot;93428A1B-E5B6-434F-B049-632B7519B126.zip&quot;, &quot;file.txt&quot;]</p>
<pre><code>    // this is again taken from the original Example
    guard let unzipPath = tempUnzipPath() else {
        return
    }
    
    print(&quot;unzipPath: \(unzipPath)&quot;)

print unzipPath: /var/mobile/Containers/Data/Application/EB439A61-07B9-4910-BF28-03E85C50B292/Documents/E4FC7DE6-F21B-46D8-9953-DCBF86E2268E
</code></pre>
<p>Reading the entries of this new directory: []</p>
<pre><code>    let filePath = zipPath
    var fileSize : UInt64 = 0
    
    do {
        let attr : NSDictionary? = try FileManager.default.attributesOfItem(atPath: filePath) as NSDictionary?
        
        if let _attr = attr {
            fileSize = _attr.fileSize();
            print(&quot;fileSize: \(fileSize)&quot;)
        }
    } catch {
        print(&quot;Error: \(error)&quot;)
    }
</code></pre>
<p>this writes <code>22</code>. I also tried to read the zip-file which works fine</p>
<p>From this part on I was desperate what to do</p>
<pre><code>    let url = URL(fileURLWithPath: zipPath)
    
    print(&quot;url: \(url)&quot;)
    print(&quot;url.path: \(url.path)&quot;)
    print(&quot;url.absoluteString: \(url.absoluteString)&quot;)
    print(&quot;url.absoluteURL: \(url.absoluteURL)&quot;)
    print(&quot;url.absoluteURL.absoluteString: \(url.absoluteURL.absoluteString)&quot;)
</code></pre>
<p>The output of these lines look all good for me:</p>
<blockquote>
<p>url:
file:///var/mobile/Containers/Data/Application/EB439A61-07B9-4910-BF28-03E85C50B292/Documents/93428A1B-E5B6-434F-B049-632B7519B126.zip</p>
<p>url.path:
/var/mobile/Containers/Data/Application/EB439A61-07B9-4910-BF28-03E85C50B292/Documents/93428A1B-E5B6-434F-B049-632B7519B126.zip</p>
<p>url.absoluteString:
file:///var/mobile/Containers/Data/Application/EB439A61-07B9-4910-BF28-03E85C50B292/Documents/93428A1B-E5B6-434F-B049-632B7519B126.zip</p>
<p>url.absoluteURL:
file:///var/mobile/Containers/Data/Application/EB439A61-07B9-4910-BF28-03E85C50B292/Documents/93428A1B-E5B6-434F-B049-632B7519B126.zip</p>
<p>url.absoluteURL.absoluteString:
file:///var/mobile/Containers/Data/Application/EB439A61-07B9-4910-BF28-03E85C50B292/Documents/93428A1B-E5B6-434F-B049-632B7519B126.zip</p>
</blockquote>
<p>And then I tried them all (Strings of course. No url allowed)</p>
<pre><code>    var success2 = SSZipArchive.unzipFile(atPath: zipPath, toDestination: unzipPath)
    if !success2 {
        print(&quot;zipPath&quot;)
    }
    success2 = SSZipArchive.unzipFile(atPath: url.path, toDestination: unzipPath)
    if !success2 {
        print(&quot;url.path&quot;)
    }
    success2 = SSZipArchive.unzipFile(atPath: url.absoluteString, toDestination: unzipPath)
    if !success2 {
        print(&quot;url.absoluteString&quot;)
    }
    success2 = SSZipArchive.unzipFile(atPath: url.absoluteURL.path, toDestination: unzipPath)
    if !success2 {
        print(&quot;url.absoluteURL.path&quot;)
    }
    
</code></pre>
<p>And it prints every line, so they all failed. At the end here comes the &quot;long&quot; way with <code>unipFileAtPath</code>. I tried this one too with <code>zipPath</code>, ...
All the same results.</p>
<pre><code>    do {
        success2 = try SSZipArchive.unzipFileAtPath(url.path, toDestination: unzipPath, overwrite: true, password: nil, delegate: nil)
        if !success2 {
            return
        }
    } catch {
        print(&quot;error \(error)&quot;)
        print(&quot;error \(error.localizedDescription)&quot;)
    }
    
</code></pre>
<blockquote>
<p>error Error Domain=SSZipArchiveErrorDomain Code=-1 &quot;failed to open zip
file&quot; UserInfo={NSLocalizedDescription=failed to open zip file} error
failed to open zip file</p>
</blockquote>
<p>Here the to func to get the path-strings. btw. I modified <code>tempZipPath</code> so returns an URL or an URL.path</p>
<p>but hey: I was desperate</p>
<pre><code>func tempZipPath() -&gt; String {
    var path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
    path += &quot;/\(UUID().uuidString).zip&quot;
    return path
}

func tempUnzipPath() -&gt; String? {
    var path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
    path += &quot;/\(UUID().uuidString)&quot;
    let url = URL(fileURLWithPath: path)
    
    do {
        try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true, attributes: nil)
    } catch {
        return nil
    }
    
    return url.path
}
</code></pre>
<p>Any suggestions? Is there something to consider manually installing <code>SSZipArchive</code>?</p>
<p>The tip &quot;<code>use .path instead of .absoluteString</code>&quot; didn't work for me as u can see above.</p>
<p>a desperate <code>tartsigam</code></p>