44import android .util .Log ;
55
66import com .facebook .react .bridge .Arguments ;
7- import com .facebook .react .bridge .Callback ;
7+ import com .facebook .react .bridge .Promise ;
88import com .facebook .react .bridge .ReactApplicationContext ;
99import com .facebook .react .bridge .ReactContextBaseJavaModule ;
1010import com .facebook .react .bridge .ReactMethod ;
@@ -43,21 +43,27 @@ public String getName() {
4343 }
4444
4545 @ ReactMethod
46- public void unzip (String zipFilePath , String destDirectory , Callback callback ) {
46+ public void unzip (String zipFilePath , String destDirectory , Promise promise ) {
4747 FileInputStream inputStream ;
4848 File file ;
4949 try {
5050 inputStream = new FileInputStream (zipFilePath );
5151 file = new File (zipFilePath );
5252 } catch (FileNotFoundException | NullPointerException e ) {
53- callback . invoke ( makeErrorPayload ( "Couldn't open file " + zipFilePath + ". " , e ) );
53+ promise . reject ( null , "Couldn't open file " + zipFilePath + ". " );
5454 return ;
5555 }
56- unzipStream (zipFilePath , destDirectory , inputStream , file .length (), callback );
56+ try {
57+ unzipStream (zipFilePath , destDirectory , inputStream , file .length ());
58+ } catch (Exception ex ) {
59+ promise .reject (null , ex .getMessage ());
60+ return ;
61+ }
62+ promise .resolve (destDirectory );
5763 }
5864
5965 @ ReactMethod
60- public void unzipAssets (String assetsPath , String destDirectory , Callback completionCallback ) {
66+ public void unzipAssets (String assetsPath , String destDirectory , Promise promise ) {
6167 InputStream assetsInputStream ;
6268 long size ;
6369
@@ -66,15 +72,21 @@ public void unzipAssets(String assetsPath, String destDirectory, Callback comple
6672 AssetFileDescriptor fileDescriptor = getReactApplicationContext ().getAssets ().openFd (assetsPath );
6773 size = fileDescriptor .getLength ();
6874 } catch (IOException e ) {
69- completionCallback . invoke ( makeErrorPayload ( String .format ("Asset file `%s` could not be opened" , assetsPath ), e ));
75+ promise . reject ( null , String .format ("Asset file `%s` could not be opened" , assetsPath ));
7076 return ;
7177 }
7278
73- unzipStream (assetsPath , destDirectory , assetsInputStream , size , completionCallback );
79+ try {
80+ unzipStream (assetsPath , destDirectory , assetsInputStream , size );
81+ } catch (Exception ex ) {
82+ promise .reject (null , ex .getMessage ());
83+ return ;
84+ }
85+ promise .resolve (destDirectory );
7486 }
7587
7688 @ ReactMethod
77- public void zip (String fileOrDirectory , String destDirectory , Callback callback ) {
89+ public void zip (String fileOrDirectory , String destDirectory , Promise promise ) {
7890 List <String > filePaths = new ArrayList <>();
7991 File file ;
8092 try {
@@ -92,14 +104,21 @@ public void zip(String fileOrDirectory, String destDirectory, Callback callback)
92104 throw new FileNotFoundException (fileOrDirectory );
93105 }
94106 } catch (FileNotFoundException | NullPointerException e ) {
95- callback . invoke ( makeErrorPayload ( "Couldn't open file/directory " + fileOrDirectory + ". " , e ) );
107+ promise . reject ( null , "Couldn't open file/directory " + fileOrDirectory + "." );
96108 return ;
97109 }
98110
99- zipStream (filePaths .toArray (new String [filePaths .size ()]), destDirectory , filePaths .size (), callback );
111+ try {
112+ zipStream (filePaths .toArray (new String [filePaths .size ()]), destDirectory , filePaths .size ());
113+ } catch (Exception ex ) {
114+ promise .reject (null , ex .getMessage ());
115+ return ;
116+ }
117+
118+ promise .resolve (destDirectory );
100119 }
101120
102- private void zipStream (String [] files , String destFile , long totalSize , Callback completionCallback ) {
121+ private void zipStream (String [] files , String destFile , long totalSize ) throws Exception {
103122 try {
104123 if (destFile .contains ("/" )) {
105124 File destDir = new File (destFile .substring (0 , destFile .lastIndexOf ("/" )));
@@ -136,11 +155,10 @@ private void zipStream(String[] files, String destFile, long totalSize, Callback
136155 }
137156 updateProgress (1 , 1 , destFile ); // force 100%
138157 out .close ();
139- completionCallback .invoke (null , null );
140158 } catch (Exception ex ) {
141159 ex .printStackTrace ();
142160 updateProgress (0 , 1 , destFile ); // force 0%
143- completionCallback . invoke ( makeErrorPayload ( String .format ("Couldn't zip %s" , destFile ), ex ));
161+ throw new Exception ( String .format ("Couldn't zip %s" , destFile ));
144162 }
145163 }
146164
@@ -161,7 +179,7 @@ private List<File> getSubFiles(File baseDir, boolean isContainFolder) {
161179 return fileList ;
162180 }
163181
164- private void unzipStream (String zipFilePath , String destDirectory , InputStream inputStream , long totalSize , Callback completionCallback ) {
182+ private void unzipStream (String zipFilePath , String destDirectory , InputStream inputStream , long totalSize ) throws Exception {
165183 try {
166184 File destDir = new File (destDirectory );
167185 if (!destDir .exists ()) {
@@ -195,11 +213,10 @@ private void unzipStream(String zipFilePath, String destDirectory, InputStream i
195213 updateProgress (1 , 1 , zipFilePath ); // force 100%
196214 bin .close ();
197215 zipIn .close ();
198- completionCallback .invoke (null , null );
199216 } catch (Exception ex ) {
200217 ex .printStackTrace ();
201218 updateProgress (0 , 1 , zipFilePath ); // force 0%
202- completionCallback . invoke ( makeErrorPayload ( String .format ("Couldn't extract %s" , zipFilePath ), ex ));
219+ throw new Exception ( String .format ("Couldn't extract %s" , zipFilePath ));
203220 }
204221 }
205222
@@ -235,10 +252,4 @@ private long extractFile(ZipInputStream zipIn, String filePath) throws IOExcepti
235252
236253 return size ;
237254 }
238-
239- private WritableMap makeErrorPayload (String message , Exception ex ) {
240- WritableMap error = Arguments .createMap ();
241- error .putString ("message" , String .format ("%s (%s)" , message , ex .getMessage ()));
242- return error ;
243- }
244255}
0 commit comments