Skip to content

Commit 6da4788

Browse files
committed
fix compatibility issue with unzipWithPassword
1 parent fb0184a commit 6da4788

File tree

4 files changed

+49
-14
lines changed

4 files changed

+49
-14
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public void isPasswordProtected(final String zipFilePath, final Promise promise)
6161
}
6262

6363
@ReactMethod
64-
public void unzip(final String zipFilePath, final String destDirectory,
64+
public void unzipWithPassword(final String zipFilePath, final String destDirectory,
6565
final String password, final Promise promise) {
6666
new Thread(new Runnable() {
6767
@Override

example/UnzipApp.js

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import {
99
} from 'react-native-custom-tabs'
1010
import AwesomeButtonRick from 'react-native-really-awesome-button/src/themes/rick'
1111
import { DocumentPicker } from 'react-native-document-picker'
12-
import { copyFile, DocumentDirectoryPath } from 'react-native-fs'
13-
import { subscribe, unzipWithPassword, isPasswordProtected } from 'react-native-zip-archive'
12+
import { copyFile, DocumentDirectoryPath, unlink } from 'react-native-fs'
13+
import { subscribe, unzip, unzipWithPassword, isPasswordProtected } from 'react-native-zip-archive'
1414

1515
export default class App extends Component {
1616
constructor (props) {
@@ -22,10 +22,10 @@ export default class App extends Component {
2222
}
2323

2424
/**
25-
* Dropbox to download a sample password protected html file
25+
* box to download a sample password protected html file
2626
*/
27-
openLink () {
28-
let url = 'https://app.box.com/s/2szqk4fzsq7brrbcnuk6z2tdl6jq2rts'
27+
openLink (isPasswordProtected) {
28+
let url = isPasswordProtected ? 'https://app.box.com/s/2szqk4fzsq7brrbcnuk6z2tdl6jq2rts' : 'https://app.box.com/s/ndkn0exa9zmuh9ki7qpjgakvbkrn98q7'
2929
CustomTabs.openURL(url, {
3030
toolbarColor: '#607D8B',
3131
enableUrlBarHiding: true,
@@ -40,8 +40,11 @@ export default class App extends Component {
4040
*/
4141
browseFiles () {
4242
DocumentPicker.show({
43-
filetype: [(Platform.OS === 'android') ? "*/*" : "public.data"]
43+
filetype: [(Platform.OS === 'android') ? '*/*' : 'public.data']
4444
}, (err, response) => {
45+
if (err) {
46+
console.error(err)
47+
}
4548
var fileDetails = {
4649
uri: response.uri,
4750
name: response.fileName,
@@ -80,23 +83,25 @@ export default class App extends Component {
8083
var filename = fileDetails.name
8184
var filePath = DocumentDirectoryPath + '/' + filename
8285
var unzipPath = DocumentDirectoryPath
83-
copyFile(fileDetails.uri, filePath).catch((err) => {
86+
unlink(filePath).catch(err => {
8487
console.log(err)
8588
return Promise.resolve()
89+
}).then(() => {
90+
return copyFile(fileDetails.uri, filePath)
8691
}).then(() => {
8792
return isPasswordProtected(filePath)
8893
}).then((isEncrypted) => {
8994
if (isEncrypted) {
9095
return unzipWithPassword(filePath, unzipPath, password)
9196
} else {
92-
throw 'Not password protected!'
97+
return unzip(filePath, unzipPath)
9398
}
9499
}).then((response) => {
95100
console.log('Successfully unzipped files')
96101
console.log(response)
97102
this.setState({
98103
...this.state,
99-
uri: `file://${unzipPath}/static_password/index.html`
104+
uri: `file://${filePath.split('.').slice(0, -1).join('.')}/index.html`
100105
})
101106
}).catch((err) => {
102107
console.log(err)
@@ -124,10 +129,21 @@ export default class App extends Component {
124129
<AwesomeButtonRick
125130
borderRadius={8}
126131
width={300}
127-
textSize={22}
132+
textSize={14}
133+
type="primary"
134+
onPress = {() => this.openLink(true)}>
135+
Download Sample Zip file with password
136+
</AwesomeButtonRick>
137+
138+
<View style = {styles.lineStyle} />
139+
140+
<AwesomeButtonRick
141+
borderRadius={8}
142+
width={300}
143+
textSize={14}
128144
type="primary"
129-
onPress = {() => this.openLink()}>
130-
Download Sample Zip file
145+
onPress = {() => this.openLink(false)}>
146+
Download Sample Zip file without password
131147
</AwesomeButtonRick>
132148

133149
<View style = {styles.lineStyle} />

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const unzip = (source, target) => {
1414
}
1515

1616
export const unzipWithPassword = (source, target, password) => {
17-
return RNZipArchive.unzip(source, target, password)
17+
return RNZipArchive.unzipWithPassword(source, target, password)
1818
}
1919

2020
export const isPasswordProtected = (source) => {

ios/RNZipArchive.m

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,25 @@ @implementation RNZipArchive
3030
}
3131

3232
RCT_EXPORT_METHOD(unzip:(NSString *)from
33+
destinationPath:(NSString *)destinationPath
34+
resolver:(RCTPromiseResolveBlock)resolve
35+
rejecter:(RCTPromiseRejectBlock)reject) {
36+
37+
[self zipArchiveProgressEvent:0 total:1 filePath:from]; // force 0%
38+
39+
BOOL success = [RNZASSZipArchive unzipFileAtPath:from toDestination:destinationPath overwrite:YES password:nil error:nil delegate:self];
40+
41+
[self zipArchiveProgressEvent:1 total:1 filePath:from]; // force 100%
42+
43+
if (success) {
44+
resolve(destinationPath);
45+
} else {
46+
NSError *error = nil;
47+
reject(@"unzip_error", @"unable to unzip", error);
48+
}
49+
}
50+
51+
RCT_EXPORT_METHOD(unzipWithPassword:(NSString *)from
3352
destinationPath:(NSString *)destinationPath
3453
password:(NSString *)password
3554
resolver:(RCTPromiseResolveBlock)resolve

0 commit comments

Comments
 (0)