如何将下载的文件解压到文档目录中

发布于 2024-10-09 03:46:19 字数 2254 浏览 5 评论 0原文

我搜索解压在文档目录中下载的 .zip 文件。但我只发现了有关它们的问题,并且没有得到任何适合我的查询的合适答案。

每个人都建议下载一些名为“MiniZip”的 api 文件并使用它。但它的代码量很大,而且我不需要那么多代码。因此,如果我得到一些更少的代码来解压缩文件并使用它,那对我来说将是非常好的。它是从 URL 中下载的,与存储时完全相同,但我不知道如何解压缩并在文档目录中使用它。任何人都可以通过提供一些示例代码或建议我来帮助我。

以下代码是使用 url 下载我的 zip 文件的代码。

-(IBAction)download:(id)sender{

    NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://some url contains .zip file"]  cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];                                                                        
    NSURLConnection  *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

    if (theConnection) {
        // Inform the user that the download failed.
        recievedData=[[NSMutableData data ]retain];

    //  [recievedData writeToFile:path atomically:YES];
        NSLog(@"download ");
    }
    else {
        NSLog(@"download fail");
    }
}


-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{

        [recievedData setLength:0];
    }

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{

    [recievedData appendData:data];
}

- (void)connection:(NSURLConnection *)connection
  didFailWithError:(NSError *)error
{
        [connection release];

    [recievedData release];

    // inform the user
    NSLog(@"Connection failed! Error - %@ %@",
          [error localizedDescription],
          [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}



- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    // do something with the data
    // receivedData is declared as a method instance elsewhere
    NSLog(@"Succeeded! Received %d bytes of data",[recievedData length]);

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

    NSString *path=[documentsDirectory stringByAppendingPathComponent:@"books"];
    NSLog(@"value of the path is:%@",path);
    [recievedData writeToFile:[path stringByAppendingPathComponent:@"file"] atomically:YES];

    [connection release];
    [recievedData release];
}   

I searched for unzipping the .zip file which is downloaded in the document directory. but i found only questions about them and i didnt get any suitable answer which suits my query.

Every one is suggesting to download some api file called "MiniZip" and make use of that. but Its bulky code and that much code is not needed for me. So, it would be greatful for me if i get some less code to unzip the file and make use of it. Its getting downloaded from the url exactly as it was stored but im not getting how to unzip and make use of it in the documents directory. can any one please help me out by giving some sample code or suggesting me..

The following code is the code for downloading my zip file using the url.

-(IBAction)download:(id)sender{

    NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://some url contains .zip file"]  cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];                                                                        
    NSURLConnection  *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

    if (theConnection) {
        // Inform the user that the download failed.
        recievedData=[[NSMutableData data ]retain];

    //  [recievedData writeToFile:path atomically:YES];
        NSLog(@"download ");
    }
    else {
        NSLog(@"download fail");
    }
}


-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{

        [recievedData setLength:0];
    }

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{

    [recievedData appendData:data];
}

- (void)connection:(NSURLConnection *)connection
  didFailWithError:(NSError *)error
{
        [connection release];

    [recievedData release];

    // inform the user
    NSLog(@"Connection failed! Error - %@ %@",
          [error localizedDescription],
          [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}



- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    // do something with the data
    // receivedData is declared as a method instance elsewhere
    NSLog(@"Succeeded! Received %d bytes of data",[recievedData length]);

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

    NSString *path=[documentsDirectory stringByAppendingPathComponent:@"books"];
    NSLog(@"value of the path is:%@",path);
    [recievedData writeToFile:[path stringByAppendingPathComponent:@"file"] atomically:YES];

    [connection release];
    [recievedData release];
}   

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

失与倦" 2024-10-16 03:46:19

MiniZip 是否会给您带来任何性能问题?

如果没有,请查看 objective-zip

Does MiniZip present any performance problems to you at all?

If it doesn't, then check out objective-zip.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文