我想为我的应用程序下载一个包含 mp3 的 zip 文件。然后,我需要将其解压缩到一个永久目录中,其中包含要按需播放的 mp3。这是一个词汇应用程序,zip 文件包含要提取的 mp3。 zip 文件大约 5 MB。
更多问题: 将这些内容下载到哪个目录比较好?如何解压?此外,这些文件(或者更确切地说,它们所在的 Web 目录)受密码保护,因此我需要提供名称和密码。
有人有任何一般性的指示吗?特别是,我想知道如何提供用户名/密码、下载的最佳目录、如何解压缩文件以及如何下载。任何代码示例将不胜感激。
第一步,要下载受密码保护的文件,您需要一个 NSURLConnection,它所在的类需要实现NSURLConnectionDelegate
协议以处理身份验证请求。文档在这里 https://developer.apple.com/library/mac/#documentation/Foundation/Reference/NSURLConnectionDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intf/NSURLConnectionDelegate.
为了永久存储这些,您必须将它们保存到应用程序的文档目录中。 (请记住,默认情况下,此处的所有文件都会备份到 iCloud,此处有大量 MP3 会导致 iCloud 备份大小太大,Apple 可能会因此拒绝您的应用程序。解决此问题的简单方法是关闭 iCloud备份您下载/解压到文档目录的每个文件)。
接下来,如果您有合适的工具,解压缩相当简单,我已经使用以下方法成功实现了这一点Objective-Zip 库 http://code.google.com/p/objective-zip/。 Wiki 中的一些方便的代码示例介绍了它的用法。
因此,就您而言,该过程将遵循以下原则:
- 创建一个
NSURLConnection
到服务器,在使用身份验证质询委托方法提示时提供用户名和密码。
-
使用类似于以下代码块的 NSURLConnection 下载委托。更安全的做法是在收到文件时将收到的字节附加到磁盘上的文件(而不是继续将其附加到 NSMutableData 对象),如果您的 zip 文件太大而无法完全保留在内存中,您将经常遇到崩溃。
// Once we have the authenticated connection, handle the received file download:
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSFileManager *fileManager = [NSFileManager defaultManager];
// Attempt to open the file and write the downloaded data to it
if (![fileManager fileExistsAtPath:currentDownload]) {
[fileManager createFileAtPath:currentDownload contents:nil attributes:nil];
}
// Append data to end of file
NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:currentDownload];
[fileHandle seekToEndOfFile];
[fileHandle writeData:data];
[fileHandle closeFile];
}
-
现在你已经完全下载了 ZipFile,使用 Objective-Zip 解压它,应该看起来像这样(同样,此方法很棒,因为它可以缓冲文件,因此即使要解压缩的大文件也不会导致内存问题!):
-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
// I set buffer size to 2048 bytes, YMMV so feel free to adjust this
#define BUFFER_SIZE 2048
ZipFile *unzipFile = [[ZipFile alloc] initWithFileName:zipFilePath mode:ZipFileModeUnzip];
NSMutableData *unzipBuffer = [NSMutableData dataWithLength:BUFFER_SIZE];
NSArray *fileArray = [unzipFile listFileInZipInfos];
NSFileHandle *fileHandle;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *targetFolder = folderToUnzipToGoesHere;
[unzipFile goToFirstFileInZip];
// For each file in the zipped file...
for (NSString *file in fileArray) {
// Get the file info/name, prepare the target name/path
ZipReadStream *readStream = [unzipFile readCurrentFileInZip];
FileInZipInfo *fileInfo = [unzipFile getCurrentFileInZipInfo];
NSString *fileName = [fileInfo name];
NSString *unzipFilePath = [targetFolder stringByAppendingPathComponent:fileName];
// Create a file handle for writing the unzipped file contents
if (![fileManager fileExistsAtPath:unzipFilePath]) {
[fileManager createFileAtPath:unzipFilePath contents:nil attributes:nil];
}
fileHandle = [NSFileHandle fileHandleForWritingAtPath:unzipFilePath];
// Read-then-write buffered loop to conserve memory
do {
// Reset buffer length
[unzipBuffer setLength:BUFFER_SIZE];
// Expand next chunk of bytes
int bytesRead = [readStream readDataWithBuffer:unzipBuffer];
if (bytesRead > 0) {
// Write what we have read
[unzipBuffer setLength:bytesRead];
[fileHandle writeData:unzipBuffer];
} else
break;
} while (YES);
[readStream finishedReading];
[fileHandle closeFile];
// NOTE: Disable iCloud backup for unzipped file if applicable here!
/*...*/
[unzipFile goToNextFileInZip];
}
[unzipFile close]; // Be sure to also manage your memory manually if not using ARC!
// Also delete the zip file here to conserve disk space if applicable!
}
您现在应该已将下载的 zip 文件解压缩到 Documents 目录中所需的子文件夹中,并且文件已准备好使用!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)