CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/data/post/zip/zip.js
Views: 1904
1
/*
2
* Original technique from http://naterice.com/zip-and-unzip-files-using-the-windows-shell-and-vbscript/
3
*/
4
5
function create_zip(dst)
6
{
7
var header = "\x50\x4b\x05\x06" +
8
"\x00\x00\x00\x00\x00\x00\x00\x00\x00" +
9
"\x00\x00\x00\x00\x00\x00\x00\x00\x00";
10
11
/*
12
* Trick to write a binary file regardless of the system locale
13
*/
14
var outw = new ActiveXObject("ADODB.Stream");
15
outw.Type = 2;
16
outw.Open();
17
outw.WriteText(header);
18
outw.Position = 0;
19
20
var outa = new ActiveXObject("ADODB.Stream");
21
outa.Type = 2;
22
outa.Charset = "windows-1252";
23
outa.Open()
24
25
outw.CopyTo(outa);
26
outa.SaveToFile(dst, 2);
27
28
outw.Close();
29
outa.Close();
30
}
31
32
function basename(path)
33
{
34
var a = path.split("\\");
35
var b = a.slice(-1);
36
return b[0];
37
}
38
39
function fileeq(a, b)
40
{
41
return basename(a).toLowerCase() == basename(b).toLowerCase();
42
}
43
44
function zip(src, dst)
45
{
46
var shell = new ActiveXObject('Shell.Application');
47
var fso = new ActiveXObject('Scripting.FileSystemObject');
48
49
/*
50
* Normalize paths, required by the shell commands
51
*/
52
src = fso.GetAbsolutePathName(src);
53
dst = fso.GetAbsolutePathName(dst);
54
55
/*
56
* Create an empty zip file if necessary
57
*/
58
if (!fso.FileExists(dst)) {
59
create_zip(dst);
60
}
61
62
/*
63
* Check for duplicates
64
*/
65
var zipfile = shell.Namespace(dst);
66
var files = zipfile.items();
67
var count = files.Count;
68
for (var i = 0; i < files.Count; i++) {
69
if (fileeq(files.Item(i).Name, src)) {
70
return;
71
}
72
}
73
74
zipfile.CopyHere(src);
75
76
/*
77
* Wait for completion, but data can be stale on network shares, so we
78
* abort after 5 seconds.
79
*/
80
var max_tries = 50;
81
while (count == zipfile.items().Count) {
82
WScript.Sleep(100);
83
if (max_tries-- == 0) {
84
return;
85
}
86
}
87
}
88
89