当您知道路径时,是否可以归档多个目录?让我们说: <hosts>
<host instances="" name="*" roles="alpha">
<tags/>
</host>
<host instances="" name="server1" id="alpha,beta">
<tags>
<tag app-id="1" instance="1" name="alpha"/>
<tag app-id="2" instance="2" name="beta"/>
</tags>
</host>
<host instances="" name="server2" id="beta,gama">
<tags>
<tag app-id="1" instance="1" name="beta"/>
<tag app-id="2" instance="2" name="gama"/>
</tags>
</host>
</hosts>
def main1(file=outfile):
tree = et.parse(file)
root = tree.getroot()
thingy = root.find('hosts')
for thing in thingy:
if "server1" in thing.get('name'):
root.remove(thing)
#thingy.remove(thing)
print thingy
。我现在正在做的是将目录复制到一个目录中,让我们说:['/dir1','dir2', .., 'dirX']
并执行以下操作:
/dirToZip
是否有方法将目录附加到存档中而不是批量需要的特定模式?提前谢谢!
答案 0 :(得分:7)
您可以使用bulk(mappings)
。尝试:
var output = fs.createWriteStream(__dirname + '/bulk-output.zip');
var archive = archiver('zip');
output.on('close', function() {
console.log(archive.pointer() + ' total bytes');
console.log('archiver has been finalized and the output file descriptor has closed.');
});
archive.on('error', function(err) {
throw err;
});
archive.pipe(output);
archive.bulk([
{ expand: true, cwd: 'views/', src: ['*'] },
{ expand: true, cwd: 'uploads/', src: ['*'] }
]);
archive.finalize();
<强>已更新强>
或者你可以更轻松地做到这一点:
var output = fs.createWriteStream(__dirname + '/bulk-output.zip');
var archive = archiver('zip');
output.on('close', function() {
console.log(archive.pointer() + ' total bytes');
console.log('archiver has been finalized and the output file descriptor has closed.');
});
archive.on('error', function(err) {
throw err;
});
archive.pipe(output);
archive.directory('views', true, { date: new Date() });
archive.directory('uploads', true, { date: new Date() });
archive.finalize();
答案 1 :(得分:2)
对于遇到同样问题的每个人来说,最终对我有用的是: 假设你有以下结构:
/用户/ X /桌面/ TMP
| __ dir1
| __ dir2
| __ dir3
var baseDir = '/Users/x/Desktop/tmp/';
var dirNames = ['dir1','dir2','dir3']; //directories to zip
var archive = archiver.create('zip', {});
archive.on('error', function(err){
throw err;
});
var output = fs.createWriteStream('/testDir/myZip.zip'); //path to create .zip file
output.on('close', function() {
console.log(archive.pointer() + ' total bytes');
console.log('archiver has been finalized and the output file descriptor has closed.');
});
archive.pipe(output);
dirNames.forEach(function(dirName) {
// 1st argument is the path to directory
// 2nd argument is how to be structured in the archive (thats what i was missing!)
archive.directory(baseDir + dirName, dirName);
});
archive.finalize();
答案 2 :(得分:1)
假设文件夹/Users/x/baseFolder
具有以下结构:
/Users/x/baseFolder
| __ dir1
| __ file1.txt
| __ file2.txt
| __ dir2
| __ file3.txt
| __ file4.txt
| __ dir3
| __ file5.txt
| __ file6.txt
| __ file7.txt
| __ file8.txt
| __ file9.txt
| __ file10.txt
我可以执行以下操作来创建具有以下结构和内容的/Users/x/baseFolder/result.zip
文件夹:
/Users/x/baseFolder/result.zip
| __ dir1
| __ file1.txt
| __ dir2
| __ file3.txt
| __ file4.txt
| __ dir3
| __ file6.txt
| __ file7.txt
| __ file8.txt
| __ file9.txt
function zipFolder(baseFolder) {
var archive = archiver('zip');
var fileNames = [
'dir1/file1.txt',
'dir3/file6.txt',
'dir3/file7.txt',
'file8.txt',
'file9.txt'
];
var folderNames = [
'dir2',
]
var output = fs.createWriteStream(path.join(baseFolder, "result.zip"));
output.on('close', function () {
console.log(archive.pointer() + ' total bytes');
console.log('archiver has been finalized and the output file descriptor has closed.');
});
archive.on('error', function (err) {
throw err;
});
archive.pipe(output);
for (i = 0; i < fileNames.length; i++) {
var stream = fs.readFileSync(path.join(baseFolder, fileNames[i]));
archive.append(stream, { name: fileNames[i] });
}
for (i = 0; i < folderNames.length; i++) {
archive.directory(path.join(baseFolder, folderNames[i]), folderNames[i]);
}
archive.finalize(function (err, bytes) {
if (err) throw err;
});
}