首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS - NSURLSessionTask解压缩文件下载

iOS - NSURLSessionTask解压缩文件下载
EN

Stack Overflow用户
提问于 2016-04-25 06:13:53
回答 3查看 628关注 0票数 1

我在didFinishDownloadingToURL:下载了文件,我想解压它。我的当前代码如下:

代码语言:javascript
复制
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
{
    for(NSMutableDictionary *downloadInfo in downloadingArray)
    {
        if([[downloadInfo objectForKey:kMZDownloadKeyTask] isEqual:downloadTask])
        {
            if (location)
            {
                NSString *srcPath = location.absoluteString;
                NSString *fullPathDst = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

                //unzip
                ZipArchive *zipArchive = [[ZipArchive alloc] init];
                [zipArchive UnzipOpenFile:srcPath Password:@"pasword"];
                [zipArchive UnzipFileTo:fullPathDst overWrite:YES];
                [zipArchive UnzipCloseFile];

                NSFileManager *fileManager = [NSFileManager defaultManager];
                NSError *error;
                NSArray *files = [fileManager contentsOfDirectoryAtPath:fullPathDst error:&error];

                NSLog(@"files: %@", files);
            }

            break;
        }
    }
}

files数组为空。我做错了什么?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-04-25 06:32:34

您不应该使用absoluteString,因为这包括一个方案(例如file://)。而是使用path方法。

票数 0
EN

Stack Overflow用户

发布于 2016-04-25 06:17:36

您没有写入文件名和文件类型:-

代码语言:javascript
复制
NSString *filepath = [[NSBundle mainBundle] pathForResource:@"ZipFileName" ofType:@"zip"];
票数 0
EN

Stack Overflow用户

发布于 2016-04-25 06:23:28

解压缩将在documents目录中创建一个包含文件的文件夹。请检查一下

代码语言:javascript
复制
NSString *zipPath = [[NSBundle mainBundle] pathForResource:zipFileName ofType:@"zip"];
NSString *destinationPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
[SSZipArchive unzipFileAtPath:zipPath toDestination:destinationPath];

从文档目录获取zip文件:-

代码语言:javascript
复制
NSString *filename = @"MyZipFile.zip";
NSArray  *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString * zipPath = [documentsDirectory stringByAppendingPathComponent:filename];

NSString *destinationPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
[SSZipArchive unzipFileAtPath:zipPath toDestination:destinationPath];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36833207

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档