ios - 如何解压缩文件?

标签 ios objective-c zip archive unzip

我使用 SSZipArchive。下载文件后,我想解压缩存档并显示图像。但是代码不起作用。如何解决?

下载文件

 -(IBAction) downloadButton:(id)sender
    {
        if (_HighScore == 2) {

            _url1 =[NSURL URLWithString:@"link2.zip"];

            _downloadTask1 = [_session downloadTaskWithURL:_url1];

            [_downloadTask1 resume];
    }

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
{

    if (downloadTask == _downloadTask1) {

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
        NSURL *documentsFolder = [paths objectAtIndex:0];

        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSURL *newLocation = [NSURL URLWithString:[NSString stringWithFormat:@"file://%@/2.zip", documentsFolder]];

        NSError *error;
        [fileManager copyItemAtURL:location toURL:newLocation error:&error];
NSLog(@"file%@", newLocation.absoluteString);
}

解压文件

    _documentsDirectory1 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    _zipPath1 = [_documentsDirectory1 stringByAppendingPathComponent:@"2.zip"];
    _destinationPath1 = [_documentsDirectory1 stringByAppendingPathComponent:@"file://%@/2.zip"];
    _fileExists1 = [[NSFileManager defaultManager] fileExistsAtPath:_zipPath1 isDirectory:false];
    if( [SSZipArchive unzipFileAtPath:_zipPath1 toDestination:_destinationPath1] != NO ) {
        NSLog(@"Dilip Success");
    }else{
        NSLog(@"Dilip Error");
    }

UPD

-(IBAction) downloadButton:(id)sender
{
    if (_HighScore == 2) {

        _url1 =[NSURL URLWithString:@"link2.zip"];

        _downloadTask1 = [_session downloadTaskWithURL:_url1];

        [_downloadTask1 resume];
}
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
{

    if (downloadTask == _downloadTask1) {
        _documentsDirectory1 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        _zipPath1 = [_documentsDirectory1 stringByAppendingPathComponent:@"2.zip"];
    }
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    if (downloadTask == _downloadTask1) { NSData *urlData1 = [NSData dataWithContentsOfURL:_url1]; [urlData1 writeToFile:_zipPath1 atomically:YES];}

});
}

最佳答案

问题在于将 zip 文件保存在 DocumentDirectory 中,_filePath1 包含解压缩的目标文件夹,而不是您需要使用 filePath 包含 zip 文件名和路径,所以像这样使用它。

[urlData1 writeToFile:filePath atomically:YES];

另外,如果你使用writeToFile:options:error:方法,你可以知道它是否成功写入Data

NSError *error = nil;
[self.responseData writeToFile:zipPath options:0 error:&error];

编辑: 您可能遇到了问题,因此请将您的代码从下载更改为像这样保存和解压缩。

-(IBAction) downloadButton:(id)sender
{
    if (_HighScore == 2) {
        _url1 =[NSURL URLWithString:@"link2.zip"];  
        _downloadTask1 = [session dataTaskWithURL:[NSURL URLWithString:@""] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

            //Saved in NSDocumentDirectory
            NSError *error = nil;
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *path = [paths objectAtIndex:0];
            NSString *zipPath = [path stringByAppendingPathComponent:@"2.zip"];
            [self.responseData writeToFile:zipPath options:0 error:&error];

            //UNZip
            NSString *zipPath1 = [path stringByAppendingPathComponent:@"2.zip"];
            NSString *destinationPath = [NSString stringWithFormat:@"%@",path];
            [SSZipArchive unzipFileAtPath:zipPath1 toDestination:destinationPath];
            //Now access the content of zip
        }]; 
        [_downloadTask1 resume];
    }   
}

关于ios - 如何解压缩文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39443737/

相关文章:

ios - NSMutableArray 追加 NSMutableArray

objective-c - 如何更改表单模态视图 Controller 的位置

c# - 如何在不解压整个文件的情况下从 zip 文件中读取数据

PHP zip_open() 和 php ://temp, 似乎无法打开

iphone - if 语句推送到 els 上的 viewController 不起作用

iphone - 如何在应用程序中检测免打扰模式?

ios - 在 Swift 中使用 NSURLSession 下载文件

ios - 在 iOS 中播放视频失败

iphone - 从不同的 View Controller 调用时,UIView 动画不会触发?

PHP 下载脚本在 Mac 上创建不可读的 ZIP 文件