You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

seek-bzip-table 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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('[infile]')
  8. .option('-m, --multistream',
  9. 'Read a multistream bzip2 file');
  10. program.on('--help', function() {
  11. console.log(' If <infile> is omitted, reads from stdin.');
  12. });
  13. program.parse(process.argv);
  14. var makeInStream = function(in_fd) {
  15. var stat = fs.fstatSync(in_fd);
  16. var stream = {
  17. buffer: new Buffer(4096),
  18. totalPos: 0,
  19. pos: 0,
  20. end: 0,
  21. _fillBuffer: function() {
  22. this.end = fs.readSync(in_fd, this.buffer, 0, this.buffer.length);
  23. this.pos = 0;
  24. },
  25. readByte: function() {
  26. if (this.pos >= this.end) { this._fillBuffer(); }
  27. if (this.pos < this.end) {
  28. this.totalPos++;
  29. return this.buffer[this.pos++];
  30. }
  31. return -1;
  32. },
  33. read: function(buffer, bufOffset, length) {
  34. if (this.pos >= this.end) { this._fillBuffer(); }
  35. var bytesRead = 0;
  36. while (bytesRead < length && this.pos < this.end) {
  37. buffer[bufOffset++] = this.buffer[this.pos++];
  38. bytesRead++;
  39. }
  40. this.totalPos += bytesRead;
  41. return bytesRead;
  42. },
  43. eof: function() {
  44. if (this.pos >= this.end) { this._fillBuffer(); }
  45. return !(this.pos < this.end);
  46. }
  47. };
  48. if (stat.size) {
  49. stream.size = stat.size;
  50. }
  51. return stream;
  52. };
  53. var in_fd = 0, close_in = function(){};
  54. if (program.args.length > 0) {
  55. in_fd = fs.openSync(program.args.shift(), 'r');
  56. close_in = function() { fs.closeSync(in_fd); };
  57. }
  58. var inStream = makeInStream(in_fd);
  59. var report = function(position, blocksize) {
  60. console.log(position+'\t'+blocksize);
  61. };
  62. Bunzip.table(inStream, report, program.multistream);
  63. close_in();
  64. return 0;