title: How to extract local zip files using Swift with Zip library (SwiftUI)
body: <p>I'd like to download an archive file (zip extension) and extract it. This app downloads zip successfully, but it won't decompress zip file.<br></p>
  <blockquote>
  <p>Zip framework library: <a href="https://github.com/marmelroy/Zip" rel="nofollow noreferrer">https://github.com/marmelroy/Zip</a><br>
  Disabled sandbox (Xcode)<br>
  Allowed arbitrary loads (HTTP)</p>
  </blockquote>
  <p>Password of archive file is: gameInfos.gameBinaryName<br>
  Location of archive file would be:</p>
  <pre><code>/Users/Shared/Bitmap Production/Games/\(gameInfos.gameBinaryName)/\(gameInfos.gameBinaryName).zip
  </code></pre>
  <p>Destination folder would be like:</p>
  <pre><code>/Users/Shared/Bitmap Production/Games/\(gameInfos.gameBinaryName)
  </code></pre>
  <p>Using let variable zipPath, zipPathURL, zipPathURL1, zipPathURL2 didn't work and I dunno why.<br>
  The console says:</p>
  <pre><code>...
  Downloading: 1.0
  OX is downloaded to your computer.
  Decompressing of OX failed. Check /Users/Shared/Bitmap Production/Games/OX/OX.zip and decompress it manually. Error code: unzipFail
  
  </code></pre>
  <p>This code is summarized. Full code is here: <a href="https://github.com/bmplatina/BitmapMac" rel="nofollow noreferrer">https://github.com/bmplatina/BitmapMac</a></p>
  <pre class="lang-swift prettyprint-override"><code>import SwiftUI
  import Foundation
  import Alamofire
  import Zip
  
  let bitmapHomeFolder = &quot;/Users/Shared/Bitmap Production&quot;
  let bitmapGameFolder = bitmapHomeFolder + &quot;/Games&quot;
  
  struct gameInfo: Codable { // This is from server's api
      // ...
      var gameDownloadMacURL: String = &quot;&quot; // Download Link for macOS Platform
      var gameTitle: String = &quot;OX&quot; // &quot;OX&quot; is for example
      var gameBinaryName: String = &quot;OX&quot; // &quot;OX&quot; is for example
  }
  
  struct gameButtons: View {
      var gameInfos: gameInfo
  
      var body: some View {
          Button(action: { downloadGame() }) {
              Text(&quot;Download&quot;)
      }
      func downloadGame() {
          let fileManager = FileManager.default
          let appURL = fileManager.urls(for: .userDirectory, in: .localDomainMask)[0]
          let fileURL = appURL.appendingPathComponent(&quot;Shared/Bitmap Production/Games/\(gameInfos.gameBinaryName)/\(gameInfos.gameBinaryName).zip&quot;)
          let destination: DownloadRequest.Destination = { _, _ in
              return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
          }
          AF.download(gameInfos.gameDownloadMacURL, method: .get, parameters: nil, encoding: JSONEncoding.default, to: destination).downloadProgress { (progress) in
              print(&quot;Downloading: \(progress.fractionCompleted)&quot;)
          }
          .response { response in
              if response.error != nil {
                  print(&quot;Download Failed. Check your internet connection.&quot;)
              }
              else {
                  print(&quot;\(gameInfos.gameTitle) is downloaded to your computer.&quot;)
                  unzipGame(zipPathURL: fileURL)
              }
          }
      }
  
      func unzipGame(zipPathURL: URL) {
          do {
              let fileManager = FileManager.default
              let gameDirectory = fileManager.urls(for: .userDirectory, in: .localDomainMask)[0]
              let zipPath = gameDirectory.appendingPathComponent(&quot;Shared/Bitmap Production/Games/\(gameInfos.gameBinaryName)/\(gameInfos.gameBinaryName).zip&quot;)
              // let zipPathURL1 = URL(fileURLWithPath: bitmapGameFolder + &quot;/\(gameInfos.gameBinaryName)/\(gameInfos.gameBinaryName).zip&quot;)
              let zipPathURL2 = URL(string: &quot;file:///Users/Shared/Bitmap%20Production/Games/\(gameInfos.gameBinaryName)/\(gameInfos.gameBinaryName).zip&quot;)!
              let destinationDirectory = gameDirectory.appendingPathComponent(&quot;Shared/Bitmap Production/Games/\(gameInfos.gameBinaryName)/&quot;)
              try Zip.unzipFile(zipPathURL2, destination: destinationDirectory, overwrite: true, password: gameInfos.gameBinaryName, progress: { (progress) -&gt; () in
                  print(&quot;Unzipping: \(progress)&quot;)
              }) // Unzip
          }
          catch {
              print(&quot;Decompressing of \(gameInfos.gameTitle) failed. Check \(bitmapGameFolder)/\(gameInfos.gameBinaryName)/\(gameInfos.gameBinaryName).zip and decompress it manually. Error code: \(error)&quot;)
          }
      }
  }
  </code></pre>