Skip to content

Commit b315174

Browse files
authored
Merge pull request #143 from mockingbot/ios-zip-with-password
can zip with password on iOS, but encryption method option is ignored.
2 parents f06b5dc + 50a4663 commit b315174

File tree

7 files changed

+103
-26
lines changed

7 files changed

+103
-26
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ react-native link react-native-zip-archive
2626
#### iOS
2727

2828
refer to the [official guide](https://facebook.github.io/react-native/docs/linking-libraries-ios.html),
29-
remember to also link `libz` to the project.
29+
remember to also link `libz`, `libiconv` and `Security` to your target.(You can follow the tutorial on [this](http://docs.onemobilesdk.aol.com/ios-ad-sdk/adding-frameworks-xcode.html))
3030

3131
#### Android
3232

@@ -167,6 +167,8 @@ zip(sourcePath, targetPath)
167167

168168
> zip source to target
169169
170+
*NOTE: encryptionType is not supported on iOS yet, so it would be igonred on that platform.*
171+
170172
Example
171173

172174
```js

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -343,10 +343,14 @@ public void zip(String fileOrDirectory, String destDirectory, Promise promise) {
343343

344344
@ReactMethod
345345
public void zipWithPassword(final String fileOrDirectory, final String destDirectory, final String password,
346-
final String encyptionMethod, final Promise promise) {
346+
final String encryptionMethod, final Promise promise) {
347347
new Thread(new Runnable() {
348348
@Override
349349
public void run() {
350+
String _encryptionMethod = encryptionMethod;
351+
if (_encryptionMethod == null) {
352+
_encryptionMethod = "STANDARD";
353+
}
350354

351355
try {
352356
net.lingala.zip4j.core.ZipFile zipFile = new net.lingala.zip4j.core.ZipFile(destDirectory);
@@ -355,7 +359,7 @@ public void run() {
355359
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
356360
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
357361

358-
String encParts[] = encyptionMethod.split("-");
362+
String encParts[] = _encryptionMethod.split("-");
359363

360364
if (password != null && !password.isEmpty()) {
361365
parameters.setEncryptFiles(true);
@@ -368,7 +372,7 @@ public void run() {
368372
} else {
369373
parameters.setAesKeyStrength(Zip4jConstants.ENC_METHOD_STANDARD);
370374
}
371-
} else if (encyptionMethod.equals("STANDARD")) {
375+
} else if (_encryptionMethod.equals("STANDARD")) {
372376
parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD);
373377
Log.d(TAG, "Standard Encryption");
374378
} else {

example/App.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ import {
1010
StyleSheet,
1111
View
1212
} from 'react-native'
13-
import UnzipApp from './UnzipApp'
13+
import Example from './Example'
1414

15-
export default class Example extends Component {
15+
export default class App extends Component {
1616
render () {
1717
return (
1818
<View style={styles.container}>
19-
<UnzipApp />
19+
<Example />
2020
</View>
2121
)
2222
}
@@ -29,4 +29,4 @@ const styles = StyleSheet.create({
2929
}
3030
})
3131

32-
AppRegistry.registerComponent('example', () => Example)
32+
AppRegistry.registerComponent('app', () => App)
Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
import AwesomeButtonRick from 'react-native-really-awesome-button/src/themes/rick'
1111
import { DocumentPicker } from 'react-native-document-picker'
1212
import { copyFile, DocumentDirectoryPath, unlink } from 'react-native-fs'
13-
import { subscribe, unzip, unzipWithPassword, isPasswordProtected } from 'react-native-zip-archive'
13+
import { subscribe, unzip, unzipWithPassword, isPasswordProtected, zipWithPassword } from 'react-native-zip-archive'
1414

1515
export default class App extends Component {
1616
constructor (props) {
@@ -25,7 +25,9 @@ export default class App extends Component {
2525
* box to download a sample password protected html file
2626
*/
2727
openLink (isPasswordProtected) {
28-
let url = isPasswordProtected ? 'https://app.box.com/s/2szqk4fzsq7brrbcnuk6z2tdl6jq2rts' : 'https://app.box.com/s/ndkn0exa9zmuh9ki7qpjgakvbkrn98q7'
28+
const url = isPasswordProtected
29+
? 'https://app.box.com/s/2szqk4fzsq7brrbcnuk6z2tdl6jq2rts'
30+
: 'https://app.box.com/s/ndkn0exa9zmuh9ki7qpjgakvbkrn98q7'
2931
CustomTabs.openURL(url, {
3032
toolbarColor: '#607D8B',
3133
enableUrlBarHiding: true,
@@ -45,7 +47,7 @@ export default class App extends Component {
4547
if (err) {
4648
console.error(err)
4749
}
48-
var fileDetails = {
50+
const fileDetails = {
4951
uri: response.uri,
5052
name: response.fileName,
5153
size: response.fileSize,
@@ -71,19 +73,19 @@ export default class App extends Component {
7173
}
7274

7375
extractFile () {
74-
var fileDetails = this.state.fileDetails
76+
const fileDetails = this.state.fileDetails
7577
if (Object.keys(fileDetails).length === 0) {
7678
Alert.alert('No file selected!')
7779
return
7880
}
7981

8082
// password for the dropbox sample zip file
81-
var password = 'helloworld'
83+
const password = 'helloworld'
8284

83-
var filename = fileDetails.name
84-
var filePath = DocumentDirectoryPath + '/' + filename
85-
var unzipPath = DocumentDirectoryPath
86-
unlink(filePath).catch(err => {
85+
const filename = fileDetails.name
86+
const filePath = DocumentDirectoryPath + '/' + filename
87+
const unzipPath = DocumentDirectoryPath
88+
return unlink(filePath).catch(err => {
8789
console.log(err)
8890
return Promise.resolve()
8991
}).then(() => {
@@ -99,15 +101,44 @@ export default class App extends Component {
99101
}).then((response) => {
100102
console.log('Successfully unzipped files')
101103
console.log(response)
102-
this.setState({
103-
...this.state,
104-
uri: `file://${filePath.split('.').slice(0, -1).join('.')}/index.html`
104+
return filePath
105+
}).catch((err) => {
106+
console.log(err)
107+
})
108+
}
109+
110+
archiveFolder () {
111+
this.extractFile().then((filePath) => {
112+
return unlink(filePath).then(() => {
113+
// password for the dropbox sample zip file
114+
const password = 'helloworld'
115+
116+
return zipWithPassword(
117+
filePath.split('.').slice(0, -1).join('.'),
118+
filePath,
119+
password
120+
).then((response) => {
121+
console.log('Successfully archived the folder')
122+
console.log(response)
123+
}).catch((err) => {
124+
console.log(err)
125+
})
126+
}).catch((err) => {
127+
console.log(err)
105128
})
106129
}).catch((err) => {
107130
console.log(err)
108131
})
109132
}
110133

134+
extractFileAndShow () {
135+
this.extractFile().then(filePath => {
136+
this.setState({
137+
uri: `file://${filePath.split('.').slice(0, -1).join('.')}/index.html`
138+
})
139+
})
140+
}
141+
111142
render () {
112143
const { uri } = this.state
113144
if (uri !== '') {
@@ -163,10 +194,21 @@ export default class App extends Component {
163194
width={300}
164195
borderRadius={8}
165196
textSize={22}
166-
onPress = {() => this.extractFile()}
197+
onPress = {() => this.extractFileAndShow()}
167198
type="anchor">
168199
Extract
169200
</AwesomeButtonRick>
201+
202+
<View style = {styles.lineStyle} />
203+
204+
<AwesomeButtonRick
205+
width={300}
206+
borderRadius={8}
207+
textSize={22}
208+
onPress = {() => this.archiveFolder()}
209+
type="anchor">
210+
Archive
211+
</AwesomeButtonRick>
170212
</View>
171213

172214
)

example/scripts/postinstall.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
const cp = require('child_process');
2-
cp.execSync(`rm -rf node_modules/react-native-zip-archive/node_modules`);
3-
cp.execSync(`rm -rf node_modules/react-native-zip-archive/example `);
1+
const cp = require('child_process')
2+
cp.execSync(`rm -rf node_modules/react-native-zip-archive/node_modules`)
3+
cp.execSync(`rm -rf node_modules/react-native-zip-archive/example `)

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ export const unzipWithPassword = (source, target, password) => {
1717
return RNZipArchive.unzipWithPassword(source, target, password)
1818
}
1919

20-
export const zipWithPassword = (source, target, password, encyptionMethod) => {
21-
return RNZipArchive.zipWithPassword(source, target, password, encyptionMethod)
20+
export const zipWithPassword = (source, target, password, encryptionMethod) => {
21+
return RNZipArchive.zipWithPassword(source, target, password, encryptionMethod)
2222
}
2323

2424
export const isPasswordProtected = (source) => {

ios/RNZipArchive.m

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,35 @@ @implementation RNZipArchive
9595
}
9696
}
9797

98+
99+
RCT_EXPORT_METHOD(zipWithPassword:(NSString *)from
100+
destinationPath:(NSString *)destinationPath
101+
password:(NSString *)password
102+
encryptionType:(NSString *)encryptionType
103+
resolver:(RCTPromiseResolveBlock)resolve
104+
rejecter:(RCTPromiseRejectBlock)reject) {
105+
[self zipArchiveProgressEvent:0 total:1 filePath:destinationPath]; // force 0%
106+
107+
NSFileManager *fileManager = [[NSFileManager alloc] init];
108+
BOOL isDir;
109+
BOOL success;
110+
[fileManager fileExistsAtPath:from isDirectory:&isDir];
111+
if (isDir) {
112+
success = [SSZipArchive createZipFileAtPath:destinationPath withContentsOfDirectory:from withPassword:password];
113+
} else {
114+
success = [SSZipArchive createZipFileAtPath:destinationPath withFilesAtPaths:@[from] withPassword:password];
115+
}
116+
117+
[self zipArchiveProgressEvent:1 total:1 filePath:destinationPath]; // force 100%
118+
119+
if (success) {
120+
resolve(destinationPath);
121+
} else {
122+
NSError *error = nil;
123+
reject(@"zip_error", @"unable to zip", error);
124+
}
125+
}
126+
98127
- (dispatch_queue_t)methodQueue {
99128
return dispatch_queue_create("com.mockingbot.ReactNative.ZipArchiveQueue", DISPATCH_QUEUE_SERIAL);
100129
}

0 commit comments

Comments
 (0)