Skip to content

Commit ac61039

Browse files
author
和欣
committed
feat: for 'unzip' method, add charset argument
1 parent e873ad5 commit ac61039

File tree

4 files changed

+12
-9
lines changed

4 files changed

+12
-9
lines changed

android/src/main/java/com/rnziparchive/RNZipArchiveModule.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
import net.lingala.zip4j.model.ZipParameters;
3434
import net.lingala.zip4j.util.Zip4jConstants;
3535

36+
import java.nio.charset.Charset;
37+
3638
public class RNZipArchiveModule extends ReactContextBaseJavaModule {
3739
private static final String TAG = RNZipArchiveModule.class.getSimpleName();
3840

@@ -100,7 +102,7 @@ public void run() {
100102
}
101103

102104
@ReactMethod
103-
public void unzip(final String zipFilePath, final String destDirectory, final Promise promise) {
105+
public void unzip(final String zipFilePath, final String destDirectory,final String charset, final Promise promise) {
104106
new Thread(new Runnable() {
105107
@Override
106108
public void run() {
@@ -123,7 +125,7 @@ public void run() {
123125
try {
124126
// Find the total uncompressed size of every file in the zip, so we can
125127
// get an accurate progress measurement
126-
final long totalUncompressedBytes = getUncompressedSize(zipFilePath);
128+
final long totalUncompressedBytes = getUncompressedSize(zipFilePath,charset);
127129

128130
File destDir = new File(destDirectory);
129131
if (!destDir.exists()) {
@@ -138,7 +140,7 @@ public void run() {
138140
final long[] extractedBytes = {0};
139141
final int[] lastPercentage = {0};
140142

141-
final ZipFile zipFile = new ZipFile(zipFilePath);
143+
final ZipFile zipFile = new ZipFile(zipFilePath,Charset.forName(charset));
142144
final Enumeration<? extends ZipEntry> entries = zipFile.entries();
143145
Log.d(TAG, "Zip has " + zipFile.size() + " entries");
144146
while (entries.hasMoreElements()) {
@@ -457,10 +459,10 @@ protected void updateProgress(long extractedBytes, long totalSize, String zipFil
457459
*
458460
* @return -1 on failure
459461
*/
460-
private long getUncompressedSize(String zipFilePath) {
462+
private long getUncompressedSize(String zipFilePath,String charset) {
461463
long totalSize = 0;
462464
try {
463-
ZipFile zipFile = new ZipFile(zipFilePath);
465+
ZipFile zipFile = new ZipFile(zipFilePath,Charset.forName(charset));
464466
Enumeration<? extends ZipEntry> entries = zipFile.entries();
465467
while (entries.hasMoreElements()) {
466468
ZipEntry entry = entries.nextElement();

index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
declare module 'react-native-zip-archive' {
22
import { NativeEventSubscription } from 'react-native';
33
export function zip(source: string, target: string): Promise<string>;
4-
export function unzip(source: string, target: string): Promise<string>;
4+
export function unzip(source: string, target: string,charset?:string): Promise<string>;
55
export function unzipWithPassword(assetPath: string, target: string, passowrd: string): Promise<string>;
66
export function unzipAssets(assetPath: string, target: string): Promise<string>;
77
export function subscribe(callback: ({ progress: number, filePath: string })): NativeEventSubscription;

index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ const {
99

1010
const RNZipArchive = NativeModules.RNZipArchive
1111

12-
export const unzip = (source, target) => {
13-
return RNZipArchive.unzip(source, target)
12+
export const unzip = (source, target, charset = 'UTF-8') => {
13+
return RNZipArchive.unzip(source, target, charset)
1414
}
1515

1616
export const unzipWithPassword = (source, target, password) => {
@@ -39,6 +39,6 @@ export const unzipAssets = (source, target) => {
3939

4040
export const subscribe = callback => {
4141
const emitter =
42-
Platform.OS === 'ios' ? NativeAppEventEmitter : DeviceEventEmitter
42+
Platform.OS === 'ios' ? NativeAppEventEmitter : DeviceEventEmitter
4343
return emitter.addListener('zipArchiveProgressEvent', callback)
4444
}

ios/RNZipArchive.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ @implementation RNZipArchive
3131

3232
RCT_EXPORT_METHOD(unzip:(NSString *)from
3333
destinationPath:(NSString *)destinationPath
34+
charset:(NSString *)charset
3435
resolver:(RCTPromiseResolveBlock)resolve
3536
rejecter:(RCTPromiseRejectBlock)reject) {
3637

0 commit comments

Comments
 (0)