From ac6103979885f99456fafddf845bf8f4481f4bac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=92=8C=E6=AC=A3?= Date: Sat, 10 Aug 2019 23:14:56 +0800 Subject: [PATCH 1/2] feat: for 'unzip' method, add charset argument --- .../java/com/rnziparchive/RNZipArchiveModule.java | 12 +++++++----- index.d.ts | 2 +- index.js | 6 +++--- ios/RNZipArchive.m | 1 + 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/android/src/main/java/com/rnziparchive/RNZipArchiveModule.java b/android/src/main/java/com/rnziparchive/RNZipArchiveModule.java index 875c37c..3fba6c4 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..fb602bb 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..09577de 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) => { @@ -39,6 +39,6 @@ export const unzipAssets = (source, target) => { export const subscribe = callback => { const emitter = - Platform.OS === 'ios' ? NativeAppEventEmitter : DeviceEventEmitter + Platform.OS === 'ios' ? NativeAppEventEmitter : DeviceEventEmitter return emitter.addListener('zipArchiveProgressEvent', callback) } 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) { From 2b6ba8f706000c6ef8ffd2b7674f020b3733e755 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=92=8C=E6=AC=A3?= Date: Sun, 11 Aug 2019 16:53:08 +0800 Subject: [PATCH 2/2] fix:reformat code and update README.md --- README.md | 5 ++++- .../main/java/com/rnziparchive/RNZipArchiveModule.java | 10 +++++----- index.d.ts | 2 +- index.js | 2 +- 4 files changed, 11 insertions(+), 8 deletions(-) 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 3fba6c4..0ec449d 100644 --- a/android/src/main/java/com/rnziparchive/RNZipArchiveModule.java +++ b/android/src/main/java/com/rnziparchive/RNZipArchiveModule.java @@ -102,7 +102,7 @@ public void run() { } @ReactMethod - public void unzip(final String zipFilePath, final String destDirectory,final String charset, 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() { @@ -125,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,charset); + final long totalUncompressedBytes = getUncompressedSize(zipFilePath, charset); File destDir = new File(destDirectory); if (!destDir.exists()) { @@ -140,7 +140,7 @@ public void run() { final long[] extractedBytes = {0}; final int[] lastPercentage = {0}; - final ZipFile zipFile = new ZipFile(zipFilePath,Charset.forName(charset)); + 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()) { @@ -459,10 +459,10 @@ protected void updateProgress(long extractedBytes, long totalSize, String zipFil * * @return -1 on failure */ - private long getUncompressedSize(String zipFilePath,String charset) { + private long getUncompressedSize(String zipFilePath, String charset) { long totalSize = 0; try { - ZipFile zipFile = new ZipFile(zipFilePath,Charset.forName(charset)); + 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 fb602bb..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,charset?: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 09577de..694439b 100644 --- a/index.js +++ b/index.js @@ -39,6 +39,6 @@ export const unzipAssets = (source, target) => { export const subscribe = callback => { const emitter = - Platform.OS === 'ios' ? NativeAppEventEmitter : DeviceEventEmitter + Platform.OS === 'ios' ? NativeAppEventEmitter : DeviceEventEmitter return emitter.addListener('zipArchiveProgressEvent', callback) }