Skip to content

Commit

Permalink
Added support for setting the compression level
Browse files Browse the repository at this point in the history
  • Loading branch information
lvsti committed Jan 5, 2018
1 parent 266df1b commit 9b35b82
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 7 deletions.
31 changes: 28 additions & 3 deletions SevenZip/SVZArchive.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,27 @@ typedef NS_ENUM(NSInteger, SVZArchiveError) {
kSVZArchiveErrorForeignEntry = -6
};

/// Compression level for archive update operations (higher is slower)
typedef NS_ENUM(NSUInteger, SVZCompressionLevel) {
/// no compression, copy only
kSVZCompressionLevelNone = 0,

// (Method, Dictionary, FastBytes, MatchFinder, Filter)
/// LZMA2, 64 KB, 32, HC4, BCJ
kSVZCompressionLevelLowest,

/// LZMA2, 1 MB, 32, HC4, BCJ
kSVZCompressionLevelLow,

/// LZMA2, 16 MB, 32, BT4, BCJ
kSVZCompressionLevelNormal,

/// LZMA2, 32 MB, 64, BT4, BCJ
kSVZCompressionLevelHigh,

/// LZMA2, 64 MB, 64, BT4, BCJ2
kSVZCompressionLevelHighest
};

/**
* Class representing a 7-zip archive file.
Expand Down Expand Up @@ -108,19 +129,23 @@ typedef NS_ENUM(NSInteger, SVZArchiveError) {
* so this awkward combination is best to be avoided.
*
* @param aEntries The entries that should be written to the archive file.
* @param aPassword The password to use for encryption
* @param aUseHeaderEncryption If set to NO, only the newly added archive entries will be encrypted,
* @param aPassword The password to use for encryption (optional). If nil, no encryption is applied.
* @param aUseHeaderEncryption Controls header encryption when a password is provided, as follows:
* If set to NO, only the newly added archive entries will be encrypted,
* which means extracting these entries will require the password given in `aPassword`.
* If set to YES, both the newly added entries AND the archive header will be encrypted,
* which means both listing the archive contents and extracting these entries will require
* the password given in `aPassword`.
* If `aPassword` is empty, this flag is ignored.
* @param aCompressionLevel Compression level to use
* @param aError Error information in case of failure. May be NULL.
*
* @return YES on success, otherwise NO.
*/
- (BOOL)updateEntries:(SVZ_GENERIC(NSArray, SVZArchiveEntry*)*)aEntries
withPassword:(NSString*)aPassword
withPassword:(NSString* SVZ_NULLABLE_PTR)aPassword
headerEncryption:(BOOL)aUseHeaderEncryption
compressionLevel:(SVZCompressionLevel)aCompressionLevel
error:(NSError**)aError;

@end
Expand Down
25 changes: 21 additions & 4 deletions SevenZip/SVZArchive.mm
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,22 @@ - (void)dealloc {

- (BOOL)updateEntries:(SVZ_GENERIC(NSArray, SVZArchiveEntry*)*)aEntries
error:(NSError**)aError {
NSString* dummyPassword = nil;
return [self updateEntries:aEntries
withPassword:dummyPassword
withPassword:nil
headerEncryption:NO
compressionLevel:kSVZCompressionLevelNormal
error:aError];
}

- (BOOL)updateEntries:(SVZ_GENERIC(NSArray, SVZArchiveEntry*)*)aEntries
withPassword:(NSString*)aPassword
headerEncryption:(BOOL)aEnableHeaderEncryption
compressionLevel:(SVZCompressionLevel)aCompressionLevel
error:(NSError**)aError {
if (!aPassword) {
aEnableHeaderEncryption = NO;
}

CObjectVector<SVZ::ArchiveItem> archiveItems;
SVZ_GENERIC(NSMutableArray, SVZStoredArchiveEntry*)* storedEntries = [NSMutableArray arrayWithCapacity:aEntries.count];

Expand Down Expand Up @@ -188,9 +193,21 @@ - (BOOL)updateEntries:(SVZ_GENERIC(NSArray, SVZArchiveEntry*)*)aEntries
result = outArchive->QueryInterface(IID_ISetProperties, (void**)&setProperties);
NSAssert(result == S_OK, @"archiver object does not support setting properties");

const wchar_t* names[] = { L"he" };
const UInt32 kRawLevelValue[] = {
[kSVZCompressionLevelNone] = 0,
[kSVZCompressionLevelLowest] = 1,
[kSVZCompressionLevelLow] = 3,
[kSVZCompressionLevelNormal] = 5,
[kSVZCompressionLevelHigh] = 7,
[kSVZCompressionLevelHighest] = 9,
};

const wchar_t* names[] = { L"he", L"x" };
const unsigned kNumProps = ARRAY_SIZE(names);
NWindows::NCOM::CPropVariant values[kNumProps] = { aPassword && aEnableHeaderEncryption ? L"on" : L"off" };
NWindows::NCOM::CPropVariant values[kNumProps] = {
aPassword && aEnableHeaderEncryption ? L"on" : L"off",
kRawLevelValue[aCompressionLevel]
};
setProperties->SetProperties(names, values, kNumProps);

// update entries
Expand Down
4 changes: 4 additions & 0 deletions SevenZipTests/UT_SVZArchive.m
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@ + (NSFileManager*)fileManager;
BOOL result = [sut updateEntries:[sut.entries arrayByAddingObject:newEntry]
withPassword:@"secret"
headerEncryption:NO
compressionLevel:kSVZCompressionLevelNormal
error:&error];

// then
Expand Down Expand Up @@ -525,6 +526,7 @@ + (NSFileManager*)fileManager;
BOOL result = [sut updateEntries:[sut.entries arrayByAddingObject:newEntry]
withPassword:@"secret"
headerEncryption:NO
compressionLevel:kSVZCompressionLevelNormal
error:&error];

// then
Expand Down Expand Up @@ -559,6 +561,7 @@ + (NSFileManager*)fileManager;
BOOL result = [sut updateEntries:[sut.entries arrayByAddingObject:newEntry]
withPassword:@"secret"
headerEncryption:YES
compressionLevel:kSVZCompressionLevelNormal
error:&error];

// then
Expand Down Expand Up @@ -593,6 +596,7 @@ + (NSFileManager*)fileManager;
BOOL result = [sut updateEntries:[sut.entries arrayByAddingObject:newEntry]
withPassword:@"secret"
headerEncryption:NO
compressionLevel:kSVZCompressionLevelNormal
error:&error];

// then
Expand Down

0 comments on commit 9b35b82

Please sign in to comment.