CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/data/post/zip/zip.js
Views: 1904
/*1* Original technique from http://naterice.com/zip-and-unzip-files-using-the-windows-shell-and-vbscript/2*/34function create_zip(dst)5{6var header = "\x50\x4b\x05\x06" +7"\x00\x00\x00\x00\x00\x00\x00\x00\x00" +8"\x00\x00\x00\x00\x00\x00\x00\x00\x00";910/*11* Trick to write a binary file regardless of the system locale12*/13var outw = new ActiveXObject("ADODB.Stream");14outw.Type = 2;15outw.Open();16outw.WriteText(header);17outw.Position = 0;1819var outa = new ActiveXObject("ADODB.Stream");20outa.Type = 2;21outa.Charset = "windows-1252";22outa.Open()2324outw.CopyTo(outa);25outa.SaveToFile(dst, 2);2627outw.Close();28outa.Close();29}3031function basename(path)32{33var a = path.split("\\");34var b = a.slice(-1);35return b[0];36}3738function fileeq(a, b)39{40return basename(a).toLowerCase() == basename(b).toLowerCase();41}4243function zip(src, dst)44{45var shell = new ActiveXObject('Shell.Application');46var fso = new ActiveXObject('Scripting.FileSystemObject');4748/*49* Normalize paths, required by the shell commands50*/51src = fso.GetAbsolutePathName(src);52dst = fso.GetAbsolutePathName(dst);5354/*55* Create an empty zip file if necessary56*/57if (!fso.FileExists(dst)) {58create_zip(dst);59}6061/*62* Check for duplicates63*/64var zipfile = shell.Namespace(dst);65var files = zipfile.items();66var count = files.Count;67for (var i = 0; i < files.Count; i++) {68if (fileeq(files.Item(i).Name, src)) {69return;70}71}7273zipfile.CopyHere(src);7475/*76* Wait for completion, but data can be stale on network shares, so we77* abort after 5 seconds.78*/79var max_tries = 50;80while (count == zipfile.items().Count) {81WScript.Sleep(100);82if (max_tries-- == 0) {83return;84}85}86}878889