压缩文件夹中的所有文件并下载压缩后的文件



我想做以下事情:

a)压缩文件夹内的所有文件,该文件夹由其文件夹Id定义,并返回压缩存档的FileId

b)从zip的文件Id创建一个下载链接,这样我就可以将链接嵌入到我的Html模板中,这样我就可以让用户(具有存储.zip文件的共享驱动器的正确权限)通过点击链接下载它。

现在我被困在a)

我的代码到目前为止是从这篇文章派生的:在Google Drive中创建一个zip文件与Apps Script

但是似乎有些解决方案已经被弃用了。DocsList)。由于我在最近两年中找不到适合我的最新解决方案,我认为现在值得研究一下拉链箱。

/*****
***   This Function returns all blobs from a folder
*/
function getBlobs(folderId) {
let blobs = [];
folder = DriveApp.getFolderById(folderId)
let files = folder.getFiles();
while (files.hasNext()) {
let file = files.next().getBlob();
blobs.push(file);
return blobs;
}
}

/*****
***   This Function zips all blobs in a folder and returns the zip file ID
*/
function zipFilesInFolder(blobs, filename) {
let zip = Utilities.zip(blobs, filename + '.zip');
zipFileId = zip.getId()
Logger.log("{zipFilesInFolder} [zipFileId]: " + zipFileId)
return zipFileId;
}
filename = "myzip"
folderId = "123456789a94..."
blobs = getBlobs(folderId)
zipFileId = zipFilesInFolder(blobs, filename)

谢谢田田君!

我修复了我的代码后,您的善意的建议:

/*****
***   This Function returns all blobs from a folder
*/
function getBlobs(folderId) {
let blobs = [];

// Get the folder containing the files to be zipped by its folderId
folder = DriveApp.getFolderById(folderId)
// Get the files from that folder 
let files = folder.getFiles();
// Iterate through the files and add each ones to the array of blobs 
while (files.hasNext()) {
let file = files.next().getBlob();
blobs.push(file);
}
return blobs;
}

/*****
***   This Function zips all blobs in a folder and returns the zip file ID
*/
function zipFilesInFolder(folderId, blobs, filename) {

// Get the folder containing the files to be zipped by its folderId
folder = DriveApp.getFolderById(folderId)
let zipped = Utilities.zip(blobs, filename + '.zip');

// Create the .zip Archive File and in the parent folder 
// of the folder containing the files which were zipped
// and return its FileId
zippedFile = folder.getParents().next().createFile(zipped);
zipFileId = zippedFile.getId();

Logger.log("{zipFilesInFolder} [zipFileId]: " + zipFileId)
return zipFileId;
}

现在的a)部分像一个魅力!感谢您的快速回复!

最新更新