如何在Objective C中解压缩,解压缩文件(arm64支持)

时间:2014-01-07 17:02:30

标签: ios objective-c zip unzip bzip2

我正在寻找一种使用arm64支持在ios上压缩和解压缩文件的方法。我正在寻找Github,但他们不支持arm64。是否已有一个可立即使用的zip和解压缩文件库。 (如果你知道如何整合7zip,bzip2,gzip,rar的话,我会爱你的)

我现在正在使用SSZipArchive,但它无效:

#import "SSZipArchive.h"

NSArray *nameWithoutExtention = [[[[self files] objectAtIndex:indexPath.row] lastPathComponent] componentsSeparatedByString:@"."];
NSString *fileName = [nameWithoutExtention objectAtIndex:0];
NSString *destPath = [NSString stringWithFormat:@"%@/%@", self.directory, fileName];
[fileManager createDirectoryAtPath:destPath withIntermediateDirectories:YES attributes:nil error:nil];
[SSZipArchive unzipFileAtPath:[[self files] objectAtIndex:indexPath.row] toDestination:destPath];

1 个答案:

答案 0 :(得分:1)

您可以查看official SSZipArchive Objective-C example(适用于iOS11,它是纯arm64)。

要压缩:

NSString *sampleDataPath = [[NSBundle mainBundle].bundleURL
                            URLByAppendingPathComponent:@"Sample Data"
                            isDirectory:YES].path;
NSString *zipPath = [self tempZipPath];
BOOL success = [SSZipArchive createZipFileAtPath:zipPath
                         withContentsOfDirectory:sampleDataPath];

要解压缩:

NSString *unzipPath = [self tempUnzipPath];
BOOL success = [SSZipArchive unzipFileAtPath:zipPath
                               toDestination:unzipPath];

tempZipPathtempUnzipPath被声明为:

- (NSString *)tempZipPath {
  NSString *path = [NSString stringWithFormat:@"%@/\%@.zip",
                    NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0],
                    [NSUUID UUID].UUIDString];
  return path;
}

- (NSString *)tempUnzipPath {
  NSString *path = [NSString stringWithFormat:@"%@/\%@",
                    NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0],
                    [NSUUID UUID].UUIDString];
  NSURL *url = [NSURL fileURLWithPath:path];
  NSError *error = nil;
  [[NSFileManager defaultManager] createDirectoryAtURL:url
                           withIntermediateDirectories:YES
                                            attributes:nil
                                                 error:&error];
  if (error) {
    return nil;
  }
  return url.path;
}