ios 使用Swift解压缩zip文件

lstz6jyr  于 2023-02-26  发布在  iOS
关注(0)|答案(5)|浏览(481)

我用Swift做了一个游戏,但我需要在我的网站上下载.zip文件,并在游戏中使用内容(图像)。实际上,我下载了.zip文件并将其存储在文档中,我需要将文件解压缩到同一个文档文件夹中。
我试过marmelroy的Zip、iOS9压缩模块和tidwall的DeflateSwift,但都不管用,如果能兼容iOS8就更好了。

qvsjd97n

qvsjd97n1#

我最近发布了一个Swift原生框架,允许您创建、读取和更新ZIP存档文件:ZIP Foundation.
它内部使用libcompression以获得出色的压缩性能。
解压缩文件基本上只有一行:

try fileManager.unzipItem(at: sourceURL, to: destinationURL)

一个带有上下文的完整示例如下所示:

let fileManager = FileManager()
let currentWorkingPath = fileManager.currentDirectoryPath
var sourceURL = URL(fileURLWithPath: currentWorkingPath)
sourceURL.appendPathComponent("archive.zip")
var destinationURL = URL(fileURLWithPath: currentWorkingPath)
destinationURL.appendPathComponent("directory")
do {
    try fileManager.createDirectory(at: destinationURL, withIntermediateDirectories: true, attributes: nil)
    try fileManager.unzipItem(at: sourceURL, to: destinationURL)
} catch {
    print("Extraction of ZIP archive failed with error:\(error)")
}

README on GitHub包含了更多的信息。所有的公共方法都有完整的文档,可以通过Xcode快速帮助获得。
我还写了一篇关于here性能特征的博客文章。

mbjcgjjk

mbjcgjjk2#

我刚刚找到了下面这个名为Minizip的库。
你可以使用Zip framework来解压缩你的文件。
样本代码

do {
    let filePath = NSBundle.mainBundle().URLForResource("file", withExtension: "zip")!
    let unzipDirectory = try Zip.quickUnzipFile(filePath) // Unzip
    let zipFilePath = try Zip.quickZipFiles([filePath], fileName: "archive") // Zip
}
catch {
  print("Something went wrong")
}

如果不适用于minizip,可以使用ZipArchive,它不是用swift编写的,而是用Objective-C编写的

q3qa4bjr

q3qa4bjr3#

我用SSZipArchive和一个桥接头来处理它:
1.将SSZipArchive目录拖放到项目中
1.创建一个{项目-模块-名称}-桥接标题.h文件,其中包含行***import“SSZipArchive. h”***
1.在构建设置中,拖动Swift编译器-代码生成-〉Objective-C桥接头中的桥接头。
1.在Swift代码中使用SSZipArchive。

vwoqyblh

vwoqyblh4#

此代码将文件下载到文档目录并解压缩zip文件
不要忘记安装ZIPFoundation

import ZIPFoundation

func downloadAndUnzip(from urlString: String, toFolder folderName: String) {
    guard let url = URL(string: urlString) else { return }
    let session = URLSession.shared
    let documentsDirectoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
    let folderURL = documentsDirectoryURL.appendingPathComponent(folderName, isDirectory: true)
    let fileURL = folderURL.appendingPathComponent(url.lastPathComponent)

    do { //Creates folder.
        try FileManager.default.createDirectory(at: folderURL, withIntermediateDirectories: true, attributes: nil)
    } catch {
        print("Could not create directory: \(error.localizedDescription)")
    }

    let task = session.downloadTask(with: url) { (tempLocalURL, response, error) in
        guard let tempLocalURL = tempLocalURL, error == nil else { return }
        
        do { //Moves data to created folder.
            try FileManager.default.moveItem(at: tempLocalURL, to: fileURL)
            print("Success: \(fileURL.path)")
        } catch {
            print("Could not move item: \(error.localizedDescription)")
        }
    }
    task.resume()
    
    do { //Unzips zip file.
        try FileManager.default.unzipItem(at: fileURL, to: fileURL.deletingPathExtension())
        print("Unzip completed!")
    } catch {
        print("Extraction of ZIP archive failed with error:\(error)")
    }
}
y1aodyip

y1aodyip5#

    • 重要事项:需要第三方库**

使用此library,您可以使用以下行:

let unzipDirectory= try Zip.quickUnzipFile(filePath) //to unZip your folder.

如果需要更多帮助,请查看存储库。

相关问题