如何下载和放大器;解压iOS上的文件 [英] How to download & unzip files on iOS

查看:109
本文介绍了如何下载和放大器;解压iOS上的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'd like to download a zip file containing mp3s for my app. Then, I will need to unzip it into a permanent directory, which will contain the mp3s to be played on demand. This is a vocabulary app and the zip file contains the mp3s to be extracted. The zip file as about 5 MB.

更多问题:什么是好的目录将这些下载到?怎么办解压?此外,文件,或者更确切地说,web目录他们在,有密码保护的,所以我需要提供用户名和密码。

More questions: What's a good directory to download these to? How to do unzipping? Also, the files, or rather, the web directory they are in, is password protected, so I will need to provide the name and password.

有没有人有任何一般指针?我特别想知道如何提供用户名/密码,下载到,如何解压文件,以及如何下载最好的目录。任何code样品将大大AP preciated。

Does anyone have any general pointers? In particular, I'd like to know how to provide the user name / password, the best directory to download to, how to unzip the file, and how to download. Any code samples would be greatly appreciated.

推荐答案

第一步,下载密码保护的文件,你需要一个NSURLConnection的,它在需要实现类的 NSURLConnectionDelegate 的协议,以处理身份验证请求。 <一href=\"https://developer.apple.com/library/mac/#documentation/Foundation/Reference/NSURLConnectionDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intf/NSURLConnectionDelegate\">Docs这里。

First step, to download password protected files you need an NSURLConnection, the class it's in needs to implement the NSURLConnectionDelegate protocol in order to handle authentication requests. Docs here.

为了永久地存储这些,你必须将它们保存到应用程序文件目录。 (记住,默认情况下,在这里的所有文件都备份到iCloud中,有大量的MP3在这里将带动iCloud的备份大小太远,苹果可能会拒绝你的应用程序了点。对于这个简单的解决方法是直接关闭的iCloud备份每个文件下载/解压缩到您的文档目录)。

In order to store these permanently, you have to save them to the app Documents directory. (Bear in mind that by default all files in here are backed up to iCloud, having lots of MP3s in here will drive the iCloud backup size too far and Apple may reject your app for that. Simple fix for this is to just turn off iCloud backup for each file you download/unzip to your documents directory).

接下来,解压缩是相当简单的,如果你有合适的工具,我有这个使用的目标 - 邮编库。一些方便code样品中的维基在此使用。

Next, unzipping is fairly straightforward if you have the right tools, I've had great success implementing this using the Objective-Zip library. Some handy code samples in the Wiki on the usage of this.

所以你的情况,这个过程将是沿着线:

So in your case, the process will be along the lines of:


  1. 要在服务器创建一个 NSURLConnection的,使用认证挑战委托方法提示时提供的用户名和密码。

  2. 使用类似下面code座的NSURLConnection的下载委托。 这是比较安全的做法是,你接受它接收的字节附加到磁盘上的文件(而不是把它追加到NSMutableData对象),如果你的zip文件太大而完全在内存中保存,你会遇到频繁死机

  1. Create an NSURLConnection to the server, providing the username and password when prompted using the authentication challenge delegate methods.
  2. Use the NSURLConnection download delegates similar to the below code block. It's safer practice to append the received bytes to the file on disk as you receive it (rather than keep appending it to an NSMutableData object), if your zip files are too large to keep entirely in memory you'll experience frequent crashes.

// 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文件到文件目录的所需的子文件夹,这些文件就可以使用!

  • You should now have unzipped the downloaded zip file to your desired sub-folder of the Documents directory, and the files are ready to be used!

    这篇关于如何下载和放大器;解压iOS上的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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