zip.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * zip.js - a simple zip implementation in jscript.
  3. *
  4. * Copyright (C) 2008-2012 Alon Bar-Lev <[email protected]>
  5. *
  6. * BSD License
  7. * ============
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. *
  11. * o Redistributions of source code must retain the above copyright notice,
  12. * this list of conditions and the following disclaimer.
  13. * o Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * o Neither the name of the Alon Bar-Lev nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  24. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. * POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. var ForReading = 0;
  34. var ForWriting = 1;
  35. var fso = new ActiveXObject("Scripting.FileSystemObject");
  36. function zip(source, destination) {
  37. try {
  38. var f = OpenTextFile(destination, ForReading);
  39. f.Close();
  40. }
  41. catch(e) {
  42. var f = fso.CreateTextFile(destination, ForWriting);
  43. var zipheader = "PK" + String.fromCharCode(5) + String.fromCharCode(6);
  44. for (var i=0;i<18;i++) {
  45. zipheader += String.fromCharCode(0);
  46. }
  47. f.Write(zipheader);
  48. f.Close();
  49. }
  50. var shell = new ActiveXObject("Shell.Application");
  51. var source = shell.NameSpace(fso.GetAbsolutePathName(source));
  52. var destination = shell.NameSpace(fso.GetAbsolutePathName(destination));
  53. destination.CopyHere(source.Items(), 4);
  54. while(source.Items().Count != destination.Items().Count) {
  55. WScript.Sleep(1000);
  56. }
  57. }
  58. var index = 0;
  59. var source = WScript.Arguments(index++);
  60. var destination = WScript.Arguments(index++);
  61. try {
  62. zip(source, destination);
  63. WScript.Quit(0);
  64. }
  65. catch(e) {
  66. WScript.Echo("ERROR: Cannot zip '" + destination + "'.");
  67. WScript.Quit(1);
  68. }