Skip to content

Commit 50016ac

Browse files
authored
Merge pull request #159 from ImHeXin/master
feat: for 'unzip' method, add charset argument
2 parents e873ad5 + 2b6ba8f commit 50016ac

File tree

5 files changed

+15
-9
lines changed

5 files changed

+15
-9
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,11 @@ Example
195195
```js
196196
const sourcePath = `${DocumentDirectoryPath}/myFile.zip`
197197
const targetPath = DocumentDirectoryPath
198+
const charset = 'UTF-8'
199+
// charset possible values: UTF-8, GBK, US-ASCII and so on. If none was passed, default value is UTF-8
198200

199-
unzip(sourcePath, targetPath)
201+
202+
unzip(sourcePath, targetPath, charset)
200203
.then((path) => {
201204
console.log(`unzip completed at ${path}`)
202205
})

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: 2 additions & 2 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) => {

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)