seek-bunzip 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #!/usr/bin/env node
  2. var program = require('commander');
  3. var Bunzip = require('../');
  4. var fs = require('fs');
  5. program
  6. .version(Bunzip.version)
  7. .usage('-d|-z [infile] [outfile]')
  8. .option('-d, --decompress',
  9. 'Decompress stdin to stdout')
  10. //.option('-z, --compress',
  11. // 'Compress stdin to stdout')
  12. .option('-b, --block <n>',
  13. 'Extract a single block, starting at <n> bits.', undefined)
  14. .option('-m, --multistream',
  15. 'Read a multistream bzip2 file');
  16. program.on('--help', function() {
  17. console.log(' If <infile> is omitted, reads from stdin.');
  18. console.log(' If <outfile> is omitted, writes to stdout.');
  19. });
  20. program.parse(process.argv);
  21. if (!program.compress) { program.decompress = true; }
  22. if (program.compress && program.block !== undefined) {
  23. console.error('--block can only be used with decompression');
  24. return 1;
  25. }
  26. if (program.decompress && program.compress) {
  27. console.error('Must specify either -d or -z.');
  28. return 1;
  29. }
  30. var makeInStream = function(in_fd) {
  31. var stat = fs.fstatSync(in_fd);
  32. var stream = {
  33. buffer: new Buffer(4096),
  34. filePos: null,
  35. pos: 0,
  36. end: 0,
  37. _fillBuffer: function() {
  38. this.end = fs.readSync(in_fd, this.buffer, 0, this.buffer.length,
  39. this.filePos);
  40. this.pos = 0;
  41. if (this.filePos !== null && this.end > 0) {
  42. this.filePos += this.end;
  43. }
  44. },
  45. readByte: function() {
  46. if (this.pos >= this.end) { this._fillBuffer(); }
  47. if (this.pos < this.end) {
  48. return this.buffer[this.pos++];
  49. }
  50. return -1;
  51. },
  52. read: function(buffer, bufOffset, length) {
  53. if (this.pos >= this.end) { this._fillBuffer(); }
  54. var bytesRead = 0;
  55. while (bytesRead < length && this.pos < this.end) {
  56. buffer[bufOffset++] = this.buffer[this.pos++];
  57. bytesRead++;
  58. }
  59. return bytesRead;
  60. },
  61. seek: function(seek_pos) {
  62. this.filePos = seek_pos;
  63. this.pos = this.end = 0;
  64. },
  65. eof: function() {
  66. if (this.pos >= this.end) { this._fillBuffer(); }
  67. return !(this.pos < this.end);
  68. }
  69. };
  70. if (stat.size) {
  71. stream.size = stat.size;
  72. }
  73. return stream;
  74. };
  75. var makeOutStream = function(out_fd) {
  76. return {
  77. buffer: new Buffer(4096),
  78. pos: 0,
  79. flush: function() {
  80. fs.writeSync(out_fd, this.buffer, 0, this.pos);
  81. this.pos = 0;
  82. },
  83. writeByte: function(byte) {
  84. if (this.pos >= this.buffer.length) { this.flush(); }
  85. this.buffer[this.pos++] = byte;
  86. }
  87. };
  88. };
  89. var in_fd = 0, close_in = function(){};
  90. var out_fd = 1, close_out = function(){};
  91. if (program.args.length > 0) {
  92. in_fd = fs.openSync(program.args.shift(), 'r');
  93. close_in = function() { fs.closeSync(in_fd); };
  94. }
  95. if (program.args.length > 0) {
  96. out_fd = fs.openSync(program.args.shift(), 'w');
  97. close_out = function() { fs.closeSync(out_fd); };
  98. }
  99. var inStream = makeInStream(in_fd);
  100. var outStream= makeOutStream(out_fd);
  101. if (program.decompress) {
  102. try {
  103. if (program.block !== undefined) {
  104. Bunzip.decodeBlock(inStream, +program.block, outStream);
  105. } else {
  106. Bunzip.decode(inStream, outStream, program.multistream);
  107. }
  108. outStream.flush();
  109. } catch (e) {
  110. if (e.code !== 'EPIPE') throw e;
  111. }
  112. close_in();
  113. close_out();
  114. return 0;
  115. }
  116. if (program.compress) {
  117. console.error('Compression not yet implemented.');
  118. return 1;
  119. }
  120. return 1;