2424import java .io .StringWriter ;
2525import java .util .ArrayList ;
2626import java .util .Arrays ;
27+ import java .util .Collections ;
2728import java .util .Enumeration ;
2829import java .util .List ;
2930import java .util .zip .ZipEntry ;
@@ -195,7 +196,7 @@ public void unzipAssets(final String assetsPath, final String destDirectory, fin
195196 @ Override
196197 public void run () {
197198 InputStream assetsInputStream ;
198- final long size ;
199+ final long compressedSize ;
199200
200201 try {
201202 if (assetsPath .startsWith ("content://" )) {
@@ -204,11 +205,11 @@ public void run() {
204205
205206 assetsInputStream = contentResolver .openInputStream (assetUri );
206207 var fileDescriptor = contentResolver .openFileDescriptor (assetUri , "r" );
207- size = fileDescriptor .getStatSize ();
208+ compressedSize = fileDescriptor .getStatSize ();
208209 } else {
209210 assetsInputStream = getReactApplicationContext ().getAssets ().open (assetsPath );
210211 AssetFileDescriptor fileDescriptor = getReactApplicationContext ().getAssets ().openFd (assetsPath );
211- size = fileDescriptor .getLength ();
212+ compressedSize = fileDescriptor .getLength ();
212213 }
213214 } catch (IOException e ) {
214215 promise .reject (null , String .format ("Asset file `%s` could not be opened" , assetsPath ));
@@ -227,13 +228,15 @@ public void run() {
227228
228229 ZipEntry entry ;
229230
230- final long [] extractedBytes = { 0 } ;
231- final int [] lastPercentage = { 0 };
231+ long extractedBytes = 0 ;
232+ updateProgress ( extractedBytes , compressedSize , assetsPath ); // force 0%
232233
233- updateProgress (0 , 1 , assetsPath ); // force 0%
234234 File fout ;
235235 while ((entry = zipIn .getNextEntry ()) != null ) {
236236 if (entry .isDirectory ()) continue ;
237+
238+ Log .i ("rnziparchive" , "Extracting: " + entry .getName ());
239+
237240 fout = new File (destDirectory , entry .getName ());
238241 String canonicalPath = fout .getCanonicalPath ();
239242 String destDirCanonicalPath = (new File (destDirectory ).getCanonicalPath ()) + File .separator ;
@@ -247,31 +250,19 @@ public void run() {
247250 (new File (fout .getParent ())).mkdirs ();
248251 }
249252
250- final ZipEntry finalEntry = entry ;
251- StreamUtil .ProgressCallback cb = new StreamUtil .ProgressCallback () {
252- @ Override
253- public void onCopyProgress (long bytesRead ) {
254- extractedBytes [0 ] += bytesRead ;
255-
256- int lastTime = lastPercentage [0 ];
257- int percentDone = (int ) ((double ) extractedBytes [0 ] * 100 / (double ) size );
258-
259- // update at most once per percent.
260- if (percentDone > lastTime ) {
261- lastPercentage [0 ] = percentDone ;
262- updateProgress (extractedBytes [0 ], size , finalEntry .getName ());
263- }
264- }
265- };
266-
267253 FileOutputStream out = new FileOutputStream (fout );
268254 BufferedOutputStream Bout = new BufferedOutputStream (out );
269- StreamUtil .copy (bin , Bout , cb );
255+ StreamUtil .copy (bin , Bout , null );
270256 Bout .close ();
271257 out .close ();
258+
259+ extractedBytes += entry .getCompressedSize ();
260+
261+ updateProgress (extractedBytes , compressedSize , entry .getName ());
272262 }
273263
274- updateProgress (1 , 1 , assetsPath ); // force 100%
264+ updateProgress (compressedSize , compressedSize , assetsPath ); // force 100%
265+
275266 bin .close ();
276267 zipIn .close ();
277268 } catch (Exception ex ) {
0 commit comments