From 16e10a84ce3f34a754698526466b19b59b613500 Mon Sep 17 00:00:00 2001 From: Book Pauk Date: Tue, 16 Aug 2022 17:34:17 +0700 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=20=D0=B4=D0=BB=D1=8F=20?= =?UTF-8?q?=D1=87=D1=82=D0=B5=D0=BD=D0=B8=D1=8F=20zip-=D0=B0=D1=80=D1=85?= =?UTF-8?q?=D0=B8=D0=B2=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/core/ZipReader.js | 57 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 server/core/ZipReader.js diff --git a/server/core/ZipReader.js b/server/core/ZipReader.js new file mode 100644 index 0000000..4103cb3 --- /dev/null +++ b/server/core/ZipReader.js @@ -0,0 +1,57 @@ +const StreamZip = require('node-stream-zip'); + +class ZipReader { + constructor() { + this.zip = null; + } + + checkState() { + if (!this.zip) + throw new Error('Zip closed'); + } + + async open(zipFile) { + if (this.zip) + throw new Error('Zip file already open'); + + const zip = new StreamZip.async({file: zipFile}); + + this.zipEntries = await zip.entries(); + + this.zip = zip; + } + + get entries() { + this.checkState(); + + return this.zipEntries; + } + + async extractToBuf(entryFilePath) { + this.checkState(); + + return await this.zip.entryData(entryFilePath); + } + + async extractToFile(entryFilePath, outputFile) { + this.checkState(); + + await this.zip.extract(entryFilePath, outputFile); + } + + async extractAllToDir(outputDir) { + this.checkState(); + + await this.zip.extract(null, outputDir); + } + + close() { + if (this.zip) { + this.zip.close(); + this.zip = null; + this.zipEntries = null; + } + } +} + +module.exports = ZipReader; \ No newline at end of file