Skip to content

Commit

Permalink
support unzip password encrypted file on iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
plrthink committed Mar 2, 2019
1 parent 070c1a7 commit fb0184a
Show file tree
Hide file tree
Showing 11 changed files with 394 additions and 759 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,26 @@ unzip(sourcePath, targetPath)
})
```

**unzipWithPassword(source: string, target: string, passowrd: string): Promise**

> unzip from source to target
Example

```js
const sourcePath = `${DocumentDirectoryPath}/myFile.zip`
const targetPath = DocumentDirectoryPath
const password = 'password'

unzipWithPassword(sourcePath, targetPath, password)
.then((path) => {
console.log(`unzip completed at ${path}`)
})
.catch((error) => {
console.log(error)
})
```

**unzipAssets(assetPath: string, target: string): Promise**

> unzip file from Android `assets` folder to target path
Expand Down
16 changes: 10 additions & 6 deletions example/UnzipApp.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import React, { Component } from 'react'
import {
StyleSheet, View,
Alert, WebView
Alert, WebView, Platform
} from 'react-native'
import {
ANIMATIONS_SLIDE,
CustomTabs
} from 'react-native-custom-tabs'
import AwesomeButtonRick from 'react-native-really-awesome-button/src/themes/rick'
import { DocumentPicker, DocumentPickerUtil } from 'react-native-document-picker'
import { DocumentPicker } from 'react-native-document-picker'
import { copyFile, DocumentDirectoryPath } from 'react-native-fs'
import { subscribe, unzipWithPassword, isPasswordProtected } from 'react-native-zip-archive'

Expand Down Expand Up @@ -40,7 +40,7 @@ export default class App extends Component {
*/
browseFiles () {
DocumentPicker.show({
filetype: [DocumentPickerUtil.allFiles()]
filetype: [(Platform.OS === 'android') ? "*/*" : "public.data"]
}, (err, response) => {
var fileDetails = {
uri: response.uri,
Expand All @@ -56,15 +56,15 @@ export default class App extends Component {
}

componentDidMount () {
this.zipProgress = subscribe(({ progress, filePath }) => {
this.unzipProgress = subscribe(({ progress, filePath }) => {
console.log('---- Progress update ----')
console.log(filePath + ': ' + progress)
console.log('-------------------------')
})
}

componentWillUnmount () {
this.zipProgress.remove()
this.unzipProgress.remove()
}

extractFile () {
Expand All @@ -80,7 +80,10 @@ export default class App extends Component {
var filename = fileDetails.name
var filePath = DocumentDirectoryPath + '/' + filename
var unzipPath = DocumentDirectoryPath
copyFile(fileDetails.uri, filePath).then(() => {
copyFile(fileDetails.uri, filePath).catch((err) => {
console.log(err)
return Promise.resolve()
}).then(() => {
return isPasswordProtected(filePath)
}).then((isEncrypted) => {
if (isEncrypted) {
Expand Down Expand Up @@ -108,6 +111,7 @@ export default class App extends Component {
<WebView
source={{uri: uri}}
style={styles.webView}
originWhitelist={['http://*', 'https://*', 'file://*']}
startInLoadingState={true}
scalesPageToFit={true}
allowFileAccess={true}
Expand Down
788 changes: 225 additions & 563 deletions example/ios/example.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
{
"images" : [
{
"idiom" : "iphone",
"size" : "20x20",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "20x20",
"scale" : "3x"
},
{
"idiom" : "iphone",
"size" : "29x29",
Expand Down Expand Up @@ -29,6 +39,11 @@
"idiom" : "iphone",
"size" : "60x60",
"scale" : "3x"
},
{
"idiom" : "ios-marketing",
"size" : "1024x1024",
"scale" : "1x"
}
],
"info" : {
Expand Down
29 changes: 13 additions & 16 deletions example/ios/example/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@
<string>1</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
<key>NSExceptionDomains</key>
<dict>
<key>localhost</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
</dict>
</dict>
<key>NSLocationWhenInUseUsageDescription</key>
<string></string>
<key>UILaunchStoryboardName</key>
Expand All @@ -40,21 +53,5 @@
</array>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
<key>NSLocationWhenInUseUsageDescription</key>
<string></string>
<key>NSAppTransportSecurity</key>
<!--See http://ste.vn/2015/06/10/configuring-app-transport-security-ios-9-osx-10-11/ -->
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
<key>NSExceptionDomains</key>
<dict>
<key>localhost</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
</dict>
</dict>
</dict>
</plist>
20 changes: 20 additions & 0 deletions example/ios/example/example.entitlements
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.developer.icloud-container-identifiers</key>
<array>
<string>iCloud.$(CFBundleIdentifier)</string>
</array>
<key>com.apple.developer.icloud-services</key>
<array>
<string>CloudDocuments</string>
</array>
<key>com.apple.developer.ubiquity-container-identifiers</key>
<array>
<string>iCloud.$(CFBundleIdentifier)</string>
</array>
<key>com.apple.developer.ubiquity-kvstore-identifier</key>
<string>$(TeamIdentifierPrefix)$(CFBundleIdentifier)</string>
</dict>
</plist>
24 changes: 0 additions & 24 deletions example/ios/exampleTests/Info.plist

This file was deleted.

68 changes: 0 additions & 68 deletions example/ios/exampleTests/exampleTests.m

This file was deleted.

2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const unzipWithPassword = (source, target, password) => {
}

export const isPasswordProtected = (source) => {
return RNZipArchive.isPasswordProtected(source)
return RNZipArchive.isPasswordProtected(source).then(isEncrypted => !!isEncrypted)
}

export const zip = (source, target) => {
Expand Down
17 changes: 13 additions & 4 deletions ios/RNZipArchive.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,23 @@ @implementation RNZipArchive

RCT_EXPORT_MODULE();

RCT_EXPORT_METHOD(isPasswordProtected:(NSString *)file
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) {

BOOL isPasswordProtected = [RNZASSZipArchive isFilePasswordProtectedAtPath:file];
resolve([NSNumber numberWithBool:isPasswordProtected]);
}

RCT_EXPORT_METHOD(unzip:(NSString *)from
destinationPath:(NSString *)destinationPath
password:(NSString *)password
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) {

[self zipArchiveProgressEvent:0 total:1 filePath:from]; // force 0%

BOOL success = [RNZASSZipArchive unzipFileAtPath:from toDestination:destinationPath delegate:self];
BOOL success = [RNZASSZipArchive unzipFileAtPath:from toDestination:destinationPath overwrite:YES password:password error:nil delegate:self];

[self zipArchiveProgressEvent:1 total:1 filePath:from]; // force 100%

Expand Down Expand Up @@ -73,9 +82,9 @@ - (dispatch_queue_t)methodQueue {

- (void)zipArchiveProgressEvent:(unsigned long long)loaded total:(unsigned long long)total filePath:(NSString *)filePath {
[self.bridge.eventDispatcher sendAppEventWithName:@"zipArchiveProgressEvent" body:@{
@"progress": @((float)loaded / (float)total),
@"filePath": filePath
}];
@"progress": @((float)loaded / (float)total),
@"filePath": filePath
}];
}

@end
Loading

0 comments on commit fb0184a

Please sign in to comment.