Skip to content

Commit 80c5d5b

Browse files
authored
Merge pull request #29 from plrthink/next
work with react-native@^0.40.0
2 parents 68e93f2 + 40ba0e4 commit 80c5d5b

File tree

91 files changed

+5719
-303
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

91 files changed

+5719
-303
lines changed

.npmignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,5 @@ project.xcworkspace
2626
#
2727
node_modules/
2828
npm-debug.log
29+
30+
example/

README.md

Lines changed: 33 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -3,104 +3,63 @@
33
Zip archive utility for react-native
44

55
> ### Important
6-
> If you're using react-native after v0.40.0, please install this package via the `next` branch. Issues and PR are welcome to make it into master and released.
6+
> If you're using react-native after 0.40.0 on iOS, be sure to use > 0.1.0 of this package.
77
88
## Installation
99

1010
```bash
1111
npm install react-native-zip-archive --save
12+
react-native link react-native-zip-archive
1213
```
1314

14-
## Getting started - iOS
15-
16-
1. In Xcode, in the project navigator right click `Libraries``Add Files to [your project's name]`
17-
2. Go to `node_modules``react-native-zip-archive` and add `RNZipArchive.xcodeproj`
18-
3. Add `libRNZipArchive.a` (from 'Products' under RNZipArchive.xcodeproj) to your project's `Build Phases``Link Binary With Libraries` phase
19-
4. Add the `libz` library to your target
20-
5. Look for Header Search Paths and make sure it contains both `$(SRCROOT)/../react-native/React` and `$(SRCROOT)/../../React` - mark both as recursive
21-
6. Run your project (`CMD+R`)
22-
23-
Warning: If you're using [rnpm](https://github.com/rnpm/rnpm) to link this module, you also need manually link `libz` library to your target otherwise your project wouldn't compile.
24-
25-
## Getting started - Android
26-
27-
* Edit `android/settings.gradle` to look like this (without the +):
28-
29-
```diff
30-
rootProject.name = 'MyApp'
31-
32-
include ':app'
33-
34-
+ include ':react-native-zip-archive'
35-
+ project(':react-native-zip-archive').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-zip-archive/android')
36-
```
37-
38-
* Edit `android/app/build.gradle` (note: **app** folder) to look like this:
39-
40-
```diff
41-
apply plugin: 'com.android.application'
42-
43-
android {
44-
...
45-
}
46-
47-
dependencies {
48-
compile fileTree(dir: 'libs', include: ['*.jar'])
49-
compile 'com.android.support:appcompat-v7:23.0.0'
50-
compile 'com.facebook.react:react-native:0.16.+'
51-
+ compile project(':react-native-zip-archive')
52-
}
53-
```
54-
55-
* Edit your `MainActivity.java` (deep in `android/app/src/main/java/...`) to look like this (note **two** places to edit):
56-
57-
```diff
58-
package com.myapp;
59-
60-
+ import com.rnziparchive.RNZipArchivePackage;
61-
62-
....
63-
64-
@Override
65-
protected List<ReactPackage> getPackages() {
66-
return Arrays.<ReactPackage>asList(
67-
new MainReactPackage(),
68-
+ new RNZipArchivePackage()
69-
);
70-
}
71-
72-
}
73-
```
74-
7515
## Usage
7616

77-
require it in your file
17+
import it into your code
7818

7919
```js
80-
const ZipArchive = require('react-native-zip-archive')
20+
import { zip, unzip, unzipAssets, subscribe } from 'react-native-zip-archive'
8121
```
8222

8323
you may also want to use something like [react-native-fs](https://github.com/johanneslumpe/react-native-fs) to access the file system (check its repo for more information)
8424

8525
```js
86-
const RNFS = require('react-native-fs')
26+
import { MainBundlePath, DocumentDirectoryPath } from 'react-native-fs'
8727
```
8828

8929
## API
9030

31+
**zip(source: string, target: string): Promise**
32+
33+
> zip source to target
34+
35+
Example
36+
37+
```js
38+
const targetPath = `${DocumentDirectoryPath}/myFile.zip`
39+
const sourcePath = DocumentDirectoryPath
40+
41+
zip(sourcePath, targetPath)
42+
.then((path) => {
43+
console.log(`unzip completed at ${path}`)
44+
})
45+
.catch((error) => {
46+
console.log(error)
47+
})
48+
```
49+
9150
**unzip(source: string, target: string): Promise**
9251

9352
> unzip from source to target
9453
9554
Example
9655

9756
```js
98-
let sourcePath = 'path_to_your_zip_file'
99-
let targetPath = RNFS.DocumentDirectoryPath
57+
const sourcePath = `${DocumentDirectoryPath}/myFile.zip`
58+
const targetPath = DocumentDirectoryPath
10059

101-
ZipArchive.unzip(sourcePath, targetPath)
102-
.then(() => {
103-
console.log('unzip completed!')
60+
unzip(sourcePath, targetPath)
61+
.then((path) => {
62+
console.log(`unzip completed at ${path}`)
10463
})
10564
.catch((error) => {
10665
console.log(error)
@@ -116,10 +75,10 @@ ZipArchive.unzip(sourcePath, targetPath)
11675
`assetPath` is the relative path to the file inside the pre-bundled assets folder, e.g. `folder/myFile.zip`. Do not pass an absolute directory.
11776

11877
```js
119-
const assetPath = 'folder/myFile.zip'
120-
const targetPath = RNFS.DocumentDirectoryPath
78+
const assetPath = `${DocumentDirectoryPath}/myFile.zip`
79+
const targetPath = DocumentDirectoryPath
12180

122-
ZipArchive.unzipAssets(assetPath, targetPath)
81+
unzipAssets(assetPath, targetPath)
12382
.then(() => {
12483
console.log('unzip completed!')
12584
})
@@ -140,7 +99,7 @@ Your callback will be passed an object with the following fields:
14099

141100
```js
142101
componentWillMount() {
143-
this.zipProgress = ZipArchive.subscribe((e) => {
102+
this.zipProgress = subscribe((e) => {
144103
this.setState({ zipProgress: e.progress })
145104
})
146105
}

RNZipArchive.podspec

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ Pod::Spec.new do |s|
1818
s.dependency 'React'
1919

2020
s.subspec 'Core' do |ss|
21-
ss.source_files = 'ios/RNZipArchive/*.{h,m}'
22-
ss.public_header_files = ['ios/RNZipArchive/RNZipArchive.h']
21+
ss.source_files = 'ios/*.{h,m}'
22+
ss.public_header_files = ['ios/RNZipArchive.h']
2323
end
2424

2525
s.subspec 'SSZipArchive' do |ss|
26-
ss.source_files = 'ios/RNZipArchive/SSZipArchive/*.{h,m}', 'ios/RNZipArchive/SSZipArchive/aes/*.{h,c}', 'ios/RNZipArchive/SSZipArchive/minizip/*.{h,c}'
27-
ss.private_header_files = 'ios/RNZipArchive/SSZipArchive/*.h', 'ios/RNZipArchive/SSZipArchive/aes/*.h', 'ios/RNZipArchive/SSZipArchive/minizip/*.h'
26+
ss.source_files = 'ios/SSZipArchive/*.{h,m}', 'ios/SSZipArchive/aes/*.{h,c}', 'ios/SSZipArchive/minizip/*.{h,c}'
27+
ss.private_header_files = 'ios/SSZipArchive/*.h', 'ios/SSZipArchive/aes/*.h', 'ios/SSZipArchive/minizip/*.h'
2828
end
2929

3030
end

android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@ repositories {
3030
}
3131

3232
dependencies {
33-
compile 'com.facebook.react:react-native:0.12.+'
33+
compile "com.facebook.react:react-native:+"
3434
}

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

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import android.util.Log;
55

66
import com.facebook.react.bridge.Arguments;
7-
import com.facebook.react.bridge.Callback;
7+
import com.facebook.react.bridge.Promise;
88
import com.facebook.react.bridge.ReactApplicationContext;
99
import com.facebook.react.bridge.ReactContextBaseJavaModule;
1010
import com.facebook.react.bridge.ReactMethod;
@@ -43,21 +43,27 @@ public String getName() {
4343
}
4444

4545
@ReactMethod
46-
public void unzip(String zipFilePath, String destDirectory, Callback callback) {
46+
public void unzip(String zipFilePath, String destDirectory, Promise promise) {
4747
FileInputStream inputStream;
4848
File file;
4949
try {
5050
inputStream = new FileInputStream(zipFilePath);
5151
file = new File(zipFilePath);
5252
} catch (FileNotFoundException | NullPointerException e) {
53-
callback.invoke(makeErrorPayload("Couldn't open file " + zipFilePath + ". ", e));
53+
promise.reject(null, "Couldn't open file " + zipFilePath + ". ");
5454
return;
5555
}
56-
unzipStream(zipFilePath, destDirectory, inputStream, file.length(), callback);
56+
try {
57+
unzipStream(zipFilePath, destDirectory, inputStream, file.length());
58+
} catch (Exception ex) {
59+
promise.reject(null, ex.getMessage());
60+
return;
61+
}
62+
promise.resolve(destDirectory);
5763
}
5864

5965
@ReactMethod
60-
public void unzipAssets(String assetsPath, String destDirectory, Callback completionCallback) {
66+
public void unzipAssets(String assetsPath, String destDirectory, Promise promise) {
6167
InputStream assetsInputStream;
6268
long size;
6369

@@ -66,15 +72,21 @@ public void unzipAssets(String assetsPath, String destDirectory, Callback comple
6672
AssetFileDescriptor fileDescriptor = getReactApplicationContext().getAssets().openFd(assetsPath);
6773
size = fileDescriptor.getLength();
6874
} catch (IOException e) {
69-
completionCallback.invoke(makeErrorPayload(String.format("Asset file `%s` could not be opened", assetsPath), e));
75+
promise.reject(null, String.format("Asset file `%s` could not be opened", assetsPath));
7076
return;
7177
}
7278

73-
unzipStream(assetsPath, destDirectory, assetsInputStream, size, completionCallback);
79+
try {
80+
unzipStream(assetsPath, destDirectory, assetsInputStream, size);
81+
} catch (Exception ex) {
82+
promise.reject(null, ex.getMessage());
83+
return;
84+
}
85+
promise.resolve(destDirectory);
7486
}
7587

7688
@ReactMethod
77-
public void zip(String fileOrDirectory, String destDirectory, Callback callback) {
89+
public void zip(String fileOrDirectory, String destDirectory, Promise promise) {
7890
List<String> filePaths = new ArrayList<>();
7991
File file;
8092
try {
@@ -92,14 +104,21 @@ public void zip(String fileOrDirectory, String destDirectory, Callback callback)
92104
throw new FileNotFoundException(fileOrDirectory);
93105
}
94106
} catch (FileNotFoundException | NullPointerException e) {
95-
callback.invoke(makeErrorPayload("Couldn't open file/directory " + fileOrDirectory + ". ", e));
107+
promise.reject(null, "Couldn't open file/directory " + fileOrDirectory + ".");
96108
return;
97109
}
98110

99-
zipStream(filePaths.toArray(new String[filePaths.size()]), destDirectory, filePaths.size(), callback);
111+
try {
112+
zipStream(filePaths.toArray(new String[filePaths.size()]), destDirectory, filePaths.size());
113+
} catch (Exception ex) {
114+
promise.reject(null, ex.getMessage());
115+
return;
116+
}
117+
118+
promise.resolve(destDirectory);
100119
}
101120

102-
private void zipStream(String[] files, String destFile, long totalSize, Callback completionCallback) {
121+
private void zipStream(String[] files, String destFile, long totalSize) throws Exception {
103122
try {
104123
if (destFile.contains("/")) {
105124
File destDir = new File(destFile.substring(0, destFile.lastIndexOf("/")));
@@ -136,11 +155,10 @@ private void zipStream(String[] files, String destFile, long totalSize, Callback
136155
}
137156
updateProgress(1, 1, destFile); // force 100%
138157
out.close();
139-
completionCallback.invoke(null, null);
140158
} catch (Exception ex) {
141159
ex.printStackTrace();
142160
updateProgress(0, 1, destFile); // force 0%
143-
completionCallback.invoke(makeErrorPayload(String.format("Couldn't zip %s", destFile), ex));
161+
throw new Exception(String.format("Couldn't zip %s", destFile));
144162
}
145163
}
146164

@@ -161,7 +179,7 @@ private List<File> getSubFiles(File baseDir, boolean isContainFolder) {
161179
return fileList;
162180
}
163181

164-
private void unzipStream(String zipFilePath, String destDirectory, InputStream inputStream, long totalSize, Callback completionCallback) {
182+
private void unzipStream(String zipFilePath, String destDirectory, InputStream inputStream, long totalSize) throws Exception {
165183
try {
166184
File destDir = new File(destDirectory);
167185
if (!destDir.exists()) {
@@ -176,17 +194,17 @@ private void unzipStream(String zipFilePath, String destDirectory, InputStream i
176194

177195
updateProgress(0, 1, zipFilePath); // force 0%
178196
File fout=null;
179-
while((entry = zipIn.getNextEntry())!=null){
180-
if(entry.isDirectory()) continue;
197+
while((entry = zipIn.getNextEntry())!=null){
198+
if(entry.isDirectory()) continue;
181199
fout=new File(destDirectory, entry.getName());
182200
if(!fout.exists()){
183-
(new File(fout.getParent())).mkdirs();
184-
}
201+
(new File(fout.getParent())).mkdirs();
202+
}
185203
FileOutputStream out=new FileOutputStream(fout);
186204
BufferedOutputStream Bout=new BufferedOutputStream(out);
187-
int b;
205+
int b;
188206
while((b=bin.read())!=-1){
189-
Bout.write(b);
207+
Bout.write(b);
190208
}
191209
Bout.close();
192210
out.close();
@@ -195,14 +213,13 @@ private void unzipStream(String zipFilePath, String destDirectory, InputStream i
195213
updateProgress(1, 1, zipFilePath); // force 100%
196214
bin.close();
197215
zipIn.close();
198-
completionCallback.invoke(null, null);
199216
} catch (Exception ex) {
200217
ex.printStackTrace();
201218
updateProgress(0, 1, zipFilePath); // force 0%
202-
completionCallback.invoke(makeErrorPayload(String.format("Couldn't extract %s", zipFilePath), ex));
219+
throw new Exception(String.format("Couldn't extract %s", zipFilePath));
203220
}
204221
}
205-
222+
206223
private void updateProgress(long extractedBytes, long totalSize, String zipFilePath) {
207224
double progress = (double) extractedBytes / (double) totalSize;
208225
Log.d(TAG, String.format("updateProgress: %.0f%%", progress * 100));
@@ -235,10 +252,4 @@ private long extractFile(ZipInputStream zipIn, String filePath) throws IOExcepti
235252

236253
return size;
237254
}
238-
239-
private WritableMap makeErrorPayload(String message, Exception ex) {
240-
WritableMap error = Arguments.createMap();
241-
error.putString("message", String.format("%s (%s)", message, ex.getMessage()));
242-
return error;
243-
}
244255
}

example/.babelrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"presets": ["react-native"]
3+
}

example/.buckconfig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
[android]
3+
target = Google Inc.:Google APIs:23
4+
5+
[maven_repositories]
6+
central = https://repo1.maven.org/maven2

0 commit comments

Comments
 (0)