2222import java .io .PrintWriter ;
2323import java .io .StringWriter ;
2424import java .util .ArrayList ;
25+ import java .util .Arrays ;
2526import java .util .Enumeration ;
2627import java .util .List ;
2728import java .util .zip .ZipEntry ;
@@ -303,82 +304,70 @@ public void onCopyProgress(long bytesRead) {
303304
304305 @ ReactMethod
305306 public void zip (String fileOrDirectory , String destDirectory , Promise promise ) {
306- List < String > filePaths = new ArrayList <>();
307+ try {
307308
308- String fromDirectory ;
309- try {
310- File tmp = new File (fileOrDirectory );
311- if (tmp .exists ()) {
312- if (tmp .isDirectory ()) {
313- fromDirectory = fileOrDirectory ;
314- List <File > files = getSubFiles (tmp , true );
315- for (int i = 0 ; i < files .size (); i ++) {
316- filePaths .add (files .get (i ).getAbsolutePath ());
309+ ZipParameters parameters = new ZipParameters ();
310+ parameters .setCompressionMethod (Zip4jConstants .COMP_DEFLATE );
311+ parameters .setCompressionLevel (Zip4jConstants .DEFLATE_LEVEL_NORMAL );
312+
313+ processZip (fileOrDirectory , destDirectory , parameters , promise );
314+
315+ } catch (Exception ex ) {
316+ promise .reject (null , ex .getMessage ());
317+ return ;
318+ }
319+ }
320+
321+ @ ReactMethod
322+ public void zipWithPassword (String fileOrDirectory , String destDirectory , String password ,
323+ String encryptionMethod , Promise promise ) {
324+ try {
325+
326+ ZipParameters parameters = new ZipParameters ();
327+ parameters .setCompressionMethod (Zip4jConstants .COMP_DEFLATE );
328+ parameters .setCompressionLevel (Zip4jConstants .DEFLATE_LEVEL_NORMAL );
329+
330+ String encParts [] = _encryptionMethod .split ("-" );
331+
332+ if (password != null && !password .isEmpty ()) {
333+ parameters .setEncryptFiles (true );
334+ if (encParts [0 ].equals ("AES" )) {
335+ parameters .setEncryptionMethod (Zip4jConstants .ENC_METHOD_AES );
336+ if (encParts [1 ].equals ("128" )) {
337+ parameters .setAesKeyStrength (Zip4jConstants .AES_STRENGTH_128 );
338+ } else if (encParts [1 ].equals ("256" )) {
339+ parameters .setAesKeyStrength (Zip4jConstants .AES_STRENGTH_256 );
340+ } else {
341+ parameters .setAesKeyStrength (Zip4jConstants .ENC_METHOD_STANDARD );
317342 }
343+ } else if (_encryptionMethod .equals ("STANDARD" )) {
344+ parameters .setEncryptionMethod (Zip4jConstants .ENC_METHOD_STANDARD );
345+ Log .d (TAG , "Standard Encryption" );
318346 } else {
319- fromDirectory = fileOrDirectory . substring ( 0 , fileOrDirectory . lastIndexOf ( "/" ) );
320- filePaths . add ( fileOrDirectory );
347+ parameters . setEncryptionMethod ( Zip4jConstants . ENC_METHOD_STANDARD );
348+ Log . d ( TAG , "Encryption type not supported default to Standard Encryption" );
321349 }
350+ parameters .setPassword (password );
322351 } else {
323- throw new FileNotFoundException ( fileOrDirectory );
352+ promise . reject ( null , "Password is empty" );
324353 }
325- } catch (FileNotFoundException | NullPointerException e ) {
326- promise .reject (null , "Couldn't open file/directory " + fileOrDirectory + "." );
327- return ;
328- }
329354
330- try {
331- String [] filePathArray = filePaths .toArray (new String [filePaths .size ()]);
332- new ZipTask (filePathArray , destDirectory , fromDirectory , promise , this ).zip ();
355+ processZip (fileOrDirectory , destDirectory , parameters , promise );
356+
333357 } catch (Exception ex ) {
334358 promise .reject (null , ex .getMessage ());
335359 return ;
336360 }
361+
337362 }
338363
339- @ ReactMethod
340- public void zipWithPassword (final String fileOrDirectory , final String destDirectory , final String password ,
341- final String encryptionMethod , final Promise promise ) {
364+ private void processZip (final String fileOrDirectory , final String destDirectory , final ZipParameters parameters , final Promise promise ) {
342365 new Thread (new Runnable () {
343366 @ Override
344367 public void run () {
345- String _encryptionMethod = encryptionMethod ;
346- if (_encryptionMethod == null ) {
347- _encryptionMethod = "STANDARD" ;
348- }
349-
350368 try {
351369 net .lingala .zip4j .core .ZipFile zipFile = new net .lingala .zip4j .core .ZipFile (destDirectory );
352370
353- ZipParameters parameters = new ZipParameters ();
354- parameters .setCompressionMethod (Zip4jConstants .COMP_DEFLATE );
355- parameters .setCompressionLevel (Zip4jConstants .DEFLATE_LEVEL_NORMAL );
356-
357- String encParts [] = _encryptionMethod .split ("-" );
358-
359- if (password != null && !password .isEmpty ()) {
360- parameters .setEncryptFiles (true );
361- if (encParts [0 ].equals ("AES" )) {
362- parameters .setEncryptionMethod (Zip4jConstants .ENC_METHOD_AES );
363- if (encParts [1 ].equals ("128" )) {
364- parameters .setAesKeyStrength (Zip4jConstants .AES_STRENGTH_128 );
365- } else if (encParts [1 ].equals ("256" )) {
366- parameters .setAesKeyStrength (Zip4jConstants .AES_STRENGTH_256 );
367- } else {
368- parameters .setAesKeyStrength (Zip4jConstants .ENC_METHOD_STANDARD );
369- }
370- } else if (_encryptionMethod .equals ("STANDARD" )) {
371- parameters .setEncryptionMethod (Zip4jConstants .ENC_METHOD_STANDARD );
372- Log .d (TAG , "Standard Encryption" );
373- } else {
374- parameters .setEncryptionMethod (Zip4jConstants .ENC_METHOD_STANDARD );
375- Log .d (TAG , "Encryption type not supported default to Standard Encryption" );
376- }
377- parameters .setPassword (password );
378- } else {
379- promise .reject (null , "Password is empty" );
380- }
381-
382371 updateProgress (0 , 100 , destDirectory );
383372
384373 File f = new File (fileOrDirectory );
@@ -389,7 +378,7 @@ public void run() {
389378 if (f .exists ()) {
390379 if (f .isDirectory ()) {
391380
392- List <File > files = getSubFiles ( f , true );
381+ List <File > files = Arrays . asList ( f . listFiles () );
393382
394383 totalFiles = files .size ();
395384 for (int i = 0 ; i < files .size (); i ++) {
@@ -425,23 +414,6 @@ public void run() {
425414 }).start ();
426415 }
427416
428- private List <File > getSubFiles (File baseDir , boolean isContainFolder ) {
429- List <File > fileList = new ArrayList <>();
430- File [] tmpList = baseDir .listFiles ();
431- for (File file : tmpList ) {
432- if (file .isFile ()) {
433- fileList .add (file );
434- }
435- if (file .isDirectory ()) {
436- if (isContainFolder ) {
437- fileList .add (file ); //key code
438- }
439- fileList .addAll (getSubFiles (file , isContainFolder ));
440- }
441- }
442- return fileList ;
443- }
444-
445417 protected void updateProgress (long extractedBytes , long totalSize , String zipFilePath ) {
446418 // Ensure progress can't overflow 1
447419 double progress = Math .min ((double ) extractedBytes / (double ) totalSize , 1 );
0 commit comments