解压缩文件夹并从解压缩文件中获取图像

时间:2015-02-10 08:31:54

标签: iphone ios8 unzip xcode6.1 nsdocumentdirectory-ios8

我已成功下载了zip文件并在ZipArchive的帮助下解压缩。但现在我必须从解压缩文件的子文件夹访问图像。解压缩文件夹保存在文档目录中。子文件夹名称始终更改。我使用以下代码解压缩zip文件:

    NSURL *zipUrl = [NSURL URLWithString:responseString];
    NSData *data = [NSData dataWithContentsOfURL:zipUrl options:0 error:&error];
    if(!error)
    {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
        NSLog(@"paths..%@",paths);
        NSString *path = [paths objectAtIndex:0];
        NSString *zipPath = [path stringByAppendingPathComponent:@"zipfile.zip"];

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

        if(!error)
        {
            ZipArchive *za = [[ZipArchive alloc] init];
            if ([za UnzipOpenFile: zipPath])
            {
                BOOL ret = [za UnzipFileTo: path overWrite: YES];
                if (NO == ret){} [za UnzipCloseFile];
            }
        }
        else
        {
            NSLog(@"Error saving file %@",error);
        }
    }
    else
    {
        NSLog(@"Error downloading zip file: %@", error);
    }

请帮帮我。 提前致谢。

2 个答案:

答案 0 :(得分:0)

使用NSFileManager方法检查解压缩文件的内容。试试

 - enumeratorAtURL:includingPropertiesForKeys:options:errorHandler:

进行深度枚举。

或者

 -contentsOfDirectoryAtURL:includingPropertiesForKeys:options:error:

用于浅层列举内容。

然后,您可以使用扩展程序过滤文件以查找图像。

答案 1 :(得分:0)

使用

NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:NULL];

获取目录列表。

您将从directoryContent数组中获取子文件夹名称。

相关问题