ZipReader.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. const StreamUnzip = require('./node_stream_zip_changed');
  2. //const StreamUnzip = require('node-stream-zip');
  3. class ZipReader {
  4. constructor() {
  5. this.zip = null;
  6. }
  7. checkState() {
  8. if (!this.zip)
  9. throw new Error('Zip closed');
  10. }
  11. async open(zipFile, zipEntries = true) {
  12. if (this.zip)
  13. throw new Error('Zip file is already open');
  14. const zip = new StreamUnzip.async({file: zipFile, skipEntryNameValidation: true});
  15. if (zipEntries)
  16. this.zipEntries = await zip.entries();
  17. this.zip = zip;
  18. }
  19. get entries() {
  20. this.checkState();
  21. return this.zipEntries;
  22. }
  23. async extractToBuf(entryFilePath) {
  24. this.checkState();
  25. return await this.zip.entryData(entryFilePath);
  26. }
  27. async extractToFile(entryFilePath, outputFile) {
  28. this.checkState();
  29. await this.zip.extract(entryFilePath, outputFile);
  30. }
  31. async extractAllToDir(outputDir) {
  32. this.checkState();
  33. await this.zip.extract(null, outputDir);
  34. }
  35. async close() {
  36. if (this.zip) {
  37. await this.zip.close();
  38. this.zip = null;
  39. this.zipEntries = undefined;
  40. }
  41. }
  42. }
  43. module.exports = ZipReader;