forked from mockingbot/react-native-zip-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
66 lines (55 loc) · 1.84 KB
/
index.js
File metadata and controls
66 lines (55 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import ReactNative from "react-native";
const { NativeEventEmitter, NativeModules } = ReactNative;
const RNZipArchive = NativeModules.RNZipArchive;
const rnzaEmitter = new NativeEventEmitter(RNZipArchive);
const normalizeFilePath = (path) =>
path.startsWith("file://") ? path.slice(7) : path;
export const unzip = (source, target, charset = "UTF-8") => {
return RNZipArchive.unzip(normalizeFilePath(source), normalizeFilePath(target), charset);
};
export const isPasswordProtected = (source) => {
return RNZipArchive.isPasswordProtected(normalizeFilePath(source)).then(
(isEncrypted) => !!isEncrypted
);
};
export const unzipWithPassword = (source, target, password) => {
return RNZipArchive.unzipWithPassword(
normalizeFilePath(source),
normalizeFilePath(target),
password
);
};
export const zipWithPassword = (
source,
target,
password,
encryptionMethod = ""
) => {
return Array.isArray(source)
? RNZipArchive.zipFilesWithPassword(
source.map(normalizeFilePath),
normalizeFilePath(target),
password,
encryptionMethod
)
: RNZipArchive.zipFolderWithPassword(
normalizeFilePath(source),
normalizeFilePath(target),
password,
encryptionMethod
);
};
export const zip = (source, target) => {
return Array.isArray(source)
? RNZipArchive.zipFiles(source.map(normalizeFilePath), normalizeFilePath(target))
: RNZipArchive.zipFolder(normalizeFilePath(source), normalizeFilePath(target));
};
export const unzipAssets = (source, target) => {
if (!RNZipArchive.unzipAssets) {
throw new Error("unzipAssets not supported on this platform");
}
return RNZipArchive.unzipAssets(normalizeFilePath(source), normalizeFilePath(target));
};
export const subscribe = (callback) => {
return rnzaEmitter.addListener("zipArchiveProgressEvent", callback);
};