【问题标题】:iOS zip file Unzipping code issueiOS zip文件解压缩代码问题
【发布时间】:2016-05-05 08:40:15
【问题描述】:

我想解压缩从网上下载的 zip 文件,但我的问题是 zip 文件没有在 if-else 语句中解压缩。

如果有错的话,请指导我。

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
NSURL *url = [NSURL URLWithString:@"http://www.icodeblog.com/wp-content/uploads/2012/08/zipfile.zip"];
NSError *error = nil;
// 2
NSData *data = [NSData dataWithContentsOfURL:url options:0 error:&error];
if(!error)
{
    // 3
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *path = [paths objectAtIndex:0];
    NSString *zipPath = [path stringByAppendingPathComponent:@"zipfile.zip"];

        [data writeToFile:zipPath options:0 error:&error];

       if(!error)
       {
            // TODO: Unzip
            ZipArchive *za = [[ZipArchive alloc] init];
            // 1
           if ([za UnzipOpenFile: zipPath]) {
                // 2
                BOOL ret = [za UnzipFileTo: path overWrite: YES];
                if (NO == ret){} [za UnzipCloseFile];

                // 3
                NSString *imageFilePath = [path stringByAppendingPathComponent:@"photo.png"];
                NSString *textFilePath = [path stringByAppendingPathComponent:@"text.txt"];
                NSData *imageData = [NSData dataWithContentsOfFile:imageFilePath options:0 error:nil];
                UIImage *img = [UIImage imageWithData:imageData];
                NSString *textString = [NSString stringWithContentsOfFile:textFilePath
                                                                 encoding:NSASCIIStringEncoding error:nil];

                // 4           
                dispatch_async(dispatch_get_main_queue(),^{
                    self.imageView.image = img;
                    self.label.text = textString;
                });
}
else
{
            NSLog(@"Error saving file %@",error);
        }
   }
    else
  {
        NSLog(@"Error downloading zip file: %@", error);
    }
    }
});

【问题讨论】:

    标签: ios objective-c zip unzip


    【解决方案1】:

    根据你的代码调试

    1) NSData *data = [NSData dataWithContentsOfURL:url options:0 error:&error];

    数据为“零”

    通过在代码行下面添加 plist

    <key>NSAppTransportSecurity</key>  
    <dict>  
      <key>NSAllowsArbitraryLoads</key><true/>  
    </dict>
    

    你会得到预期的输出

    【讨论】:

    • 接受并投票赞成对某些人有帮助的答案
    猜你喜欢
    • 1970-01-01
    • 2020-01-16
    • 1970-01-01
    • 1970-01-01
    • 2017-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多