以zip格式压缩多个文件夹 - Google Drive Scripts [英] Zip Multiple Folders in 1 zip - Google Drive Scripts
问题描述
我想为Google云端硬盘创建一个脚本。我想每周备份一次我的文件夹并将它们存储在Google云端硬盘中的另一个文件夹中。
关于每周触发器,我已经确定了,但是我遇到了问题,因为我找不到压缩整个文件夹的方法。
我想压缩的文件夹有多个子文件夹和文档。我试过在每个文件夹中搜索并制作一个文件的zip文件,但是我觉得它很复杂,并且我最终得到了一个文件夹的许多zip文件。我到目前为止的代码是这样的:
I would like to make a script for Google Drive. I want to make a weekly backup of my folders and store them in another folder in Google Drive. About the weekly trigger, I've got that OK, but I'm having problems because I can't find a way to zip an entire folder. The folders that I want to zip have multiple subfolders and documents. I've tried searching in each folder and making a zip of the files, but I find it complicated and I end up with many zip files for one folder. The code that I have so far is this:
function zipFolder(pathFolder, filename, destinyPath) {
var date = Utilities.formatDate(new Date(), "GMT", "ddMMyyyy");
var destiny = DocsList.getFolder(destinyPath);
var folder = DriveApp.getFolderById(DocsList.getFolder(pathFolder).getId());
var zip = Utilities.zip(folder, filename+date+'.zip');
destiny.createFile(zip);
}
我错误地收到它无法压缩文件夹,成为一个blob。如何解决这个问题?
And I receive in error that it can't zip a folder, it has to be a blob. How can I fix this?
谢谢!
Thanks!
推荐答案
使用这段代码:
You can use this code:
function zipFolder(pathFolder, filename, destinyPath) {
var date = Utilities.formatDate(new Date(), "GMT", "ddMMyyyy");
var destiny = DocsList.getFolder(destinyPath);
var folder = DriveApp.getFolderById(DocsList.getFolder(pathFolder).getId());
var zip = Utilities.zip(getBlobsPath(folder, ''), filename+date+'.zip');
destiny.createFile(zip);
}
function getBlobsPath(reFolder, path) {
var blobs = [];
var files = reFolder.getFiles();
while (files.hasNext()) {
var file = files.next().getBlob();
file.setName(path+file.getName());
blobs.push(file);
}
var folders = reFolder.getFolders();
while (folders.hasNext()) {
var folder = folders.next();
var fPath = path+folder.getName()+'/';
blobs.push(Utilities.newBlob([]).setName(fPath)); //comment/uncomment this line to skip/include empty folders
blobs = blobs.concat(getBlobsPath(folder, fPath));
}
return blobs;
}
这篇关于以zip格式压缩多个文件夹 - Google Drive Scripts的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!