From 6da4788d632738af32da3d045ff698e6d31a3eb7 Mon Sep 17 00:00:00 2001 From: Perry Poon Date: Sun, 3 Mar 2019 12:21:41 +0800 Subject: [PATCH] fix compatibility issue with unzipWithPassword --- .../com/rnziparchive/RNZipArchiveModule.java | 2 +- example/UnzipApp.js | 40 +++++++++++++------ index.js | 2 +- ios/RNZipArchive.m | 19 +++++++++ 4 files changed, 49 insertions(+), 14 deletions(-) diff --git a/android/src/main/java/com/rnziparchive/RNZipArchiveModule.java b/android/src/main/java/com/rnziparchive/RNZipArchiveModule.java index 779b9eb..e3587fa 100644 --- a/android/src/main/java/com/rnziparchive/RNZipArchiveModule.java +++ b/android/src/main/java/com/rnziparchive/RNZipArchiveModule.java @@ -61,7 +61,7 @@ public void isPasswordProtected(final String zipFilePath, final Promise promise) } @ReactMethod - public void unzip(final String zipFilePath, final String destDirectory, + public void unzipWithPassword(final String zipFilePath, final String destDirectory, final String password, final Promise promise) { new Thread(new Runnable() { @Override diff --git a/example/UnzipApp.js b/example/UnzipApp.js index db9866c..c20971b 100644 --- a/example/UnzipApp.js +++ b/example/UnzipApp.js @@ -9,8 +9,8 @@ import { } from 'react-native-custom-tabs' import AwesomeButtonRick from 'react-native-really-awesome-button/src/themes/rick' import { DocumentPicker } from 'react-native-document-picker' -import { copyFile, DocumentDirectoryPath } from 'react-native-fs' -import { subscribe, unzipWithPassword, isPasswordProtected } from 'react-native-zip-archive' +import { copyFile, DocumentDirectoryPath, unlink } from 'react-native-fs' +import { subscribe, unzip, unzipWithPassword, isPasswordProtected } from 'react-native-zip-archive' export default class App extends Component { constructor (props) { @@ -22,10 +22,10 @@ export default class App extends Component { } /** - * Dropbox to download a sample password protected html file + * box to download a sample password protected html file */ - openLink () { - let url = 'https://app.box.com/s/2szqk4fzsq7brrbcnuk6z2tdl6jq2rts' + openLink (isPasswordProtected) { + let url = isPasswordProtected ? 'https://app.box.com/s/2szqk4fzsq7brrbcnuk6z2tdl6jq2rts' : 'https://app.box.com/s/ndkn0exa9zmuh9ki7qpjgakvbkrn98q7' CustomTabs.openURL(url, { toolbarColor: '#607D8B', enableUrlBarHiding: true, @@ -40,8 +40,11 @@ export default class App extends Component { */ browseFiles () { DocumentPicker.show({ - filetype: [(Platform.OS === 'android') ? "*/*" : "public.data"] + filetype: [(Platform.OS === 'android') ? '*/*' : 'public.data'] }, (err, response) => { + if (err) { + console.error(err) + } var fileDetails = { uri: response.uri, name: response.fileName, @@ -80,23 +83,25 @@ export default class App extends Component { var filename = fileDetails.name var filePath = DocumentDirectoryPath + '/' + filename var unzipPath = DocumentDirectoryPath - copyFile(fileDetails.uri, filePath).catch((err) => { + unlink(filePath).catch(err => { console.log(err) return Promise.resolve() + }).then(() => { + return copyFile(fileDetails.uri, filePath) }).then(() => { return isPasswordProtected(filePath) }).then((isEncrypted) => { if (isEncrypted) { return unzipWithPassword(filePath, unzipPath, password) } else { - throw 'Not password protected!' + return unzip(filePath, unzipPath) } }).then((response) => { console.log('Successfully unzipped files') console.log(response) this.setState({ ...this.state, - uri: `file://${unzipPath}/static_password/index.html` + uri: `file://${filePath.split('.').slice(0, -1).join('.')}/index.html` }) }).catch((err) => { console.log(err) @@ -124,10 +129,21 @@ export default class App extends Component { this.openLink(true)}> + Download Sample Zip file with password + + + + + this.openLink()}> - Download Sample Zip file + onPress = {() => this.openLink(false)}> + Download Sample Zip file without password diff --git a/index.js b/index.js index 7aecfbb..e4ebfa5 100644 --- a/index.js +++ b/index.js @@ -14,7 +14,7 @@ export const unzip = (source, target) => { } export const unzipWithPassword = (source, target, password) => { - return RNZipArchive.unzip(source, target, password) + return RNZipArchive.unzipWithPassword(source, target, password) } export const isPasswordProtected = (source) => { diff --git a/ios/RNZipArchive.m b/ios/RNZipArchive.m index 57e6f9c..e702d3a 100644 --- a/ios/RNZipArchive.m +++ b/ios/RNZipArchive.m @@ -30,6 +30,25 @@ @implementation RNZipArchive } RCT_EXPORT_METHOD(unzip:(NSString *)from + destinationPath:(NSString *)destinationPath + resolver:(RCTPromiseResolveBlock)resolve + rejecter:(RCTPromiseRejectBlock)reject) { + + [self zipArchiveProgressEvent:0 total:1 filePath:from]; // force 0% + + BOOL success = [RNZASSZipArchive unzipFileAtPath:from toDestination:destinationPath overwrite:YES password:nil error:nil delegate:self]; + + [self zipArchiveProgressEvent:1 total:1 filePath:from]; // force 100% + + if (success) { + resolve(destinationPath); + } else { + NSError *error = nil; + reject(@"unzip_error", @"unable to unzip", error); + } +} + +RCT_EXPORT_METHOD(unzipWithPassword:(NSString *)from destinationPath:(NSString *)destinationPath password:(NSString *)password resolver:(RCTPromiseResolveBlock)resolve