diff --git a/README.md b/README.md index 38905ad..bea74d7 100644 --- a/README.md +++ b/README.md @@ -195,8 +195,11 @@ Example ```js const sourcePath = `${DocumentDirectoryPath}/myFile.zip` const targetPath = DocumentDirectoryPath +const charset = 'UTF-8' +// charset possible values: UTF-8, GBK, US-ASCII and so on. If none was passed, default value is UTF-8 -unzip(sourcePath, targetPath) + +unzip(sourcePath, targetPath, charset) .then((path) => { console.log(`unzip completed at ${path}`) }) diff --git a/android/src/main/java/com/rnziparchive/RNZipArchiveModule.java b/android/src/main/java/com/rnziparchive/RNZipArchiveModule.java index 875c37c..0ec449d 100644 --- a/android/src/main/java/com/rnziparchive/RNZipArchiveModule.java +++ b/android/src/main/java/com/rnziparchive/RNZipArchiveModule.java @@ -33,6 +33,8 @@ import net.lingala.zip4j.model.ZipParameters; import net.lingala.zip4j.util.Zip4jConstants; +import java.nio.charset.Charset; + public class RNZipArchiveModule extends ReactContextBaseJavaModule { private static final String TAG = RNZipArchiveModule.class.getSimpleName(); @@ -100,7 +102,7 @@ public void run() { } @ReactMethod - public void unzip(final String zipFilePath, final String destDirectory, final Promise promise) { + public void unzip(final String zipFilePath, final String destDirectory, final String charset, final Promise promise) { new Thread(new Runnable() { @Override public void run() { @@ -123,7 +125,7 @@ public void run() { try { // Find the total uncompressed size of every file in the zip, so we can // get an accurate progress measurement - final long totalUncompressedBytes = getUncompressedSize(zipFilePath); + final long totalUncompressedBytes = getUncompressedSize(zipFilePath, charset); File destDir = new File(destDirectory); if (!destDir.exists()) { @@ -138,7 +140,7 @@ public void run() { final long[] extractedBytes = {0}; final int[] lastPercentage = {0}; - final ZipFile zipFile = new ZipFile(zipFilePath); + final ZipFile zipFile = new ZipFile(zipFilePath, Charset.forName(charset)); final Enumeration entries = zipFile.entries(); Log.d(TAG, "Zip has " + zipFile.size() + " entries"); while (entries.hasMoreElements()) { @@ -457,10 +459,10 @@ protected void updateProgress(long extractedBytes, long totalSize, String zipFil * * @return -1 on failure */ - private long getUncompressedSize(String zipFilePath) { + private long getUncompressedSize(String zipFilePath, String charset) { long totalSize = 0; try { - ZipFile zipFile = new ZipFile(zipFilePath); + ZipFile zipFile = new ZipFile(zipFilePath, Charset.forName(charset)); Enumeration entries = zipFile.entries(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); diff --git a/index.d.ts b/index.d.ts index 68ee681..b823305 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,7 +1,7 @@ declare module 'react-native-zip-archive' { import { NativeEventSubscription } from 'react-native'; export function zip(source: string, target: string): Promise; - export function unzip(source: string, target: string): Promise; + export function unzip(source: string, target: string, charset?: string): Promise; export function unzipWithPassword(assetPath: string, target: string, passowrd: string): Promise; export function unzipAssets(assetPath: string, target: string): Promise; export function subscribe(callback: ({ progress: number, filePath: string })): NativeEventSubscription; diff --git a/index.js b/index.js index b1c83a6..694439b 100644 --- a/index.js +++ b/index.js @@ -9,8 +9,8 @@ const { const RNZipArchive = NativeModules.RNZipArchive -export const unzip = (source, target) => { - return RNZipArchive.unzip(source, target) +export const unzip = (source, target, charset = 'UTF-8') => { + return RNZipArchive.unzip(source, target, charset) } export const unzipWithPassword = (source, target, password) => { diff --git a/ios/RNZipArchive.m b/ios/RNZipArchive.m index 4b72bc7..57b0c6d 100644 --- a/ios/RNZipArchive.m +++ b/ios/RNZipArchive.m @@ -31,6 +31,7 @@ @implementation RNZipArchive RCT_EXPORT_METHOD(unzip:(NSString *)from destinationPath:(NSString *)destinationPath + charset:(NSString *)charset resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {