64 lines
2.2 KiB
JavaScript
64 lines
2.2 KiB
JavaScript
const express = require("express");
|
|
const fs = require('fs');
|
|
const path = require("path");
|
|
const { argv } = require("process");
|
|
const yauzl = require('yauzl');
|
|
const file = argv[2]
|
|
|
|
function unzipFile(zipPath, extractPath, callback) {
|
|
yauzl.open(zipPath, { lazyEntries: true }, function (err, zipfile) {
|
|
if (err) return callback(err);
|
|
console.log("start unzip " + zipPath + " to " + extractPath);
|
|
zipfile.readEntry();
|
|
zipfile.on("entry", function (entry) {
|
|
const entryPath = path.join(extractPath, entry.fileName);
|
|
if (/\/$/.test(entry.fileName)) {
|
|
// 递归创建目录
|
|
fs.mkdir(entryPath, { recursive: true }, function (err) {
|
|
if (err) return callback(err);
|
|
zipfile.readEntry();
|
|
});
|
|
} else {
|
|
// 先确保父目录存在
|
|
fs.mkdir(path.dirname(entryPath), { recursive: true }, function (err) {
|
|
if (err) return callback(err);
|
|
zipfile.openReadStream(entry, function (err, readStream) {
|
|
if (err) return callback(err);
|
|
const writeStream = fs.createWriteStream(entryPath);
|
|
readStream.pipe(writeStream);
|
|
readStream.on("end", function () {
|
|
zipfile.readEntry();
|
|
});
|
|
writeStream.on("error", callback);
|
|
});
|
|
});
|
|
}
|
|
});
|
|
zipfile.on("end", function () {
|
|
callback();
|
|
});
|
|
zipfile.on("error", callback);
|
|
});
|
|
}
|
|
|
|
const zipPath = path.join(__dirname, 'zip', file);
|
|
const extractPath = path.join(__dirname, 'static');
|
|
|
|
if (!file) {
|
|
console.error('请在命令行参数中指定 zip 文件名');
|
|
process.exit(1);
|
|
}
|
|
|
|
unzipFile(zipPath, extractPath, function (err) {
|
|
if (err) {
|
|
console.error('解压失败:', err);
|
|
process.exit(1);
|
|
}
|
|
console.log("unzip done");
|
|
|
|
var app = express();
|
|
app.use(express.static(extractPath));
|
|
var port = 1188;
|
|
app.listen(port);
|
|
console.log("html5_server listen to " + port);
|
|
}); |