|
32 | 32 | import net.lingala.zip4j.exception.ZipException; |
33 | 33 | import net.lingala.zip4j.model.FileHeader; |
34 | 34 | import net.lingala.zip4j.progress.ProgressMonitor; |
| 35 | +import net.lingala.zip4j.model.ZipParameters; |
| 36 | +import net.lingala.zip4j.util.Zip4jConstants; |
35 | 37 |
|
36 | 38 | public class RNZipArchiveModule extends ReactContextBaseJavaModule { |
37 | 39 | private static final String TAG = RNZipArchiveModule.class.getSimpleName(); |
@@ -339,6 +341,91 @@ public void zip(String fileOrDirectory, String destDirectory, Promise promise) { |
339 | 341 | } |
340 | 342 | } |
341 | 343 |
|
| 344 | + @ReactMethod |
| 345 | + public void zipWithPassword(final String fileOrDirectory, final String destDirectory, final String password, |
| 346 | + final String encyptionMethod, final Promise promise) { |
| 347 | + new Thread(new Runnable() { |
| 348 | + @Override |
| 349 | + public void run() { |
| 350 | + |
| 351 | + try { |
| 352 | + net.lingala.zip4j.core.ZipFile zipFile = new net.lingala.zip4j.core.ZipFile(destDirectory); |
| 353 | + |
| 354 | + ZipParameters parameters = new ZipParameters(); |
| 355 | + parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); |
| 356 | + parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); |
| 357 | + |
| 358 | + String encParts[] = encyptionMethod.split("-"); |
| 359 | + |
| 360 | + if (password != null && !password.isEmpty()) { |
| 361 | + parameters.setEncryptFiles(true); |
| 362 | + if (encParts[0].equals("AES")) { |
| 363 | + parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES); |
| 364 | + if (encParts[1].equals("128")) { |
| 365 | + parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_128); |
| 366 | + } else if (encParts[1].equals("256")) { |
| 367 | + parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256); |
| 368 | + } else { |
| 369 | + parameters.setAesKeyStrength(Zip4jConstants.ENC_METHOD_STANDARD); |
| 370 | + } |
| 371 | + } else if (encyptionMethod.equals("STANDARD")) { |
| 372 | + parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD); |
| 373 | + Log.d(TAG, "Standard Encryption"); |
| 374 | + } else { |
| 375 | + parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD); |
| 376 | + Log.d(TAG, "Encryption type not supported default to Standard Encryption"); |
| 377 | + } |
| 378 | + parameters.setPassword(password); |
| 379 | + } else { |
| 380 | + promise.reject(null, "Password is empty"); |
| 381 | + } |
| 382 | + |
| 383 | + updateProgress(0, 100, destDirectory); |
| 384 | + |
| 385 | + File f = new File(fileOrDirectory); |
| 386 | + |
| 387 | + int totalFiles = 0; |
| 388 | + int fileCounter = 0; |
| 389 | + |
| 390 | + if (f.exists()) { |
| 391 | + if (f.isDirectory()) { |
| 392 | + |
| 393 | + List<File> files = getSubFiles(f, true); |
| 394 | + |
| 395 | + totalFiles = files.size(); |
| 396 | + for (int i = 0; i < files.size(); i++) { |
| 397 | + if (files.get(i).isDirectory()) { |
| 398 | + zipFile.addFolder(files.get(i).getAbsolutePath(), parameters); |
| 399 | + } |
| 400 | + else { |
| 401 | + zipFile.addFile(files.get(i), parameters); |
| 402 | + } |
| 403 | + fileCounter += 1; |
| 404 | + updateProgress(fileCounter, totalFiles, destDirectory); |
| 405 | + } |
| 406 | + |
| 407 | + } else { |
| 408 | + totalFiles = 1; |
| 409 | + zipFile.addFile(f, parameters); |
| 410 | + fileCounter += 1; |
| 411 | + updateProgress(fileCounter, totalFiles, destDirectory); |
| 412 | + } |
| 413 | + } |
| 414 | + else { |
| 415 | + promise.reject(null, "File or folder does not exist"); |
| 416 | + } |
| 417 | + |
| 418 | + updateProgress(1, 1, destDirectory); // force 100% |
| 419 | + promise.resolve(destDirectory); |
| 420 | + |
| 421 | + } catch (Exception ex) { |
| 422 | + promise.reject(null, ex.getMessage()); |
| 423 | + return; |
| 424 | + } |
| 425 | + } |
| 426 | + }).start(); |
| 427 | + } |
| 428 | + |
342 | 429 | private List<File> getSubFiles(File baseDir, boolean isContainFolder) { |
343 | 430 | List<File> fileList = new ArrayList<>(); |
344 | 431 | File[] tmpList = baseDir.listFiles(); |
|
0 commit comments