zoukankan      html  css  js  c++  java
  • 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;
      	}
      
  • 相关阅读:
    【新特性速递】数字输入框的前缀和后缀(位于输入框内部)
    【新特性速递】进度条,进度条,进度条
    【新特性速递】当法语遇上FineUI(Bonjour)!
    【新特性速递】自定义数字输入框的小数分隔符和千分位分隔符
    【经验分享】FineUICore中如何处理文件导出异常?
    【网友作品】服装分销系统架构与界面分享(基于FineUICore基础版)
    FineUIPro/Mvc/Core v6.3.0 正式发布了!
    星球居民突破 1700 人!
    【新特性速递】开关样式复选框增强!
    【新特性速递】为RenderField新增QuickSortField属性!
  • 原文地址:https://www.cnblogs.com/QianChia/p/5771272.html
Copyright © 2011-2022 走看看