react-native-zip-archive 7.0.2 → 7.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -54,12 +54,14 @@ import { MainBundlePath, DocumentDirectoryPath } from 'react-native-fs'
54
54
 
55
55
  ## API
56
56
 
57
- **`zip(source: string | string[], target: string): Promise<string>`**
57
+ **`zip(source: string | string[], target: string, compressionLevel?: number): Promise<string>`**
58
58
 
59
59
  > zip source to target
60
60
 
61
61
  ***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)***
62
62
 
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
+
63
65
  Example
64
66
 
65
67
  ```js
@@ -75,13 +77,15 @@ zip(sourcePath, targetPath)
75
77
  })
76
78
  ```
77
79
 
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>`**
79
81
 
80
82
  > zip source to target
81
83
 
82
84
  ***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)***
83
85
 
84
- ***NOTE: encryptionType is not supported on iOS yet, so it would be igonred on that platform.***
86
+ ***NOTE: On iOS, AES-256 and AES-128 both use AES-256 encryption.***
87
+
88
+ ***NOTE: customizing the compression level is not supported on iOS with a files source and will be ignored, use a directory source instead.***
85
89
 
86
90
  Example
87
91
 
@@ -64,7 +64,7 @@ public class RNZipArchiveModule extends ReactContextBaseJavaModule {
64
64
  net.lingala.zip4j.ZipFile zipFile = new net.lingala.zip4j.ZipFile(zipFilePath);
65
65
  promise.resolve(zipFile.isEncrypted());
66
66
  } catch (ZipException ex) {
67
- promise.reject(null, String.format("Unable to check for encryption due to: %s", getStackTrace(ex)));
67
+ promise.reject("RNZipArchiveError", String.format("Unable to check for encryption due to: %s", getStackTrace(ex)));
68
68
  }
69
69
  }
70
70
 
@@ -79,7 +79,7 @@ public class RNZipArchiveModule extends ReactContextBaseJavaModule {
79
79
  if (zipFile.isEncrypted()) {
80
80
  zipFile.setPassword(password.toCharArray());
81
81
  } else {
82
- promise.reject(null, String.format("Zip file: %s is not password protected", zipFilePath));
82
+ promise.reject("RNZipArchiveError", String.format("Zip file: %s is not password protected", zipFilePath));
83
83
  }
84
84
 
85
85
  List fileHeaderList = zipFile.getFileHeaders();
@@ -107,7 +107,7 @@ public class RNZipArchiveModule extends ReactContextBaseJavaModule {
107
107
  promise.resolve(Arguments.fromList(extractedFileNames));
108
108
  } catch (Exception ex) {
109
109
  updateProgress(0, 1, zipFilePath); // force 0%
110
- promise.reject(null, String.format("Failed to unzip file, due to: %s", getStackTrace(ex)));
110
+ promise.reject("RNZipArchiveError", String.format("Failed to unzip file, due to: %s", getStackTrace(ex)));
111
111
  }
112
112
  }
113
113
  }).start();
@@ -122,7 +122,7 @@ public class RNZipArchiveModule extends ReactContextBaseJavaModule {
122
122
  try {
123
123
  new File(zipFilePath);
124
124
  } catch (NullPointerException e) {
125
- promise.reject(null, "Couldn't open file " + zipFilePath + ". ");
125
+ promise.reject("RNZipArchiveError", "Couldn't open file " + zipFilePath + ". ");
126
126
  return;
127
127
  }
128
128
 
@@ -174,7 +174,7 @@ public class RNZipArchiveModule extends ReactContextBaseJavaModule {
174
174
  }
175
175
  } catch (Exception ex) {
176
176
  updateProgress(0, 1, zipFilePath); // force 0%
177
- promise.reject(null, "Failed to extract file " + ex.getLocalizedMessage());
177
+ promise.reject("RNZipArchiveError", "Failed to extract file " + ex.getLocalizedMessage());
178
178
  }
179
179
  }
180
180
  }).start();
@@ -212,7 +212,7 @@ public class RNZipArchiveModule extends ReactContextBaseJavaModule {
212
212
  compressedSize = fileDescriptor.getLength();
213
213
  }
214
214
  } catch (IOException e) {
215
- promise.reject(null, String.format("Asset file `%s` could not be opened", assetsPath));
215
+ promise.reject("RNZipArchiveError", String.format("Asset file `%s` could not be opened", assetsPath));
216
216
  return;
217
217
  }
218
218
 
@@ -274,7 +274,7 @@ public class RNZipArchiveModule extends ReactContextBaseJavaModule {
274
274
  throw new Exception(String.format("Couldn't extract %s", assetsPath));
275
275
  }
276
276
  } catch (Exception ex) {
277
- promise.reject(null, ex.getMessage());
277
+ promise.reject("RNZipArchiveError", ex.getMessage());
278
278
  return;
279
279
  }
280
280
  promise.resolve(destDirectory);
@@ -283,39 +283,37 @@ public class RNZipArchiveModule extends ReactContextBaseJavaModule {
283
283
  }
284
284
 
285
285
  @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);
288
288
  }
289
289
 
290
290
  @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) {
292
292
  ArrayList<Object> folderAsArrayList = new ArrayList<>();
293
293
  folderAsArrayList.add(folder);
294
- zip(folderAsArrayList, destFile, promise);
294
+ zip(folderAsArrayList, destFile, compressionLevel, promise);
295
295
  }
296
296
 
297
297
  @ReactMethod
298
298
  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);
301
301
  }
302
302
 
303
-
304
303
  @ReactMethod
305
304
  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) {
307
306
  ArrayList<Object> folderAsArrayList = new ArrayList<>();
308
307
  folderAsArrayList.add(folder);
309
- zipWithPassword(folderAsArrayList, destFile, password, encryptionMethod, promise);
308
+ zipWithPassword(folderAsArrayList, destFile, password, encryptionMethod, compressionLevel, promise);
310
309
  }
311
310
 
312
311
  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) {
314
313
  try{
315
-
316
314
  ZipParameters parameters = new ZipParameters();
317
315
  parameters.setCompressionMethod(CompressionMethod.DEFLATE);
318
- parameters.setCompressionLevel(CompressionLevel.NORMAL);
316
+ parameters.setCompressionLevel(getCompressionLevel(compressionLevel));
319
317
 
320
318
  String encParts[] = encryptionMethod.split("-");
321
319
 
@@ -338,29 +336,27 @@ public class RNZipArchiveModule extends ReactContextBaseJavaModule {
338
336
  Log.d(TAG, "Encryption type not supported default to Standard Encryption");
339
337
  }
340
338
  } else {
341
- promise.reject(null, "Password is empty");
339
+ promise.reject("RNZipArchiveError", "Password is empty");
342
340
  }
343
341
 
344
342
  processZip(filesOrDirectory, destFile, parameters, promise, password.toCharArray());
345
343
 
346
344
  } catch (Exception ex) {
347
- promise.reject(null, ex.getMessage());
345
+ promise.reject("RNZipArchiveError", ex.getMessage());
348
346
  return;
349
347
  }
350
-
351
348
  }
352
349
 
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) {
354
351
  try{
355
-
356
352
  ZipParameters parameters = new ZipParameters();
357
353
  parameters.setCompressionMethod(CompressionMethod.DEFLATE);
358
- parameters.setCompressionLevel(CompressionLevel.NORMAL);
354
+ parameters.setCompressionLevel(getCompressionLevel(compressionLevel));
359
355
 
360
356
  processZip(filesOrDirectory, destFile, parameters, promise, null);
361
357
 
362
358
  } catch (Exception ex) {
363
- promise.reject(null, ex.getMessage());
359
+ promise.reject("RNZipArchiveError", ex.getMessage());
364
360
  return;
365
361
  }
366
362
  }
@@ -410,14 +406,14 @@ public class RNZipArchiveModule extends ReactContextBaseJavaModule {
410
406
  }
411
407
  }
412
408
  else {
413
- promise.reject(null, "File or folder does not exist");
409
+ promise.reject("RNZipArchiveError", "File or folder does not exist");
414
410
  }
415
411
 
416
412
  updateProgress(1, 1, destFile); // force 100%
417
413
  }
418
414
  promise.resolve(destFile);
419
415
  } catch (Exception ex) {
420
- promise.reject(null, ex.getMessage());
416
+ promise.reject("RNZipArchiveError", ex.getMessage());
421
417
  return;
422
418
  }
423
419
  }
@@ -473,6 +469,36 @@ public class RNZipArchiveModule extends ReactContextBaseJavaModule {
473
469
  return totalSize;
474
470
  }
475
471
 
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
+
476
502
  /**
477
503
  * Returns the exception stack trace as a string
478
504
  */
@@ -492,5 +518,4 @@ public class RNZipArchiveModule extends ReactContextBaseJavaModule {
492
518
  public void removeListeners(Integer count) {
493
519
  // Keep: Required for RN built in Event Emitter Calls.
494
520
  }
495
-
496
521
  }
package/index.d.ts CHANGED
@@ -1,13 +1,27 @@
1
- declare module 'react-native-zip-archive' {
1
+ declare module "react-native-zip-archive" {
2
2
  enum EncryptionMethods {
3
3
  STANDARD = "STANDARD",
4
4
  AES_128 = "AES-128",
5
- AES_256 = "AES-256"
5
+ AES_256 = "AES-256",
6
6
  }
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;
8
12
  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>;
11
25
  export function unzip(source: string, target: string, charset?: string): Promise<string>;
12
26
  export function unzipWithPassword(assetPath: string, target: string, password: string): Promise<string>;
13
27
  export function unzipAssets(assetPath: string, target: string): Promise<string>;
package/index.js CHANGED
@@ -9,6 +9,11 @@ const rnzaEmitter = new NativeEventEmitter(RNZipArchive);
9
9
  const normalizeFilePath = (path) =>
10
10
  path.startsWith("file://") ? path.slice(7) : path;
11
11
 
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
+
12
17
  export const unzip = (source, target, charset = "UTF-8") => {
13
18
  return RNZipArchive.unzip(
14
19
  normalizeFilePath(source),
@@ -16,6 +21,7 @@ export const unzip = (source, target, charset = "UTF-8") => {
16
21
  charset
17
22
  );
18
23
  };
24
+
19
25
  export const isPasswordProtected = (source) => {
20
26
  return RNZipArchive.isPasswordProtected(normalizeFilePath(source)).then(
21
27
  (isEncrypted) => !!isEncrypted
@@ -34,32 +40,37 @@ export const zipWithPassword = (
34
40
  source,
35
41
  target,
36
42
  password,
37
- encryptionMethod = ""
43
+ encryptionMethod = "",
44
+ compressionLevel = DEFAULT_COMPRESSION,
38
45
  ) => {
39
46
  return Array.isArray(source)
40
47
  ? RNZipArchive.zipFilesWithPassword(
41
48
  source.map(normalizeFilePath),
42
49
  normalizeFilePath(target),
43
50
  password,
44
- encryptionMethod
51
+ encryptionMethod,
52
+ compressionLevel
45
53
  )
46
54
  : RNZipArchive.zipFolderWithPassword(
47
55
  normalizeFilePath(source),
48
56
  normalizeFilePath(target),
49
57
  password,
50
- encryptionMethod
58
+ encryptionMethod,
59
+ compressionLevel
51
60
  );
52
61
  };
53
62
 
54
- export const zip = (source, target) => {
63
+ export const zip = (source, target, compressionLevel = DEFAULT_COMPRESSION) => {
55
64
  return Array.isArray(source)
56
65
  ? RNZipArchive.zipFiles(
57
66
  source.map(normalizeFilePath),
58
- normalizeFilePath(target)
67
+ normalizeFilePath(target),
68
+ compressionLevel
59
69
  )
60
70
  : RNZipArchive.zipFolder(
61
71
  normalizeFilePath(source),
62
- normalizeFilePath(target)
72
+ normalizeFilePath(target),
73
+ compressionLevel
63
74
  );
64
75
  };
65
76
 
@@ -7,6 +7,7 @@
7
7
  //
8
8
 
9
9
  #import "RNZipArchive.h"
10
+ #import <zlib.h>
10
11
 
11
12
  #if __has_include(<React/RCTEventDispatcher.h>)
12
13
  #import <React/RCTEventDispatcher.h>
@@ -91,12 +92,14 @@ RCT_EXPORT_METHOD(unzipWithPassword:(NSString *)from
91
92
  if (success) {
92
93
  resolve(destinationPath);
93
94
  } else {
94
- reject(@"unzip_error", @"unable to unzip", error);
95
+ NSString *errorMessage = error ? [error localizedDescription] : @"unable to unzip";
96
+ reject(@"unzip_error", errorMessage, error);
95
97
  }
96
98
  }
97
99
 
98
100
  RCT_EXPORT_METHOD(zipFolder:(NSString *)from
99
101
  destinationPath:(NSString *)destinationPath
102
+ compressionLevel:(double)compressionLevel
100
103
  resolver:(RCTPromiseResolveBlock)resolve
101
104
  rejecter:(RCTPromiseRejectBlock)reject) {
102
105
  self.progress = 0.0;
@@ -106,7 +109,13 @@ RCT_EXPORT_METHOD(zipFolder:(NSString *)from
106
109
  BOOL success;
107
110
  [self setProgressHandler];
108
111
 
109
- 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];
110
119
 
111
120
  self.progress = 1.0;
112
121
  [self zipArchiveProgressEvent:1 total:1]; // force 100%
@@ -121,6 +130,7 @@ RCT_EXPORT_METHOD(zipFolder:(NSString *)from
121
130
 
122
131
  RCT_EXPORT_METHOD(zipFiles:(NSArray<NSString *> *)from
123
132
  destinationPath:(NSString *)destinationPath
133
+ compressionLevel:(double)compressionLevel
124
134
  resolver:(RCTPromiseResolveBlock)resolve
125
135
  rejecter:(RCTPromiseRejectBlock)reject) {
126
136
  self.progress = 0.0;
@@ -143,11 +153,11 @@ RCT_EXPORT_METHOD(zipFiles:(NSArray<NSString *> *)from
143
153
  }
144
154
  }
145
155
 
146
-
147
156
  RCT_EXPORT_METHOD(zipFolderWithPassword:(NSString *)from
148
157
  destinationPath:(NSString *)destinationPath
149
158
  password:(NSString *)password
150
159
  encryptionType:(NSString *)encryptionType
160
+ compressionLevel:(double)compressionLevel
151
161
  resolver:(RCTPromiseResolveBlock)resolve
152
162
  rejecter:(RCTPromiseRejectBlock)reject) {
153
163
  self.progress = 0.0;
@@ -156,7 +166,14 @@ RCT_EXPORT_METHOD(zipFolderWithPassword:(NSString *)from
156
166
 
157
167
  BOOL success;
158
168
  [self setProgressHandler];
159
- success = [SSZipArchive createZipFileAtPath:destinationPath withContentsOfDirectory:from keepParentDirectory:NO withPassword:password andProgressHandler:self.progressHandler];
169
+ BOOL useAES = encryptionType && ![encryptionType isEqualToString:@"STANDARD"];
170
+ success = [SSZipArchive createZipFileAtPath:destinationPath
171
+ withContentsOfDirectory:from
172
+ keepParentDirectory:NO
173
+ compressionLevel:compressionLevel
174
+ password:password
175
+ AES:useAES
176
+ progressHandler:self.progressHandler];
160
177
 
161
178
  self.progress = 1.0;
162
179
  [self zipArchiveProgressEvent:1 total:1]; // force 100%
@@ -173,6 +190,7 @@ RCT_EXPORT_METHOD(zipFilesWithPassword:(NSArray<NSString *> *)from
173
190
  destinationPath:(NSString *)destinationPath
174
191
  password:(NSString *)password
175
192
  encryptionType:(NSString *)encryptionType
193
+ compressionLevel:(double)compressionLevel
176
194
  resolver:(RCTPromiseResolveBlock)resolve
177
195
  rejecter:(RCTPromiseRejectBlock)reject) {
178
196
  self.progress = 0.0;
@@ -181,6 +199,7 @@ RCT_EXPORT_METHOD(zipFilesWithPassword:(NSArray<NSString *> *)from
181
199
 
182
200
  BOOL success;
183
201
  [self setProgressHandler];
202
+ // Note: withFilesAtPaths doesn't have AES class method, using password only
184
203
  success = [SSZipArchive createZipFileAtPath:destinationPath withFilesAtPaths:from withPassword:password];
185
204
 
186
205
  self.progress = 1.0;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-zip-archive",
3
- "version": "7.0.2",
3
+ "version": "7.1.0",
4
4
  "description": "A little wrapper on ZipArchive for react-native",
5
5
  "main": "index.js",
6
6
  "scripts": {