Skip to content

Commit 8508480

Browse files
committed
fix unzip progess issue on ios
1 parent 8b03e1c commit 8508480

File tree

2 files changed

+42
-15
lines changed

2 files changed

+42
-15
lines changed

ios/RNZipArchive.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414
#endif
1515
#import "SSZipArchive/SSZipArchive.h"
1616

17-
@interface RNZipArchive : NSObject<RCTBridgeModule, SSZipArchiveDelegate>
17+
@interface RNZipArchive : NSObject<RCTBridgeModule, SSZipArchiveDelegate>{
18+
NSString *unzippedFilePath;
19+
float unzipProgress;
20+
}
21+
22+
@property (nonatomic) NSString *unzippedFilePath;
23+
@property (nonatomic) float unzipProgress;
1824

1925
@end

ios/RNZipArchive.m

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
@implementation RNZipArchive
1919

2020
@synthesize bridge = _bridge;
21+
@synthesize unzipProgress;
22+
@synthesize unzippedFilePath;
2123

2224
RCT_EXPORT_MODULE();
2325

@@ -34,14 +36,16 @@ @implementation RNZipArchive
3436
charset:(NSString *)charset
3537
resolver:(RCTPromiseResolveBlock)resolve
3638
rejecter:(RCTPromiseRejectBlock)reject) {
37-
38-
[self zipArchiveProgressEvent:0 total:1 filePath:from]; // force 0%
39+
self.unzipProgress = 0.0;
40+
self.unzippedFilePath = @"";
41+
[self zipArchiveProgressEvent:0 total:1]; // force 0%
3942

4043
NSError *error = nil;
4144

4245
BOOL success = [SSZipArchive unzipFileAtPath:from toDestination:destinationPath overwrite:YES password:nil error:&error delegate:self];
4346

44-
[self zipArchiveProgressEvent:1 total:1 filePath:from]; // force 100%
47+
self.unzipProgress = 1.0;
48+
[self zipArchiveProgressEvent:1 total:1]; // force 100%
4549

4650
if (success) {
4751
resolve(destinationPath);
@@ -55,14 +59,16 @@ @implementation RNZipArchive
5559
password:(NSString *)password
5660
resolver:(RCTPromiseResolveBlock)resolve
5761
rejecter:(RCTPromiseRejectBlock)reject) {
58-
59-
[self zipArchiveProgressEvent:0 total:1 filePath:from]; // force 0%
62+
self.unzipProgress = 0.0;
63+
self.unzippedFilePath = @"";
64+
[self zipArchiveProgressEvent:0 total:1]; // force 0%
6065

6166
NSError *error = nil;
6267

6368
BOOL success = [SSZipArchive unzipFileAtPath:from toDestination:destinationPath overwrite:YES password:password error:&error delegate:self];
6469

65-
[self zipArchiveProgressEvent:1 total:1 filePath:from]; // force 100%
70+
self.unzipProgress = 1.0;
71+
[self zipArchiveProgressEvent:1 total:1]; // force 100%
6672

6773
if (success) {
6874
resolve(destinationPath);
@@ -75,8 +81,9 @@ @implementation RNZipArchive
7581
destinationPath:(NSString *)destinationPath
7682
resolver:(RCTPromiseResolveBlock)resolve
7783
rejecter:(RCTPromiseRejectBlock)reject) {
78-
79-
[self zipArchiveProgressEvent:0 total:1 filePath:destinationPath]; // force 0%
84+
self.unzipProgress = 0.0;
85+
self.unzippedFilePath = @"";
86+
[self zipArchiveProgressEvent:0 total:1]; // force 0%
8087

8188
NSFileManager *fileManager = [[NSFileManager alloc] init];
8289
BOOL isDir;
@@ -88,7 +95,8 @@ @implementation RNZipArchive
8895
success = [SSZipArchive createZipFileAtPath:destinationPath withFilesAtPaths:@[from]];
8996
}
9097

91-
[self zipArchiveProgressEvent:1 total:1 filePath:destinationPath]; // force 100%
98+
self.unzipProgress = 1.0;
99+
[self zipArchiveProgressEvent:1 total:1]; // force 100%
92100

93101
if (success) {
94102
resolve(destinationPath);
@@ -105,7 +113,9 @@ @implementation RNZipArchive
105113
encryptionType:(NSString *)encryptionType
106114
resolver:(RCTPromiseResolveBlock)resolve
107115
rejecter:(RCTPromiseRejectBlock)reject) {
108-
[self zipArchiveProgressEvent:0 total:1 filePath:destinationPath]; // force 0%
116+
self.unzipProgress = 0.0;
117+
self.unzippedFilePath = @"";
118+
[self zipArchiveProgressEvent:0 total:1]; // force 0%
109119

110120
NSFileManager *fileManager = [[NSFileManager alloc] init];
111121
BOOL isDir;
@@ -117,7 +127,8 @@ @implementation RNZipArchive
117127
success = [SSZipArchive createZipFileAtPath:destinationPath withFilesAtPaths:@[from] withPassword:password];
118128
}
119129

120-
[self zipArchiveProgressEvent:1 total:1 filePath:destinationPath]; // force 100%
130+
self.unzipProgress = 1.0;
131+
[self zipArchiveProgressEvent:1 total:1]; // force 100%
121132

122133
if (success) {
123134
resolve(destinationPath);
@@ -131,10 +142,20 @@ - (dispatch_queue_t)methodQueue {
131142
return dispatch_queue_create("com.mockingbot.ReactNative.ZipArchiveQueue", DISPATCH_QUEUE_SERIAL);
132143
}
133144

134-
- (void)zipArchiveProgressEvent:(unsigned long long)loaded total:(unsigned long long)total filePath:(NSString *)filePath {
145+
- (void)zipArchiveProgressEvent:(unsigned long long)loaded total:(unsigned long long)total {
146+
self.unzipProgress = (float)loaded / (float)total;
147+
[self dispatchProgessEvent:self.unzipProgress unzippedFilePath:self.unzippedFilePath];
148+
}
149+
150+
- (void)zipArchiveDidUnzipFileAtIndex:(NSInteger)fileIndex totalFiles:(NSInteger)totalFiles archivePath:(NSString *)archivePath unzippedFilePath:(NSString *)unzippedFilePath {
151+
self.unzippedFilePath = unzippedFilePath;
152+
[self dispatchProgessEvent:self.unzipProgress unzippedFilePath:self.unzippedFilePath];
153+
}
154+
155+
- (void)dispatchProgessEvent:(float)progress unzippedFilePath:(NSString *)unzippedFilePath {
135156
[self.bridge.eventDispatcher sendAppEventWithName:@"zipArchiveProgressEvent" body:@{
136-
@"progress": @((float)loaded / (float)total),
137-
@"filePath": filePath
157+
@"progress": @(progress),
158+
@"filePath": unzippedFilePath
138159
}];
139160
}
140161

0 commit comments

Comments
 (0)