使用apps脚本在Google Drive中创建一个zip文件 [英] Creating a zip file inside google drive with apps script
问题描述
我在包含少量文件的Google云端硬盘文件夹中有一个文件夹。我想制作一个谷歌应用程序脚本,用于压缩该文件夹中的所有文件,并创建该文件夹中的zip文件。我找到了一个具有Utilities.zip()函数的视频。但是没有API参考。如何使用它?提前致谢。
I have a folder in google drive folder containing few files. I want to make a google apps script that zip all files in that folder and crate the zip file inside that same folder. I found a video that has Utilities.zip() function. But there is no API reference for that. How to use it? Thanks in advance.
推荐答案
其实它比这更简单。文件已经是Blob(任何具有getBlob()的文件都可以传递给任何需要Blob的函数)。所以代码如下所示:
Actually it's even easier than that. Files are already Blobs (anything that has getBlob() can be passed in to any function that expects Blobs). So the code looks like this:
var folder = DocsList.getFolder('path/to/folder');
folder.createFile(Utilities.zip(folder.getFiles(), 'newFiles.zip'));
另外,如果您在文件夹中有多个具有相同名称的文件,它将不起作用。 。谷歌云端硬盘文件夹支持,但Zip文件不支持。
Additionally, it won't work if you have multiple files with the same name in the Folder... Google Drive folders support that, but Zip files do not.
为了使这个工作可以使用多个同名的文件:
To make this work with multiple files that have the same name:
var folder = DocsList.getFolder('path/to/folder');
var names = {};
folder.createFile(Utilities.zip(folder.getFiles().map(function(f){
var n = f.getName();
while (names[n]) { n = '_' + n }
names[n] = true;
return f.getBlob().setName(n);
}), 'newFiles.zip'));
这篇关于使用apps脚本在Google Drive中创建一个zip文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!