在iPhone上解压缩下载文件最简单的方法? [英] Simplest way on iPhone to unzip downloaded file?

查看:138
本文介绍了在iPhone上解压缩下载文件最简单的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标:下载压缩文件,解压缩并将其保存在iPhone应用程序的Documents目录中。

Goal: download a zipped file, unzip it, and save it in the iPhone app's Documents directory.

以下代码使用添加的initWithGzippedData方法在这里找到的Molecule应用程序中的NSData:
http://www.sunsetlakesoftware.com/molecules

The following code makes use of the initWithGzippedData method that was added to NSData in the Molecule app found here: http://www.sunsetlakesoftware.com/molecules

根据我的应用进行调整:

As adapted to my app:

NSString *sFolder = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *sFileName = [sFolder stringByAppendingPathComponent:@"MyFile.db"];
NSURL *oURL = [NSURL URLWithString: @"http://www.isystant.com/Files/MyFile.zip"];
NSData *oZipData = [NSData dataWithContentsOfURL: oURL];
NSData *oData = [[NSData alloc] initWithGzippedData:oZipData];
[oZipData release];
b = [oData writeToFile:sFileName atomically:NO];
NSLog(@"Unzip %i", b);

结果:已成功下载zip文件。从它开始,在Documents目录中创建一个新的,应该解压缩的文件,其中包含所需的名称(MyFile.db)但它没有字节。

Result: A zip file is successfully downloaded. From it a new, supposedly unzipped file is created in the Documents directory with the desired name (MyFile.db) but it has zero bytes.

任何人都能看到问题?或者是否有一种更简单的解压缩下载文件的方法,而不是Molecules应用程序中使用的文件?

Anybody see the problem? Or else is there a simpler way to unzip a downloaded file than the one used in the Molecules app?

推荐答案

我认为你的问题可能是您试图gzip-deflate Zip文件。这是两种不同的压缩算法。

I think that your problem may be that you are attempting to gzip-deflate a Zip file. Those are two different compression algorithms.

我基于分子中的gzip-deflating代码此NSData类别(我已将其代码复制到这个答案)由CocoaDev wiki的贡献者提供。你想要做的是使用他们的 -zlibDeflate 实现,它应该正确地解压缩Zip文件。

I based the gzip-deflating code in Molecules on this NSData category (the code of which I've copied into this answer) provided by the contributors to the CocoaDev wiki. What you'll want to do is use their -zlibDeflate implementation, which should properly unzip a Zip file.

与您的问题无关,而不是使用 NSHomeDirectory()并附加路径组件,建议的查找文档目录的方法如下:

Unrelated to your problem, instead of using NSHomeDirectory() and appending a path component, the recommended approach for finding the documents directory is the following:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

这篇关于在iPhone上解压缩下载文件最简单的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆