24
24
import java .io .StringWriter ;
25
25
import java .util .ArrayList ;
26
26
import java .util .Arrays ;
27
+ import java .util .Collections ;
27
28
import java .util .Enumeration ;
28
29
import java .util .List ;
29
30
import java .util .zip .ZipEntry ;
@@ -195,7 +196,7 @@ public void unzipAssets(final String assetsPath, final String destDirectory, fin
195
196
@ Override
196
197
public void run () {
197
198
InputStream assetsInputStream ;
198
- final long size ;
199
+ final long compressedSize ;
199
200
200
201
try {
201
202
if (assetsPath .startsWith ("content://" )) {
@@ -204,11 +205,11 @@ public void run() {
204
205
205
206
assetsInputStream = contentResolver .openInputStream (assetUri );
206
207
var fileDescriptor = contentResolver .openFileDescriptor (assetUri , "r" );
207
- size = fileDescriptor .getStatSize ();
208
+ compressedSize = fileDescriptor .getStatSize ();
208
209
} else {
209
210
assetsInputStream = getReactApplicationContext ().getAssets ().open (assetsPath );
210
211
AssetFileDescriptor fileDescriptor = getReactApplicationContext ().getAssets ().openFd (assetsPath );
211
- size = fileDescriptor .getLength ();
212
+ compressedSize = fileDescriptor .getLength ();
212
213
}
213
214
} catch (IOException e ) {
214
215
promise .reject (null , String .format ("Asset file `%s` could not be opened" , assetsPath ));
@@ -227,13 +228,15 @@ public void run() {
227
228
228
229
ZipEntry entry ;
229
230
230
- final long [] extractedBytes = { 0 } ;
231
- final int [] lastPercentage = { 0 };
231
+ long extractedBytes = 0 ;
232
+ updateProgress ( extractedBytes , compressedSize , assetsPath ); // force 0%
232
233
233
- updateProgress (0 , 1 , assetsPath ); // force 0%
234
234
File fout ;
235
235
while ((entry = zipIn .getNextEntry ()) != null ) {
236
236
if (entry .isDirectory ()) continue ;
237
+
238
+ Log .i ("rnziparchive" , "Extracting: " + entry .getName ());
239
+
237
240
fout = new File (destDirectory , entry .getName ());
238
241
String canonicalPath = fout .getCanonicalPath ();
239
242
String destDirCanonicalPath = (new File (destDirectory ).getCanonicalPath ()) + File .separator ;
@@ -247,31 +250,19 @@ public void run() {
247
250
(new File (fout .getParent ())).mkdirs ();
248
251
}
249
252
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
-
267
253
FileOutputStream out = new FileOutputStream (fout );
268
254
BufferedOutputStream Bout = new BufferedOutputStream (out );
269
- StreamUtil .copy (bin , Bout , cb );
255
+ StreamUtil .copy (bin , Bout , null );
270
256
Bout .close ();
271
257
out .close ();
258
+
259
+ extractedBytes += entry .getCompressedSize ();
260
+
261
+ updateProgress (extractedBytes , compressedSize , entry .getName ());
272
262
}
273
263
274
- updateProgress (1 , 1 , assetsPath ); // force 100%
264
+ updateProgress (compressedSize , compressedSize , assetsPath ); // force 100%
265
+
275
266
bin .close ();
276
267
zipIn .close ();
277
268
} catch (Exception ex ) {
0 commit comments