Skip to content

Commit

Permalink
make getUncompressedSize public, add iOS function
Browse files Browse the repository at this point in the history
  • Loading branch information
bdtren authored and plrthink committed Sep 2, 2023
1 parent b60b84d commit 298b0c7
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
//import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

import net.lingala.zip4j.exception.ZipException;
Expand Down Expand Up @@ -415,6 +416,12 @@ protected void updateProgress(long extractedBytes, long totalSize, String zipFil
.emit(PROGRESS_EVENT_NAME, map);
}

@ReactMethod
public void getUncompressedSize(String zipFilePath, String charset, final Promise promise) {
long totalSize = getUncompressedSize(zipFilePath, charset);
promise.resolve((double) totalSize);
}

/**
* Return the uncompressed size of the ZipFile (only works for files on disk, not in assets)
*
Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ declare module 'react-native-zip-archive' {
export function unzipWithPassword(assetPath: string, target: string, password: string): Promise<string>;
export function unzipAssets(assetPath: string, target: string): Promise<string>;
export function subscribe(callback: ({ progress, filePath }: { progress: number, filePath: string }) => void): NativeEventSubscription;
export function getUncompressedSize(source: string, charset?: string): Promise<number>;
}
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,7 @@ export const unzipAssets = (source, target) => {
export const subscribe = (callback) => {
return rnzaEmitter.addListener("zipArchiveProgressEvent", callback);
};

export const getUncompressedSize = (source, charset = "UTF-8") => {
return RNZipArchive.getUncompressedSize(normalizeFilePath(source), charset);
};
16 changes: 16 additions & 0 deletions ios/RNZipArchive.m
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,22 @@ -(void)stopObserving {
}
}


RCT_EXPORT_METHOD(getUncompressedSize:(NSString *)path
charset:(NSString *)charset
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) {
NSError *error = nil;
NSNumber *wantedFileSize = [SSZipArchive payloadSizeForArchiveAtPath:path error:&error];

if (error == nil) {
resolve(wantedFileSize);
} else {
// reject(@"get_uncompressed_size_error", [error localizedDescription], error);
resolve(@-1);
}
}

- (dispatch_queue_t)methodQueue {
return dispatch_queue_create("com.mockingbot.ReactNative.ZipArchiveQueue", DISPATCH_QUEUE_SERIAL);
}
Expand Down

0 comments on commit 298b0c7

Please sign in to comment.