8

StoreKit 下载 IAP 内容包后,它会向我返回一个 NSURL,如下所示:

文件://localhost/private/var/mobile/Applications/45EF2B3A-3CAB-5A44-4B4A-631A122A4299/Library/Caches/BA32BC55-55DD-3AA4-B4AC-C2A456622229.zip/

尽管我发现所有消息来源都声称 StoreKit 会在下载内容包后解压缩,但它给了我一个 ZIP。此 ZIP 可能包含内容包的文件结构。但是我该如何解压呢?

4

2 回答 2

31

如果您使用 Swift 语言工作,请使用Zip Foundation 。它易于使用,是解压缩 zip 文件的最佳 swift 库之一。

压缩:

let fileManager = FileManager()
let currentWorkingPath = fileManager.currentDirectoryPath
var sourceURL = URL(fileURLWithPath: currentWorkingPath)
sourceURL.appendPathComponent("file.txt")
var destinationURL = URL(fileURLWithPath: currentWorkingPath)
destinationURL.appendPathComponent("archive.zip")
do {
    try fileManager.zipItem(at: sourceURL, to: destinationURL)
} catch {
    print("Creation of ZIP archive failed with error:\(error)")
}

解压缩:

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

如果您使用的是 Objective-C,那么SSZipArchive是最好的选择。

你可以用这个解压

NSArray  *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *outputPath = [documentsDirectory stringByAppendingPathComponent:@"/ImagesFolder"];

NSString *zipPath = Your zip file path;

[SSZipArchive unzipFileAtPath:zipPath toDestination:outputPath delegate:self];

希望它可以帮助你。

于 2013-04-19T13:20:26.083 回答
9

有一个很棒的 3rd 方工具,用于为 iPhone 压缩/解压缩文件

https://github.com/soffes/ssziparchive

使用非常简单。希望有帮助!!

编辑:

我创建的快速方法需要 url,下载 zip 并解压缩

-(void)downloadAndUnzip : (NSString *)sURL_p : (NSString *)sFolderName_p
{
    dispatch_queue_t q = dispatch_get_global_queue(0, 0);
    dispatch_queue_t main = dispatch_get_main_queue();
    dispatch_async(q, ^{
        //Path info
        NSURL *url = [NSURL URLWithString:sURL_p];
        NSData *data = [NSData  dataWithContentsOfURL:url];
        NSString *fileName = [[url path] lastPathComponent];
        NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:fileName];
        [data writeToFile:filePath atomically:YES];
        dispatch_async(main, ^


                 {
                       //Write To
                       NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                       NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
                       NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:sFolderName_p];

                       [SSZipArchive unzipFileAtPath:filePath toDestination:dataPath];

                   });
});

}
于 2013-04-19T12:56:07.147 回答