它不是围绕LZMA SDK C部分的又一个新的包装,它有所有的限制。
基于C++版本19.00(1900 - 目前最新版本)的LZMA SDK,并针对iOS & Mac OS平台进行了修复。
可以用于Swift和Objective-C。
描述
它不是围绕LZMA SDK C部分的又一个新的包装,它有所有的限制。基于C++版本的LZMA SDK 19.00(1900 - 目前最新版本)并针对iOS & Mac OS平台进行了修复。
主要优点是
- 列出、提取< stärk >7z文件(Lzma & < strong>Lzma2 压缩方法)。
- 列出、提取< 强调 >加密的强 受密码保护 >7z文件(< strong>Lzma & < strong>Lzma2 压缩方法)。
- 列出、提取< 强调 >加密的强 受密码保护 >+ < 强调 >加密的标题强 无可见内容,文件列表,无密码 > 7z文件(< strong>Lzma & < strong>Lzma2 压缩方法)。
- 创建< 强调 >7z强 >存档(< 强调 >Lzma & < 强调 >Lzma2 压缩方法)。
- 创建< 强调 >加密的强 受密码保护 >< 强调 >7z强 >存档(< 强调 >Lzma & < 强调 >Lzma2 压缩方法)。
- 创建< 强调 >加密的强 受密码保护 >+ < 强调 >加密的标题强 无可见内容,文件列表,无密码 > < 强调 >7z强 >存档(< 强调 >Lzma & < 强调 >Lzma2 压缩方法)。
- 在列出/提取过程中管理内存分配。请参阅以下部分:< strong >调整速度,性能和磁盘IO操作
- 调整以不超过500Kb用于列出/提取,可以在运行时轻松更改(无硬编码定义)。请参阅以下部分:< strong >调整速度,性能和磁盘IO操作
- 管理IO读写操作,也可以在运行时轻松更改(无硬编码定义)。请参阅以下部分:< strong >调整速度,性能和磁盘IO操作
- 跟踪平滑进度,这已成为可能。
- 支持读取和解压缩大小超过4GB的存档文件。
- 支持UTF8。
- 使用Lzma2对单个NSData对象进行额外的压缩/解压缩功能。
CocoaPods安装
使用Podfile
use_frameworks!
platform :ios, '9.0'
target '<REPLACE_WITH_YOUR_TARGET>' do
pod 'LzmaSDK-ObjC', :inhibit_warnings => true
end
使用框架(动态链接)将Lzma编解码器代码包含到您的应用程序中。
Swift和Objective-C示例
列出和提取
创建并设置读取器,提供存档路径和/或存档类型,可选代理和可选密码提供程序块(如果存档被加密)
Swift
import LzmaSDK_ObjC
...
// select full path to archive file with 7z extension
let archivePath = "path to archive"
// 1.1 Create reader object.
let reader = LzmaSDKObjCReader(fileURL: NSURL(fileURLWithPath: archivePath)
// 1.2 Or create with predefined archive type if path doesn't containes suitable extension
let reader = LzmaSDKObjCReader(fileURL: NSURL(fileURLWithPath: archivePath), andType: LzmaSDKObjCFileType7z)
// Optionaly: assign delegate for tracking extract progress.
reader.delegate = self
// If achive encrypted - define password getter handler.
// NOTES:
// - Encrypted file needs password for extract process.
// - Encrypted file with encrypted header needs password for list(iterate) and extract archive items.
reader.passwordGetter = {
return "password to my achive"
}
...
// Delegate extension
extension ReaderDelegateObject: LzmaSDKObjCReaderDelegate {
func onLzmaSDKObjCReader(reader: LzmaSDKObjCReader, extractProgress progress: Float) {
print("Reader progress: \(progress) %")
}
}
Objective-C
// select full path to archive file with 7z extension
NSString * archivePath = <path to archive>;
// 1.1 Create and hold strongly reader object.
self.reader = [[LzmaSDKObjCReader alloc] initWithFileURL:[NSURL fileURLWithPath:archivePath]];
// 1.2 Or create with predefined archive type if path doesn't containes suitable extension
self.reader = [[LzmaSDKObjCReader alloc] initWithFileURL:[NSURL fileURLWithPath:archivePath]
andType:LzmaSDKObjCFileType7z];
// Optionaly: assign weak delegate for tracking extract progress.
_reader.delegate = self;
// If achive encrypted - define password getter handler.
// NOTES:
// - Encrypted file needs password for extract process.
// - Encrypted file with encrypted header needs password for list(iterate) and extract archive items.
_reader.passwordGetter = ^NSString*(void){
return @"password to my achive";
};
打开存档,例如找出存档类型、定位解码器并读取存档头信息
Swift
// Try open archive.
do {
try reader.open()
}
catch let error as NSError {
print("Can't open archive: \(error.localizedDescription) ")
}
Objective-C
// Open archive, with or without error. Error can be nil.
NSError * error = nil;
if (![_reader open:&error]) {
NSLog(@"Open error: %@", error);
}
NSLog(@"Open error: %@", _reader.lastError);
遍历存档项目,选择并存储未来的处理所需的项目
Swift
var items = [LzmaSDKObjCItem]() // Array with selected items.
// Iterate all archive items, track what items do you need & hold them in array.
reader.iterateWithHandler({(item: LzmaSDKObjCItem, error: NSError?) -> Bool in
items.append(item) // if needs this item - store to array.
return true // true - continue iterate, false - stop iteration
})
Objective-C
NSMutableArray * items = [NSMutableArray array]; // Array with selected items.
// Iterate all archive items, track what items do you need & hold them in array.
[_reader iterateWithHandler:^BOOL(LzmaSDKObjCItem * item, NSError * error){
NSLog(@"\n%@", item);
if (item) [items addObject:item]; // if needs this item - store to array.
return YES; // YES - continue iterate, NO - stop iteration
}];
NSLog(@"Iteration error: %@", _reader.lastError);
提取或测试存档项
Swift
// Extract selected items from prev. step.
// true - create subfolders structure for the items.
// false - place item file to the root of path(in this case items with the same names will be overwrited automaticaly).
if reader.extract(items, toPath: "/Volumes/Data/1/", withFullPaths: true) {
print("Extract failed: \(reader.lastError?.localizedDescription)")
}
// Test selected items from prev. step.
if reader.test(items) {
print("Test failed: \(reader.lastError?.localizedDescription)")
}
Objective-C
// Extract selected items from prev. step.
// YES - create subfolders structure for the items.
// NO - place item file to the root of path(in this case items with the same names will be overwrited automaticaly).
[_reader extract:items
toPath:@"/extract/path"
withFullPaths:YES];
NSLog(@"Extract error: %@", _reader.lastError);
// Test selected items from prev. step.
[_reader test:items];
NSLog(@"test error: %@", _reader.lastError);
创建7z存档
Swift
// Create writer
let writer = LzmaSDKObjCWriter(fileURL: NSURL(fileURLWithPath: "/Path/MyArchive.7z"))
// Add file data's or paths
writer.addData(NSData(...), forPath: "MyArchiveFileName.txt") // Add file data
writer.addPath("/Path/somefile.txt", forPath: "archiveDir/somefile.txt") // Add file at path
writer.addPath("/Path/SomeDirectory", forPath: "SomeDirectory") // Recursively add directory with all contents
// Setup writer
writer.delegate = self // Track progress
writer.passwordGetter = { // Password getter
return "1234"
}
// Optional settings
writer.method = LzmaSDKObjCMethodLZMA2 // or LzmaSDKObjCMethodLZMA
writer.solid = true
writer.compressionLevel = 9
writer.encodeContent = true
writer.encodeHeader = true
writer.compressHeader = true
writer.compressHeaderFull = true
writer.writeModificationTime = false
writer.writeCreationTime = false
writer.writeAccessTime = false
// Open archive file
do {
try writer.open()
} catch let error as NSError {
print(error.localizedDescription)
}
// Write archive within current thread
writer.write()
// or
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)) {
writer.write()
}
Objective-C
// Create writer
LzmaSDKObjCWriter * writer = [[LzmaSDKObjCWriter alloc] initWithFileURL:[NSURL fileURLWithPath:@"/Path/MyArchive.7z"]];
// Add file data's or paths
[writer addData:[NSData ...] forPath:@"MyArchiveFileName.txt"]; // Add file data
[writer addPath:@"/Path/somefile.txt" forPath:@"archiveDir/somefile.txt"]; // Add file at path
[writer addPath:@"/Path/SomeDirectory" forPath:@"SomeDirectory"]; // Recursively add directory with all contents
// Setup writer
writer.delegate = self; // Track progress
writer.passwordGetter = ^NSString*(void) { // Password getter
return @"1234";
};
// Optional settings
writer.method = LzmaSDKObjCMethodLZMA2; // or LzmaSDKObjCMethodLZMA
writer.solid = YES;
writer.compressionLevel = 9;
writer.encodeContent = YES;
writer.encodeHeader = YES;
writer.compressHeader = YES;
writer.compressHeaderFull = YES;
writer.writeModificationTime = NO;
writer.writeCreationTime = NO;
writer.writeAccessTime = NO;
// Open archive file
NSError * error = nil;
[writer open:&error];
// Write archive within current thread
[writer write];
// or
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
[writer write];
});
压缩解压单个数据缓冲区
Swift
// Compress data with compression maximum compression ratio.
let compressedData = LzmaSDKObjCBufferCompressLZMA2(sourceData, 1)
// Decompress previvously compressed data.
let decompressedData = LzmaSDKObjCBufferDecompressLZMA2(compressedData)
Objective-C
// Compress data with compression maximum compression ratio.
NSData * compressedData = LzmaSDKObjCBufferCompressLZMA2(sourceData, 1);
// Decompress previvously compressed data.
NSData * decompressedData = LzmaSDKObjCBufferDecompressLZMA2(compressedData);
调整速度、性能和磁盘IO操作
修改了>LZMA SDK的原始C++部分,使其有调整默认(硬编码)设置的可能性。对于列表和提取功能,定义了以下全局变量:kLzmaSDKObjCStreamReadSize
、kLzmaSDKObjCStreamWriteSize
、kLzmaSDKObjCDecoderReadSize
和kLzmaSDKObjCDecoderWriteSize
。这些变量持有字节大小值,因此,对于单个读者对象的总结将分配这4个值的总和。
请注意:操作比磁盘IO操作快得多,所以阅读以下情况
switch (<what do I need ?>) {
case <I need faster list and extract>:
//TODO: Increase stream and decoder size of buffers
Result:
1. more allocated memory
2. less IO read/write operations and less delays
3. less smoothed progress
4. more CPU load (do a job, not distracted to read/write data)
break;
case <I need use less memory or more smoothed progress>:
//TODO: Decrease stream and decoder size of buffers
Result:
1. less allocated memory
2. more IO read/write operations and more delays
3. more smoothed progress
4. less CPU load (wait for read/write data)
break;
default:
//TODO: use current settings
break;
};
注意:在创建并使用读取器对象之前修改全局变量。
注意:这种分配大小不会影响到用于归档字典分配的内存。
功能列表(待办/完成)
- Lzma/*.7z
- 列表
- 常规归档。
tests/files/lzma.7z
- 带有AES256加密的加密归档。
tests/files/lzma_aes256.7z
- 带有AES256加密的加密归档+加密的头部(无可见内容、文件列表、无密码)。
tests/files/lzma_aes256_encfn.7z
- 常规归档。
- 提取
- 常规归档。
tests/files/lzma.7z
- 带有AES256加密的加密归档。
tests/files/lzma_aes256.7z
- 带有AES256加密的加密归档+加密的头部(无可见内容、文件列表、无密码)。
tests/files/lzma_aes256_encfn.7z
- 常规归档。
- 创建
- 常规归档。
- 带有AES256加密的加密归档。
- 带有AES256加密的加密归档+加密的头部(无可见内容、文件列表、无密码)。
tests/files/lzma_aes256_encfn.7z
- 列表
- Lzma2/*.7z
- 列表
- 常规归档。
tests/files/lzma2.7z
- 带有AES256加密的加密归档。
tests/files/lzma2_aes256.7z
- 带有AES256加密的加密归档+加密的头部(无可见内容、文件列表、无密码)。
tests/files/lzma2_aes256_encfn.7z
- 常规归档。
- 提取
- 常规归档。
tests/files/lzma2.7z
- 带有AES256加密的加密归档。
tests/files/lzma2_aes256.7z
- 带有AES256加密的加密归档+加密的头部(无可见内容、文件列表、无密码)。
tests/files/lzma2_aes256_encfn.7z
- 常规归档。
- 创建
- 常规归档。
- 带有AES256加密的加密归档。
- 带有AES256加密的加密归档+加密的头部(无可见内容、文件列表、无密码)。
tests/files/lzma_aes256_encfn.7z
- 列表
- 省略未使用的代码,减少可构建,原始代码大小。
许可
通过使用此软件,您同意接受原始 LZMA SDK 和 MIT 许可证(见下文)
MIT 许可证(MIT)
版权所有(c)2015 - 2019 Kulykov Oleh [email protected]
任何获取此软件和相关文档副本(“软件”)的个人,都可以免费处理该软件,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或销售软件副件,并允许向用户提供软件的人这样做,前提是遵守以下条件:
上述版权声明和本许可声明应包含在软件的所有副本或实质性部分中。
软件按“原样”提供,不提供任何明示或暗示的保证,包括但不限于适销性、针对特定用途的适用性和非侵权性。在任何情况下,作者或版权所有者均不对任何主张、损害或其他责任承担责任,无论该责任是基于合同、侵权或其他行为,无论该行为是否与软件或其使用或其他方式有关。