如何在iOS上下载和解压缩文件

我想为我的应用程序下载包含mp3的zip文件。 然后,我需要将其解压缩到一个永久目录中,该目录将包含要按需播放的mp3。 这是一个词汇应用程序,zip文件包含要提取的mp3。 zip文件大约5 MB。

更多问题:下载这些目录的好目录是什么? 解压缩怎么办? 此外,文件,或者更确切地说,它们所在的Web目录是受密码保护的,因此我需要提供名称和密码。

有人有任何一般指示吗? 特别是,我想知道如何提供用户名/密码,下载的最佳目录,如何解压缩文件以及如何下载。 任何代码示例将不胜感激。

第一步,要下载受密码保护的文件,您需要一个NSURLConnection,它所需的类需要实现NSURLConnectionDelegate协议才能处理身份validation请求。 文件在这里 。

要永久存储这些内容,您必须将它们保存到应用程序文档目录中。 (请记住,默认情况下,此处的所有文件都备份到iCloud,这里有大量的MP3会导致iCloud备份大小太远,Apple可能会拒绝你的应用程序。简单的解决方法就是关闭iCloud备份/下载/解压缩到文档目录的每个文件。

接下来,如果你有合适的工具,解压缩是相当简单的,我已经使用Objective-Zip库实现了这一function。 Wiki中的一些方便的代码示例使用了这个。

因此,在您的情况下,该过程将遵循以下方式:

  1. 创建服务器的NSURLConnection ,在使用身份validation质询委托方法提示时提供用户名和密码。
  2. 使用类似于以下代码块的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]; } 
  3. 现在你已经完全下载了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! } 
  4. 您现在应该已将下载的zip文件解压缩到Documents目录的所需子文件夹中,并且可以使用这些文件!