task.plugin('zip', { every:false }, function * (files, opts) { opts = Object.assign({ file:'archive.zip' }, opts); let curr; for (const file of files) { if (file.data) { curr = p.relative(task.root, p.format(file)); zip.file(curr, file.data, { base64:true }); } } const outdata = yield zip.generateAsync({ type:'nodebuffer' }); // if an alt `dest` was given if (opts.dest !== void 0) { // write file without overwriting files const outfile = p.resolve(task.root, opts.dest, opts.file); yield utils.write(outfile, outdata); } else { // overwrite with the archive this._.files = [{ dir: files[0].dir, base: opts.file, data: outdata }]; } });
/** * Creates an ZipUtil object, the representation of a zip file * @constructor */ function ZipUtil() { JSZip.call(this); }
// Wait for all queue to resolve Promise.all(queue) .then(() => zip.generateAsync({ type: 'nodebuffer' })) .then((buffer) => { let filename = 'Postleaf Backup (' + Moment.tz('utc').format('YYYY-MM-DD') + ').zip'; // Send zip as a download res.setHeader('Content-Type', 'application/octet-stream'); res.setHeader('Content-disposition', 'attachment; filename="' + filename + '"'); res.end(buffer); }) .catch((err) => next(err));
/* 压缩文件 读取目录及文件 */ function readDir(obj, nowPath, laodPath) { let files = fs.readdirSync(nowPath); //读取目录中的所有文件及文件夹(同步操作) files.forEach(function(fileName, index) { //遍历检测目录中的文件 let fillPath = nowPath + "/" + fileName; let file = fs.statSync(fillPath); //获取一个文件的属性 if (file.isDirectory()) { //如果是目录的话,继续查询 let pushPath = fillPath.replace(laodPath, '') let dirlist = zip.folder(pushPath); //压缩对象中生成该目录 readDir(dirlist, fillPath, laodPath); //重新检索目录文件 } else { obj.file(fileName, fs.readFileSync(fillPath)); //压缩目录添加文件 } }); }
persistAssetsFromSVFPath (svfPath, derivativeUrn) { return new Promise((resolve, reject) => { try { fs.readFile(svfPath, (err, data) => { if (err) reject(err); jsZip.loadAsync(data).then(contents => { contents.files['manifest.json'].async('string').then(data => { this.persistAssetsFromManifest(JSON.parse(data), derivativeUrn); resolve() }) }) }) } catch (err) { reject(err) } }) }
JSZip.loadAsync(data) .then((zip) => { let foundCsv = false zip.forEach((relPath, file) => { if (!foundCsv && relPath.endsWith('.csv')) { foundCsv = true file.async('string').then((csvString) => { return done(null, csvString, csvString.length, options) }).catch(done) } }) if (!foundCsv) { console.error('No CSV file found in ZIP archive.') return done(new Error('no CSV file found')) } }).catch(done)
function packfiles (directory) { const files = fs.readdirSync(directory) for (let i = 0; i < files.length; i++) { const file = files[i] const filepath = path.join(directory, file) let invalid = false for (let j = 0; j < ignoreFiles.length; j++) { const ignoreFile = path.join(__dirname, '..', ignoreFiles[j]).replace(/{\.}/g, '.') if (filepath.substr(0, ignoreFile.length) === ignoreFile) { invalid = true break } } if (invalid) { continue } const stat = fs.statSync(filepath) const relative = path.relative(rootDir, filepath).replace(/\\/g, '/') if (stat.isDirectory()) { zipArchive.folder(relative) packfiles(filepath) } else { zipArchive.file(relative, fs.readFileSync(filepath)) } } }
request(core.latestVersionZip, function () { const jsZip = require('jszip') jsZip.loadAsync(fs.readFileSync(localZipFile)).then(function (zip) { let countDone = 0 let countFiles = Object.keys(zip.files).length const fileDoneCb = function () { countDone++ Object.keys(zip.files).forEach(function (index) { const zipFile = zip.files[index] const filepath = path.join(dir, zipFile.name.replace(/\\/g, path.sep)) if (zipFile.dir === true) {
JSZip.loadAsync(data) .then((zip) => verifyBackup(zip))
}, function(tileDone) { GeoPackage.getTileFromXYZ(geopackage, name, xTile, yTile, zoom, 256, 256, function(err, tile) { zip.file(zoom+'/'+xTile+'/'+yTile+'.png', tile); yTile++; tileDone(); zip.generateAsync({type: 'nodebuffer', compression: 'DEFLATE'}).then(function(content) { zipCreated(null, content); });
name: fileTemp, }) await zip.generateAsync({ //设置压缩格式,开始打包 type: "nodebuffer", //nodejs用 compression: "DEFLATE", //压缩算法