File Archive/UnArchive 文件压缩/解压

1、ZipArchive 方式

  • ZipArchive 只能对 zip 类文件进行压缩和解压缩

  • GitHub 网址:ZipArchive

  • ZipArchive 使用 ARC

  • 添加 ZipArchive

    1
    2
    3
    4
    5
    6
    7
    8
    // 添加第三方库文件
    ZipArchive

    // 包含系统动态库
    libz.tbd (libz.dylib)

    // 包含头文件
    #import "ZipArchive.h"
  • ZipArchive 压缩

    • 文件压缩

      1
      2
      3
      4
      5
      6
      7
      8
      9
      // 目标路径
      NSString *destinationFilePath = @"/Users/JHQ0228/Desktop/test.zip";

      // 源路径,待压缩的文件
      NSArray *resourcesFilePaths = @[@"/Users/JHQ0228/Desktop/test1.rtf",
      @"/Users/JHQ0228/Desktop/test2.rtf"];

      BOOL result = [SSZipArchive createZipFileAtPath:destinationFilePath
      withFilesAtPaths:resourcesFilePaths];
    • 文件夹压缩

      1
      2
      3
      4
      5
      6
      7
      8
      // 目标路径
      NSString *destinationFilePath = @"/Users/JHQ0228/Desktop/test.zip";

      // 源路径,待压缩的文件夹
      NSString *resourcesDirPath = @"/Users/JHQ0228/Desktop/test";

      BOOL result = [SSZipArchive createZipFileAtPath:destinationFilePath
      withContentsOfDirectory:resourcesDirPath];
  • ZipArchive 解压缩

    • 普通解压

      1
      2
      3
      4
      5
      6
      7
      8
      // 源路径,待解压缩的文件
      NSString *resourcesFilePath = @"/Users/JHQ0228/Desktop/test.zip";

      // 目标路径
      NSString *destinationDirPath = @"/Users/JHQ0228/Desktop/test";

      BOOL result = [SSZipArchive unzipFileAtPath:resourcesFilePath
      toDestination:destinationDirPath];
    • 密码解压

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      // 源路径,待解压缩的文件
      NSString *resourcesFilePath = @"/Users/JHQ0228/Desktop/test.zip";

      // 目标路径
      NSString *destinationDirPath = @"/Users/JHQ0228/Desktop/test";

      BOOL result = [SSZipArchive unzipFileAtPath:resourcesFilePath
      toDestination:destinationDirPath
      overwrite:YES
      password:@"password"
      error:nil];
    • 协议解压

      1
      2
      3
      4
      5
      6
      7
      8
      9
      // 源路径,待解压缩的文件
      NSString *resourcesFilePath = @"/Users/JHQ0228/Desktop/test.zip";

      // 目标路径
      NSString *destinationDirPath = @"/Users/JHQ0228/Desktop/test";

      BOOL result = [SSZipArchive unzipFileAtPath:resourcesFilePath
      toDestination:destinationDirPath
      delegate:self];
    • 协议密码解压

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      // 源路径,待解压缩的文件
      NSString *resourcesFilePath = @"/Users/JHQ0228/Desktop/test.zip";

      // 目标路径
      NSString *destinationDirPath = @"/Users/JHQ0228/Desktop/test";

      BOOL result = [SSZipArchive unzipFileAtPath:resourcesFilePath
      toDestination:destinationDirPath
      overwrite:YES
      password:@"password"
      error:NULL
      delegate:self];
    • 协议方法

      1
      2
      3
      4
      - (void)zipArchiveProgressEvent:(unsigned long long)loaded total:(unsigned long long)total {

      NSLog(@"%f", (float)loaded/total);
      }

2、SARUnArchiveANY 方式

  • SARUnArchiveANY 可以对 .zip, .rar, .7z 类文件进行压缩和解压缩。

  • GitHub 网址:SARUnArchiveANY

  • SARUnArchiveANY 使用 ARC

  • 添加 SARUnArchiveANY

    1
    2
    3
    4
    5
    6
    7
    8
    9
    // 添加第三方库文件
    SARUnArchiveANY

    // 包含系统动态库
    libz.tbd (libz.dylib)

    // 包含头文件
    #import "SARUnArchiveANY.h"
    #import "LZMAExtractor.h"
  • SARUnArchiveANY 解压缩

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    - (void)unZip {
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Zip_Example" ofType:@"zip"];
    NSString *destPath = [self applicationDocumentsDirectory];
    [self unArchive:filePath andPassword:nil destinationPath:destPath];
    }

    - (void)unRar {
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"example" ofType:@"rar"];
    NSString *destPath = [self applicationDocumentsDirectory];
    [self unArchive:filePath andPassword:nil destinationPath:destPath];
    }

    - (void)unZip_pwd {
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Zip_Example_pwd" ofType:@"zip"];
    NSString *destPath = [self applicationDocumentsDirectory];
    [self unArchive:filePath andPassword:@"SARUnArchiveANY_ZIP" destinationPath:destPath];
    }

    - (void)unRar_pwd {
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"example_pwd" ofType:@"rar"];
    NSString *destPath = [self applicationDocumentsDirectory];
    [self unArchive:filePath andPassword:@"SARUnArchiveANY_RAR" destinationPath:destPath];
    }

    - (void)Unzip7z {
    NSString *archiveFilename = @"example.7z";
    NSString *archiveResPath = [[NSBundle mainBundle] pathForResource:archiveFilename ofType:nil];
    NSAssert(archiveResPath, @"can't find .7z file");
    NSString *destPath = [self applicationDocumentsDirectory];
    [self unArchive:archiveResPath andPassword:nil destinationPath:destPath];
    }

    - (void)unArchive: (NSString *)filePath andPassword:(NSString*)password destinationPath:(NSString *)destPath {

    NSAssert(filePath, @"can't find filePath");

    SARUnArchiveANY *unarchive = [[SARUnArchiveANY alloc] initWithPath:filePath];

    if (password != nil && password.length > 0) {
    unarchive.password = password;
    }

    if (destPath != nil) {

    // (Optional). If it is not given, then the file is unarchived in the same location of its archive/file.
    unarchive.destinationPath = destPath;
    }

    unarchive.completionBlock = ^(NSArray *filePaths) {

    NSLog(@"For Archive : %@", filePath);

    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"US Presidents://"]]) {

    NSLog(@"US Presidents app is installed.");
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"US Presidents://"]];
    }

    for (NSString *filename in filePaths) {
    NSLog(@"File: %@", filename);
    }
    };

    unarchive.failureBlock = ^(){
    NSLog(@"Cannot be unarchived");
    };

    [unarchive decompress];
    }

    - (void)handleFileFromURL:(NSString *)filePath {
    NSLog(@"********* FILES FROM THE OTHER APPS *********");
    [self unArchive:filePath andPassword:nil destinationPath:nil];
    }

    - (NSString *) applicationDocumentsDirectory {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    return basePath;
    }
文章目录
  1. 1. 1、ZipArchive 方式
  2. 2. 2、SARUnArchiveANY 方式
隐藏目录