-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
59 lines (52 loc) · 1.94 KB
/
index.js
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
var FS = require('fs'),
NODE_RATIFY = require('node-ratify');
function isZip(path, cb) {
if (!NODE_RATIFY.isFunction(cb)) {
throw new TypeError('callback not provided');
} else if(!NODE_RATIFY.isString(path)) {
throw new TypeError('provided path is not correct');
} else {
var buffer = new Buffer(4);
FS.open(path, 'r', function(err, fd) {
if(err) {
throw(err);
} else {
FS.read(fd, buffer, 0, 4, 0, function(err, bytesRead, buffer) {
if(err) {
FS.close(fd, function(err1) {
cb(err1 || err, false);
});
} else {
if (buffer && buffer.length === 4) {
cb(null, (buffer[0] === 0x50 && buffer[1] === 0x4b && (buffer[2] === 0x03 || buffer[2] === 0x05 || buffer[2] === 0x07) && (buffer[3] === 0x04 || buffer[3] === 0x06 || buffer[3] === 0x08)));
} else {
cb(null, false);
}
}
});
}
});
}
};
function isZipSync(path) {
var ret = false;
if(!NODE_RATIFY.isString(path)) {
throw new TypeError('provided path is not correct');
} else {
var buffer = new Buffer(4);
var fd = FS.openSync(path, 'r');
if(fd) {
// Read the file synchronously
FS.readSync(fd, buffer, 0, 4, 0);
if (buffer && buffer.length === 4)
ret = (buffer[0] === 0x50 && buffer[1] === 0x4b && (buffer[2] === 0x03 || buffer[2] === 0x05 || buffer[2] === 0x07) && (buffer[3] === 0x04 || buffer[3] === 0x06 || buffer[3] === 0x08));
// Close the file
FS.closeSync(fd);
}
return ret;
}
};
exports = module.exports = {
isZip : isZip,
isZipSync : isZipSync
};