Skip to content

Commit 76ebc71

Browse files
committed
Merge pull request #332: Add compression level argument
2 parents 5fdaccc + 4b805d8 commit 76ebc71

5 files changed

Lines changed: 102 additions & 33 deletions

File tree

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,14 @@ import { MainBundlePath, DocumentDirectoryPath } from 'react-native-fs'
5454

5555
## API
5656

57-
**`zip(source: string | string[], target: string): Promise<string>`**
57+
**`zip(source: string | string[], target: string, compressionLevel?: number): Promise<string>`**
5858

5959
> zip source to target
6060
6161
***NOTE: the string version of source is for folder, the string[] version is for file, so if you want to zip a single file, use zip([file]) instead of zip(file)***
6262

63+
***NOTE: customizing the compression level is not supported on iOS with a files source and will be ignored, use a directory source instead.***
64+
6365
Example
6466

6567
```js
@@ -75,14 +77,16 @@ zip(sourcePath, targetPath)
7577
})
7678
```
7779

78-
**`zipWithPassword(source: string | string[], target: string, password: string, encryptionType: string): Promise<string>`**
80+
**`zipWithPassword(source: string | string[], target: string, password: string, encryptionType?: string, compressionLevel?: number): Promise<string>`**
7981

8082
> zip source to target
8183
8284
***NOTE: the string version of source is for folder, the string[] version is for file, so if you want to zip a single file, use zip([file]) instead of zip(file)***
8385

8486
***NOTE: On iOS, AES-256 and AES-128 both use AES-256 encryption.***
8587

88+
***NOTE: customizing the compression level is not supported on iOS with a files source and will be ignored, use a directory source instead.***
89+
8690
Example
8791

8892
```js

android/src/main/java/com/rnziparchive/RNZipArchiveModule.java

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -283,39 +283,37 @@ public void run() {
283283
}
284284

285285
@ReactMethod
286-
public void zipFiles(final ReadableArray files, final String destDirectory, final Promise promise) {
287-
zip(files.toArrayList(), destDirectory, promise);
286+
public void zipFiles(final ReadableArray files, final String destDirectory, final double compressionLevel, final Promise promise) {
287+
zip(files.toArrayList(), destDirectory, compressionLevel, promise);
288288
}
289289

290290
@ReactMethod
291-
public void zipFolder(final String folder, final String destFile, final Promise promise) {
291+
public void zipFolder(final String folder, final String destFile, final double compressionLevel, final Promise promise) {
292292
ArrayList<Object> folderAsArrayList = new ArrayList<>();
293293
folderAsArrayList.add(folder);
294-
zip(folderAsArrayList, destFile, promise);
294+
zip(folderAsArrayList, destFile, compressionLevel, promise);
295295
}
296296

297297
@ReactMethod
298298
public void zipFilesWithPassword(final ReadableArray files, final String destFile, final String password,
299-
String encryptionMethod, Promise promise) {
300-
zipWithPassword(files.toArrayList(), destFile, password, encryptionMethod, promise);
299+
String encryptionMethod, final double compressionLevel, Promise promise) {
300+
zipWithPassword(files.toArrayList(), destFile, password, encryptionMethod, compressionLevel, promise);
301301
}
302302

303-
304303
@ReactMethod
305304
public void zipFolderWithPassword(final String folder, final String destFile, final String password,
306-
String encryptionMethod, Promise promise) {
305+
String encryptionMethod, final double compressionLevel, Promise promise) {
307306
ArrayList<Object> folderAsArrayList = new ArrayList<>();
308307
folderAsArrayList.add(folder);
309-
zipWithPassword(folderAsArrayList, destFile, password, encryptionMethod, promise);
308+
zipWithPassword(folderAsArrayList, destFile, password, encryptionMethod, compressionLevel, promise);
310309
}
311310

312311
private void zipWithPassword(final ArrayList<Object> filesOrDirectory, final String destFile, final String password,
313-
String encryptionMethod, Promise promise) {
312+
String encryptionMethod, final double compressionLevel, Promise promise) {
314313
try{
315-
316314
ZipParameters parameters = new ZipParameters();
317315
parameters.setCompressionMethod(CompressionMethod.DEFLATE);
318-
parameters.setCompressionLevel(CompressionLevel.NORMAL);
316+
parameters.setCompressionLevel(getCompressionLevel(compressionLevel));
319317

320318
String encParts[] = encryptionMethod.split("-");
321319

@@ -347,15 +345,13 @@ private void zipWithPassword(final ArrayList<Object> filesOrDirectory, final Str
347345
promise.reject("RNZipArchiveError", ex.getMessage());
348346
return;
349347
}
350-
351348
}
352349

353-
private void zip(final ArrayList<Object> filesOrDirectory, final String destFile, final Promise promise) {
350+
private void zip(final ArrayList<Object> filesOrDirectory, final String destFile, final double compressionLevel, final Promise promise) {
354351
try{
355-
356352
ZipParameters parameters = new ZipParameters();
357353
parameters.setCompressionMethod(CompressionMethod.DEFLATE);
358-
parameters.setCompressionLevel(CompressionLevel.NORMAL);
354+
parameters.setCompressionLevel(getCompressionLevel(compressionLevel));
359355

360356
processZip(filesOrDirectory, destFile, parameters, promise, null);
361357

@@ -473,6 +469,36 @@ private long getUncompressedSize(String zipFilePath, String charset) {
473469
return totalSize;
474470
}
475471

472+
private static CompressionLevel getCompressionLevel(double compressionLevel) {
473+
switch (compressionLevel) {
474+
case -1:
475+
return CompressionLevel.NORMAL;
476+
case 0:
477+
return CompressionLevel.NO_COMPRESSION;
478+
case 1:
479+
return CompressionLevel.FASTEST;
480+
case 2:
481+
return CompressionLevel.FASTER;
482+
case 3:
483+
return CompressionLevel.FAST;
484+
case 4:
485+
return CompressionLevel.MEDIUM_FAST;
486+
case 5:
487+
return CompressionLevel.NORMAL;
488+
case 6:
489+
return CompressionLevel.HIGHER;
490+
case 7:
491+
return CompressionLevel.MAXIMUM;
492+
case 8:
493+
return CompressionLevel.PRE_ULTRA;
494+
case 9:
495+
return CompressionLevel.ULTRA;
496+
default:
497+
Log.w(TAG, "Unsupported compression level: " + compressionLevel + ", defaulting to NORMAL (5)");
498+
return CompressionLevel.NORMAL;
499+
}
500+
}
501+
476502
/**
477503
* Returns the exception stack trace as a string
478504
*/
@@ -492,5 +518,4 @@ public void addListener(String eventName) {
492518
public void removeListeners(Integer count) {
493519
// Keep: Required for RN built in Event Emitter Calls.
494520
}
495-
496521
}

index.d.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
1-
declare module 'react-native-zip-archive' {
1+
declare module "react-native-zip-archive" {
22
enum EncryptionMethods {
33
STANDARD = "STANDARD",
44
AES_128 = "AES-128",
5-
AES_256 = "AES-256"
5+
AES_256 = "AES-256",
66
}
7-
import { NativeEventSubscription } from 'react-native';
7+
import { NativeEventSubscription } from "react-native";
8+
export const DEFAULT_COMPRESSION: number;
9+
export const NO_COMPRESSION: number;
10+
export const BEST_SPEED: number;
11+
export const BEST_COMPRESSION: number;
812
export function isPasswordProtected(source: string): Promise<boolean>;
9-
export function zip(source: string | string[], target: string): Promise<string>;
10-
export function zipWithPassword(source: string | string[], target: string, password: string, encryptionMethod?: encryptionMethods): Promise<string>;
13+
export function zip(
14+
source: string | string[],
15+
target: string,
16+
compressionLevel: number = DEFAULT_COMPRESSION
17+
): Promise<string>;
18+
export function zipWithPassword(
19+
source: string | string[],
20+
target: string,
21+
password: string,
22+
encryptionMethod?: EncryptionMethods,
23+
compressionLevel: number = DEFAULT_COMPRESSION
24+
): Promise<string>;
1125
export function unzip(source: string, target: string, charset?: string): Promise<string>;
1226
export function unzipWithPassword(assetPath: string, target: string, password: string): Promise<string>;
1327
export function unzipAssets(assetPath: string, target: string): Promise<string>;

index.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,19 @@ const rnzaEmitter = new NativeEventEmitter(RNZipArchive);
99
const normalizeFilePath = (path) =>
1010
path.startsWith("file://") ? path.slice(7) : path;
1111

12+
export const DEFAULT_COMPRESSION = -1;
13+
export const NO_COMPRESSION = 0;
14+
export const BEST_SPEED = 1;
15+
export const BEST_COMPRESSION = 9;
16+
1217
export const unzip = (source, target, charset = "UTF-8") => {
1318
return RNZipArchive.unzip(
1419
normalizeFilePath(source),
1520
normalizeFilePath(target),
1621
charset
1722
);
1823
};
24+
1925
export const isPasswordProtected = (source) => {
2026
return RNZipArchive.isPasswordProtected(normalizeFilePath(source)).then(
2127
(isEncrypted) => !!isEncrypted
@@ -34,32 +40,37 @@ export const zipWithPassword = (
3440
source,
3541
target,
3642
password,
37-
encryptionMethod = ""
43+
encryptionMethod = "",
44+
compressionLevel = DEFAULT_COMPRESSION,
3845
) => {
3946
return Array.isArray(source)
4047
? RNZipArchive.zipFilesWithPassword(
4148
source.map(normalizeFilePath),
4249
normalizeFilePath(target),
4350
password,
44-
encryptionMethod
51+
encryptionMethod,
52+
compressionLevel
4553
)
4654
: RNZipArchive.zipFolderWithPassword(
4755
normalizeFilePath(source),
4856
normalizeFilePath(target),
4957
password,
50-
encryptionMethod
58+
encryptionMethod,
59+
compressionLevel
5160
);
5261
};
5362

54-
export const zip = (source, target) => {
63+
export const zip = (source, target, compressionLevel = DEFAULT_COMPRESSION) => {
5564
return Array.isArray(source)
5665
? RNZipArchive.zipFiles(
5766
source.map(normalizeFilePath),
58-
normalizeFilePath(target)
67+
normalizeFilePath(target),
68+
compressionLevel
5969
)
6070
: RNZipArchive.zipFolder(
6171
normalizeFilePath(source),
62-
normalizeFilePath(target)
72+
normalizeFilePath(target),
73+
compressionLevel
6374
);
6475
};
6576

ios/RNZipArchive.m

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ -(void)stopObserving {
9999

100100
RCT_EXPORT_METHOD(zipFolder:(NSString *)from
101101
destinationPath:(NSString *)destinationPath
102+
compressionLevel:(double)compressionLevel
102103
resolver:(RCTPromiseResolveBlock)resolve
103104
rejecter:(RCTPromiseRejectBlock)reject) {
104105
self.progress = 0.0;
@@ -108,7 +109,13 @@ -(void)stopObserving {
108109
BOOL success;
109110
[self setProgressHandler];
110111

111-
success = [SSZipArchive createZipFileAtPath:destinationPath withContentsOfDirectory:from keepParentDirectory:NO withPassword:nil andProgressHandler:self.progressHandler];
112+
success = [SSZipArchive createZipFileAtPath:destinationPath
113+
withContentsOfDirectory:from
114+
keepParentDirectory:NO
115+
compressionLevel:compressionLevel
116+
password:nil
117+
AES:NO
118+
progressHandler:self.progressHandler];
112119

113120
self.progress = 1.0;
114121
[self zipArchiveProgressEvent:1 total:1]; // force 100%
@@ -123,6 +130,7 @@ -(void)stopObserving {
123130

124131
RCT_EXPORT_METHOD(zipFiles:(NSArray<NSString *> *)from
125132
destinationPath:(NSString *)destinationPath
133+
compressionLevel:(double)compressionLevel
126134
resolver:(RCTPromiseResolveBlock)resolve
127135
rejecter:(RCTPromiseRejectBlock)reject) {
128136
self.progress = 0.0;
@@ -145,11 +153,11 @@ -(void)stopObserving {
145153
}
146154
}
147155

148-
149156
RCT_EXPORT_METHOD(zipFolderWithPassword:(NSString *)from
150157
destinationPath:(NSString *)destinationPath
151158
password:(NSString *)password
152159
encryptionType:(NSString *)encryptionType
160+
compressionLevel:(double)compressionLevel
153161
resolver:(RCTPromiseResolveBlock)resolve
154162
rejecter:(RCTPromiseRejectBlock)reject) {
155163
self.progress = 0.0;
@@ -159,7 +167,13 @@ -(void)stopObserving {
159167
BOOL success;
160168
[self setProgressHandler];
161169
BOOL useAES = encryptionType && ![encryptionType isEqualToString:@"STANDARD"];
162-
success = [SSZipArchive createZipFileAtPath:destinationPath withContentsOfDirectory:from keepParentDirectory:NO compressionLevel:Z_DEFAULT_COMPRESSION password:password AES:useAES progressHandler:self.progressHandler];
170+
success = [SSZipArchive createZipFileAtPath:destinationPath
171+
withContentsOfDirectory:from
172+
keepParentDirectory:NO
173+
compressionLevel:compressionLevel
174+
password:password
175+
AES:useAES
176+
progressHandler:self.progressHandler];
163177

164178
self.progress = 1.0;
165179
[self zipArchiveProgressEvent:1 total:1]; // force 100%
@@ -176,6 +190,7 @@ -(void)stopObserving {
176190
destinationPath:(NSString *)destinationPath
177191
password:(NSString *)password
178192
encryptionType:(NSString *)encryptionType
193+
compressionLevel:(double)compressionLevel
179194
resolver:(RCTPromiseResolveBlock)resolve
180195
rejecter:(RCTPromiseRejectBlock)reject) {
181196
self.progress = 0.0;

0 commit comments

Comments
 (0)