Skip to content

Commit fde65bd

Browse files
authored
Merge pull request #133 from brystfire08/master
For zip with password
2 parents 399dce2 + 4113936 commit fde65bd

File tree

3 files changed

+113
-1
lines changed

3 files changed

+113
-1
lines changed

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,27 @@ zip(sourcePath, targetPath)
163163
})
164164
```
165165

166+
**zipWithPassword(source: string, target: string, password: string, encryptionType: string): Promise**
167+
168+
> zip source to target
169+
170+
Example
171+
172+
```js
173+
const targetPath = `${DocumentDirectoryPath}/myFile.zip`
174+
const sourcePath = DocumentDirectoryPath
175+
const password = 'password'
176+
const encryptionType = 'STANDARD'; //possible values: AES-256, AES-128, STANDARD. default is STANDARD
177+
178+
zipWithPassword(sourcePath, targetPath, password, encryptionType)
179+
.then((path) => {
180+
console.log(`zip completed at ${path}`)
181+
})
182+
.catch((error) => {
183+
console.log(error)
184+
})
185+
```
186+
166187
**unzip(source: string, target: string): Promise**
167188

168189
> unzip from source to target
@@ -182,7 +203,7 @@ unzip(sourcePath, targetPath)
182203
})
183204
```
184205

185-
**unzipWithPassword(source: string, target: string, passowrd: string): Promise**
206+
**unzipWithPassword(source: string, target: string, password: string): Promise**
186207

187208
> unzip from source to target
188209

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

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import net.lingala.zip4j.exception.ZipException;
3333
import net.lingala.zip4j.model.FileHeader;
3434
import net.lingala.zip4j.progress.ProgressMonitor;
35+
import net.lingala.zip4j.model.ZipParameters;
36+
import net.lingala.zip4j.util.Zip4jConstants;
3537

3638
public class RNZipArchiveModule extends ReactContextBaseJavaModule {
3739
private static final String TAG = RNZipArchiveModule.class.getSimpleName();
@@ -339,6 +341,91 @@ public void zip(String fileOrDirectory, String destDirectory, Promise promise) {
339341
}
340342
}
341343

344+
@ReactMethod
345+
public void zipWithPassword(final String fileOrDirectory, final String destDirectory, final String password,
346+
final String encyptionMethod, final Promise promise) {
347+
new Thread(new Runnable() {
348+
@Override
349+
public void run() {
350+
351+
try {
352+
net.lingala.zip4j.core.ZipFile zipFile = new net.lingala.zip4j.core.ZipFile(destDirectory);
353+
354+
ZipParameters parameters = new ZipParameters();
355+
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
356+
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
357+
358+
String encParts[] = encyptionMethod.split("-");
359+
360+
if (password != null && !password.isEmpty()) {
361+
parameters.setEncryptFiles(true);
362+
if (encParts[0].equals("AES")) {
363+
parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
364+
if (encParts[1].equals("128")) {
365+
parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_128);
366+
} else if (encParts[1].equals("256")) {
367+
parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
368+
} else {
369+
parameters.setAesKeyStrength(Zip4jConstants.ENC_METHOD_STANDARD);
370+
}
371+
} else if (encyptionMethod.equals("STANDARD")) {
372+
parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD);
373+
Log.d(TAG, "Standard Encryption");
374+
} else {
375+
parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD);
376+
Log.d(TAG, "Encryption type not supported default to Standard Encryption");
377+
}
378+
parameters.setPassword(password);
379+
} else {
380+
promise.reject(null, "Password is empty");
381+
}
382+
383+
updateProgress(0, 100, destDirectory);
384+
385+
File f = new File(fileOrDirectory);
386+
387+
int totalFiles = 0;
388+
int fileCounter = 0;
389+
390+
if (f.exists()) {
391+
if (f.isDirectory()) {
392+
393+
List<File> files = getSubFiles(f, true);
394+
395+
totalFiles = files.size();
396+
for (int i = 0; i < files.size(); i++) {
397+
if (files.get(i).isDirectory()) {
398+
zipFile.addFolder(files.get(i).getAbsolutePath(), parameters);
399+
}
400+
else {
401+
zipFile.addFile(files.get(i), parameters);
402+
}
403+
fileCounter += 1;
404+
updateProgress(fileCounter, totalFiles, destDirectory);
405+
}
406+
407+
} else {
408+
totalFiles = 1;
409+
zipFile.addFile(f, parameters);
410+
fileCounter += 1;
411+
updateProgress(fileCounter, totalFiles, destDirectory);
412+
}
413+
}
414+
else {
415+
promise.reject(null, "File or folder does not exist");
416+
}
417+
418+
updateProgress(1, 1, destDirectory); // force 100%
419+
promise.resolve(destDirectory);
420+
421+
} catch (Exception ex) {
422+
promise.reject(null, ex.getMessage());
423+
return;
424+
}
425+
}
426+
}).start();
427+
}
428+
342429
private List<File> getSubFiles(File baseDir, boolean isContainFolder) {
343430
List<File> fileList = new ArrayList<>();
344431
File[] tmpList = baseDir.listFiles();

index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ 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)
22+
}
23+
2024
export const isPasswordProtected = (source) => {
2125
return RNZipArchive.isPasswordProtected(source).then(isEncrypted => !!isEncrypted)
2226
}

0 commit comments

Comments
 (0)