使用 unzip module通过 NPM:
转换时,转换后的文件名为doc.xml
,因为解压后是xml文件。
不想要名称 doc
,所以这样做:
'use strict';
var fs = require('fs')
var unzip = require('unzip')
convert(process.argv[2], process.argv[3])
function convert(path, fileName) {
fs.createReadStream(path)
.pipe(unzip.Extract({ path: '/users/*****/desktop/templatexml/' + fileName + '.xml' }))
}
然后运行这个:
node /Users/*****/Desktop/converter/converter.js /Users/******/Desktop/template/103.zip 103
但结果总是"fileName.xml/doc.xml"
“fileName.xml”
显示为目录名称。
想要更改doc
部分。
如果可能的话,想从原始文件路径中获取文件名。
process.argv[2] 里面是这样的
"/Users/*****/Desktop/template/fileName.zip"
想从这里获取文件名。
谢谢你的评论,就像这样。 但是没有用。
'use strict';
var fs = require('fs')
var unzip = require('unzip')
convert(process.argv[2])
function convert(path) {
fs.createReadStream(path)
.pipe(unzip.Parse())
.on('entry', function(entry) {
var fileName = entry.path;
var type = entry.type; // 'Directory' or 'File'
var size = entry.size;
if (fileName === "this IS the file I'm looking for") {
entry.pipe(fs.createWriteStream('/users/*****/desktop/templatexml/'));
} else {
entry.autodrain();
}
});
}
最佳答案
fs.createReadStream('path/to/archive.zip')
.pipe(unzip.Parse())
.on('entry', function (entry) {
var fileName = entry.path;
var type = entry.type; // 'Directory' or 'File'
var size = entry.size;
if (fileName === "this IS the file I'm looking for") {
entry.pipe(fs.createWriteStream('output/path'));
} else {
entry.autodrain();
}
});
关于javascript - Node 解压。如何制作文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52809376/