iOS

1、ZipArchive 方式

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

  • GitHub 网址:https://github.com/ZipArchive/ZipArchive

  • ZipArchive 使用 ARC

  • 添加 ZipArchive

    	// 添加第三方库文件
    	ZipArchive
    
    	// 包含系统动态库
    	libz.tbd (libz.dylib)
    
    	// 包含头文件
    	#import "ZipArchive.h"
    
  • ZipArchive 压缩

    • 文件压缩

      	// 目标路径
      	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];
      
    • 文件夹压缩

      	// 目标路径
      	NSString *destinationFilePath = @"/Users/JHQ0228/Desktop/test.zip";
      	
      	// 源路径,待压缩的文件夹
      	NSString *resourcesDirPath = @"/Users/JHQ0228/Desktop/test";
      
      	BOOL result = [SSZipArchive createZipFileAtPath:destinationFilePath 
      	                        withContentsOfDirectory:resourcesDirPath];
      
  • ZipArchive 解压缩

    • 普通解压

      	// 源路径,待解压缩的文件
      	NSString *resourcesFilePath = @"/Users/JHQ0228/Desktop/test.zip";
      	
      	// 目标路径
      	NSString *destinationDirPath = @"/Users/JHQ0228/Desktop/test";
      
      	BOOL result = [SSZipArchive unzipFileAtPath:resourcesFilePath 
      	                              toDestination:destinationDirPath];
      
    • 密码解压

      	// 源路径,待解压缩的文件
      	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];
      
    • 协议解压

      	// 源路径,待解压缩的文件
      	NSString *resourcesFilePath = @"/Users/JHQ0228/Desktop/test.zip";
      	
      	// 目标路径
      	NSString *destinationDirPath = @"/Users/JHQ0228/Desktop/test";
      
      	BOOL result = [SSZipArchive unzipFileAtPath:resourcesFilePath 
      	                              toDestination:destinationDirPath 
      	                                   delegate:self];
      
    • 协议密码解压

      	// 源路径,待解压缩的文件
      	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];
      
    • 协议方法

      	- (void)zipArchiveProgressEvent:(unsigned long long)loaded total:(unsigned long long)total {
      
      		NSLog(@"%f", (float)loaded/total);
      	}
      

2、SARUnArchiveANY 方式

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

  • GitHub 网址:https://github.com/saru2020/SARUnArchiveANY

  • SARUnArchiveANY 使用 ARC

  • 添加 SARUnArchiveANY

    	// 添加第三方库文件
    	SARUnArchiveANY
    
    	// 包含系统动态库
    	libz.tbd (libz.dylib)
    
    	// 包含头文件
    	#import "SARUnArchiveANY.h"
    	#import "LZMAExtractor.h"
    
  • SARUnArchiveANY 解压缩

    	- (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;
    	}
    
原文地址:https://www.cnblogs.com/QianChia/p/5771272.html