gpt4 book ai didi

google-apps-script - 在 1 个 zip 中压缩多个文件夹 - Google Drive Scripts

转载 作者:行者123 更新时间:2023-12-02 03:37:50 27 4
gpt4 key购买 nike

我想为 Google 云端硬盘制作一个脚本。我想每周备份我的文件夹并将它们存储在 Google Drive 的另一个文件夹中。关于每周触发器,我已经确定了,但是我遇到了问题,因为我找不到压缩整个文件夹的方法。我要压缩的文件夹有多个子文件夹和文档。我试过在每个文件夹中搜索并压缩文件,但我发现这很复杂,我最终得到一个文件夹的许多压缩文件。我到目前为止的代码是这样的:

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。我该如何解决这个问题?

谢谢!

最佳答案

您可以使用此代码:

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;
}

关于google-apps-script - 在 1 个 zip 中压缩多个文件夹 - Google Drive Scripts,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22237799/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com