【问题标题】:Not getting eject file after unzipping downloaded file URL解压缩下载的文件 URL 后没有弹出文件
【发布时间】:2019-07-25 06:39:21
【问题描述】:

首先,使用 Alamofire 从 URL 成功下载后,我将文件扩展名更改为 .ZIP,然后在解压缩时出错。 没有得到预期的文件。

let destination = DownloadRequest.suggestedDownloadDestination(for: .documentDirectory)

    Alamofire.download(fileURL!, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: headers, to: destination).downloadProgress(closure: { (progress) in
            print(progress.completedUnitCount)
        }).responseData { (responce) in
            let destiUrl = responce.destinationURL
            print(destiUrl!)
            let newUrl = destiUrl?.deletingPathExtension().appendingPathExtension("zip")
            do {
                try FileManager.default.copyItem(at: destiUrl!, to: newUrl!)

                let unzipDirectory = try Zip.quickUnzipFile(newUrl!)
                print(unzipDirectory.absoluteString)
           }
            catch let error as NSError{
                print(error)
            }
    }

下载成功后的文件地址-->

file:///var/mobile/Containers/Data/Application/9D96958C-903E-4693-9965-6FB919BB24F1/Documents/'87dc4a8ddce24cf9ad35a251d6a98195.hub'

转换为 .zip 后的文件 URL

file:///var/mobile/Containers/Data/Application/9D96958C-903E-4693-9965-6FB919BB24F1/Documents/'87dc4a8ddce24cf9ad35a251d6a98195.zip

解压后的最终网址

file:///var/mobile/Containers/Data/Application/9D96958C-903E-4693-9965-6FB919BB24F1/Documents/'87dc4a8ddce24cf9ad35a251d6a98195/

实际结果应该是音频文件。

【问题讨论】:

  • 您不能只是将文件扩展名更改为 zip 并期望它是 zip 存档。
  • 下载的文件是一个zip压缩包,出于安全原因,文件扩展名是从后端更改的。如果您没有收到问题,请不要标记为否定。
  • 解压时出现什么错误?
  • 解压后请查看最终网址

标签: ios swift


【解决方案1】:

尝试使用以下代码在成功下载时替换文件名。 -->

 func saveFileInDocDirectory(data: Data?, fileName: String?, successblock: @escaping (_ path: String?) -> Void) { // To add the image to cache for given identifier.

    let paths = NSSearchPathForDirectoriesInDomains( .documentDirectory, .userDomainMask, true)[0] as String
    let path = paths.appending("/\(fileName!)")
    if (FileManager.default.fileExists(atPath: path)) {
        try! FileManager.default.removeItem(atPath: path)
    } else {
        do {
            try data?.write(to: URL(fileURLWithPath: path, isDirectory: false))
            successblock(path)
        } catch {
            successblock(nil)
            print("Error while caching the data in cache folder.")
        }
    }

}

然后在 Alamofire 下载功能中使用 SSZipArchive 库解压缩 -->

Alamofire.download(fileURL!, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: headers, to: destination).downloadProgress(closure: { (progress) in
            print(progress.completedUnitCount)
        }).responseData { (responce) in
            let destiUrl = responce.destinationURL
            print(destiUrl!)
            let name = destiUrl?.deletingPathExtension().lastPathComponent
            self.saveFileInDocDirectory(data: responce.result.value, fileName: "\(name!).zip", successblock: { (path) in
                print(path!)
                var filepath = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true)[0]
                let url = URL(fileURLWithPath: filepath)
                do {
                    try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true, attributes: nil)
                    let done = SSZipArchive.unzipFile(atPath: path!, toDestination: url.path)
                    if done{
                        let items = try FileManager.default.contentsOfDirectory(atPath: url.path)
                        print(items)
                        let destinationUrl = url.appendingPathComponent(items[0])
                        print(destinationUrl)
                         try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
                        player = AVQueuePlayer(url: destinationUrl)
                        player.play()
                    }
                } catch let error as NSError{
                    print(error)
                }
            })

    }

【讨论】:

    猜你喜欢
    • 2014-05-26
    • 1970-01-01
    • 2021-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多