Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

First of all, after successfully download from the URL using Alamofire, I am changing the file extension to.ZIP, then getting an error while unzipping. Not getting expected file.

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 URL after successful download-->

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

File URL after converting to .zip

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

Final url after unzipping

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

Actual result should be audio file.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
114 views
Welcome To Ask or Share your Answers For Others

1 Answer

Tried replacing the name of the file at the time of successfully download using below code. -->

 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.")
        }
    }

}

And after that unzipped using SSZipArchive library in Alamofire download function -->

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)
                }
            })

    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...