|
| 1 | + |
| 2 | +var yauzl = require("../"); |
| 3 | +var path = require("path"); |
| 4 | +var fs = require("fs"); |
| 5 | +var Transform = require("stream").Transform; |
| 6 | +var Pend = require("pend"); // npm install pend |
| 7 | + |
| 8 | +var zipFilePath = process.argv[2]; |
| 9 | +if (zipFilePath == null || /^-/.test(zipFilePath)) { |
| 10 | + console.log( |
| 11 | + "usage: node unzip.js path/to/file.zip\n" + |
| 12 | + "\n" + |
| 13 | + "unzips the specified zip file into the current directory"); |
| 14 | + process.exit(1); |
| 15 | +} |
| 16 | + |
| 17 | +function mkdirpSync(dir) { |
| 18 | + if (dir === ".") return; |
| 19 | + try { |
| 20 | + fs.statSync(dir); |
| 21 | + return; // already exists |
| 22 | + } catch (e) { |
| 23 | + } |
| 24 | + var parent = path.dirname(dir); |
| 25 | + mkdirpSync(parent); |
| 26 | + |
| 27 | + process.stdout.write(dir.replace(/\/$/, "") + "/\n"); |
| 28 | + fs.mkdirSync(dir); |
| 29 | +} |
| 30 | + |
| 31 | +yauzl.open(zipFilePath, function(err, zipfile) { |
| 32 | + if (err) throw err; |
| 33 | + |
| 34 | + // Use Pend to do one thing at a time. |
| 35 | + // This enables prettier progress output. |
| 36 | + var pend = new Pend(); |
| 37 | + pend.max = 1; |
| 38 | + |
| 39 | + // don't start any of the other `go()`s until we're done creating directories |
| 40 | + pend.go(function(callback) { |
| 41 | + zipfile.on("end", function() { |
| 42 | + callback(); |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + zipfile.on("entry", function(entry) { |
| 47 | + if (/\/$/.test(entry.fileName)) { |
| 48 | + // directory file names end with '/' |
| 49 | + mkdirpSync(entry.fileName); |
| 50 | + } else { |
| 51 | + // ensure parent directory exists |
| 52 | + mkdirpSync(path.dirname(entry.fileName)); |
| 53 | + // call openReadStream before we return from this function, |
| 54 | + // or else the zipfile might autoclose before we get a chance to read it. |
| 55 | + zipfile.openReadStream(entry, function(err, readStream) { |
| 56 | + if (err) throw err; |
| 57 | + pend.go(function(callback) { |
| 58 | + // report progress through large files |
| 59 | + var byteCount = 0; |
| 60 | + var totalBytes = entry.uncompressedSize; |
| 61 | + var lastReportedString = byteCount + "/" + totalBytes + " 0%"; |
| 62 | + process.stdout.write(entry.fileName + "..." + lastReportedString); |
| 63 | + function reportString(msg) { |
| 64 | + var clearString = ""; |
| 65 | + for (var i = 0; i < lastReportedString.length; i++) { |
| 66 | + clearString += "\b"; |
| 67 | + if (i >= msg.length) { |
| 68 | + clearString += " \b"; |
| 69 | + } |
| 70 | + } |
| 71 | + process.stdout.write(clearString + msg); |
| 72 | + lastReportedString = msg; |
| 73 | + } |
| 74 | + // report progress at 60Hz |
| 75 | + var progressInterval = setInterval(function() { |
| 76 | + reportString(byteCount + "/" + totalBytes + " " + ((byteCount / totalBytes * 100) | 0) + "%"); |
| 77 | + }, 1000 / 60); |
| 78 | + var filter = new Transform(); |
| 79 | + filter._transform = function(chunk, encoding, cb) { |
| 80 | + byteCount += chunk.length; |
| 81 | + cb(null, chunk); |
| 82 | + }; |
| 83 | + filter._flush = function(cb) { |
| 84 | + clearInterval(progressInterval); |
| 85 | + reportString(""); |
| 86 | + // delete the "..." |
| 87 | + process.stdout.write("\b \b\b \b\b \b\n"); |
| 88 | + cb(); |
| 89 | + callback(); |
| 90 | + }; |
| 91 | + |
| 92 | + // pump file contents |
| 93 | + readStream.pipe(filter).pipe(fs.createWriteStream(entry.fileName)); |
| 94 | + }); |
| 95 | + }); |
| 96 | + } |
| 97 | + }); |
| 98 | +}); |
| 99 | + |
0 commit comments