我正在使用Node.js并将其解压缩以仅将目录解压缩,保存到磁盘,然后它们为节点静态服务器提供服务。
这很奇怪,但是随后我将文件夹解压缩并保存到磁盘,从目录中删除了一些文件。奇怪的是,当我制作另一个模块进行测试时,仅包含一个Express服务器以及将文件解压缩并保存到磁盘的代码时,不会删除任何内容,并且将目录解压缩并保存所有文件仍在其中。
我猜测是因为在我的应用程序中,该文件夹在相当大的异步功能中被解压缩,因此出于某种原因它正在删除文件,否则解压缩过程会中断。
我将发布异步功能和生成的解压缩文件夹以及测试解压缩功能及其解压缩的文件夹。
const unzipp = (req, res, file) => {
try {
async.series([
(cb) => {
//const dir = `./uploads/unzip/`
const random = crypto.randomBytes(16).toString('hex');
const file = `${random}${Date.now()}`;
filename = file; //assign the random string to a new variable so it is only created once
const dirname = path.dirname(req.file.path)
const basename = path.basename(req.file.path)
newdir = path.resolve(dirname, (req.file.path.replace(basename, filename)));
fs.createReadStream(req.file.path)
.pipe(unzipper.Extract({ path: newdir }));
fs.unlinkSync(req.file.path) //using sync as you cant use (if err throw err) andsuch in async's
//resolve new path and replace zipped file with unzipped file
console.log(newdir) ///Users/nancycollins/virtuload-beta/backend/uploads/unzip/uploads/unzip/Nancy_Collins_118226967_v2.zip1568553133064
//delete the zipped file
console.log('one')
cb(null, 1)
},
(cb) => {
let newUpload = new Upload(req.body); //new instance of uplaod based on the model based on req.body
newUpload.vileId = filename; //assinging vileId to the random gen crypto string and date.now()
newUpload.path = newdir; //save upload path as the path where unzipped file was saved
newUpload.title = req.body.title;
newUpload.description = req.body.description;
newUpload.labels = req.body.labels;
//newUpload.user = req.params.id;
console.log("Ok so far")
newUpload.save()
.then(file => {
res.status(200).send("File added successfully")
console.log(file)
})
.catch(err => {
res.status(400).send('File upload failed to save to DB :(')
console.log(err)
})
console.log('two')
cb(null, 2)
}
]), (error, results) => {
res.json(results)
}
} catch (e) {
console.error(e)
}
}
// load server with express
const express = require('express');
const app = express();
const fs = require('fs')
const unzipper = require('unzipper')
// ROUTES:
app.get("/", (req, res) => {
res.send("Hello from rooooot");
});
fs.createReadStream('./file/Nancy_Collins_118226967_v2.zip')
.pipe(unzipper.Extract({ path: './file/unzip' }));
// localhost:3003
app.listen(3003, () => {
console.log("Server is listening on port 3003.")
});
我需要为我的应用解压缩整个目录。
有人知道为什么会这样吗?