decompress-zip 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/usr/bin/env node
  2. 'use strict';
  3. var nopt = require('nopt');
  4. var path = require('path');
  5. var version = require('../package.json').version;
  6. var knownOptions = {
  7. 'list': Boolean,
  8. 'extract': Boolean,
  9. 'path': path
  10. };
  11. var shortcuts = {
  12. 'x': ['--extract'],
  13. 'l': ['--list'],
  14. 'p': ['--path'],
  15. 'v': ['--version']
  16. };
  17. var parsedOptions = nopt(knownOptions, shortcuts);
  18. var pad = function (string, length) {
  19. string = String(string);
  20. if (length <= string.length) {
  21. return string;
  22. }
  23. return string + (new Array(length - string.length).join(' '));
  24. };
  25. var octal = function (number, digits) {
  26. var result = '';
  27. for (var i = 0; i < digits; i++) {
  28. result = (number & 0x07) + result;
  29. number >>= 3;
  30. }
  31. return result;
  32. };
  33. var DecompressZip = require('../lib/decompress-zip');
  34. var zip = new DecompressZip(parsedOptions.argv.remain[0]);
  35. zip.on('file', function (file) {
  36. console.log([octal(file.mode, 4), pad(file.type, 13), pad(file.compressedSize, 10), pad(file.uncompressedSize, 10), file.path].join(' '));
  37. });
  38. zip.on('list', function (fileList) {
  39. // console.log(fileList);
  40. });
  41. zip.on('extract', function (result) {
  42. console.log(result);
  43. });
  44. zip.on('error', function (error) {
  45. console.error(error.message, error.stack);
  46. });
  47. if (parsedOptions.version) {
  48. console.log('version ' + version);
  49. } else if (parsedOptions.list) {
  50. console.log('Mode Type Zip size Full size Path');
  51. console.log('---- ---- -------- --------- ----');
  52. zip.list();
  53. } else if (parsedOptions.extract) {
  54. var options = {};
  55. if (parsedOptions.path) {
  56. options.path = parsedOptions.path;
  57. }
  58. zip.extract(options);
  59. } else {
  60. console.log('Usage: decompress-zip <options> <file>');
  61. console.log(' -x, --extract extract the given file');
  62. console.log(' -l, --list list the contents of the given file');
  63. console.log(' -v, --version extract the given file');
  64. console.log(' -p, --path <path> extract the file into <path>');
  65. console.log(' -h, --help show this message');
  66. }