将文件夹下载为Zip Google Drive API [英] Download Folder as Zip Google Drive API
问题描述
我目前在Google表格中有一个Google App脚本,该脚本可以为我提供文件夹的URL,然后可以使用该URL下载.尽管这是一个额外的步骤,但我想删除它,并直接获取压缩内容的URL.
I currently have a Google App Script in Google Sheet that gives me the URL of a folder, which I can then use to download. Though it is an extra step I would like to remove, and get the URL of the zipped content directly.
这是我的代码(谷歌应用程序脚本):
Here's my code (google app script):
function downloadSelectedScripts() {
// ...
var scriptFiles = getScriptFiles(scriptFileNames)
var tempFolder = copyFilesToTempFolder(scriptFiles)
Browser.msgBox(tempFolder.getUrl())
}
function copyFilesToTempFolder(files) {
var tempFolder = DriveApp.getFolderById(FOLDERS.TEMP)
var tempSubFolder = tempFolder.createFolder('download_' + Date.now())
for (var i in files) {
var file = files[i]
file.makeCopy(file.getName(), tempSubFolder)
}
return tempSubFolder
}
推荐答案
- 您想将文件夹中的所有文件压缩为zip文件.
- 该文件夹没有子文件夹.
- 所有文件都是纯文本文件.
- 所有文件的总大小小于50 MB.
- 检索文件夹.
- 检索文件夹中所有文件的斑点.
- 压缩blob并获取zip文件.
- 创建一个zip blob作为文件.
- 获取下载URL.
如果我的理解是正确的,那么该示例脚本如何?该脚本的流程如下.
If my understanding is correct, how about this sample script? The flow of this script is as follows.
示例脚本:
使用此脚本时,请设置要压缩的文件夹的文件夹ID.
Sample script:
When you use this script, please set the folder ID of folder that you want to compress.
function myFunction() { var folderId = "###"; // Please set the folder ID here. var folder = DriveApp.getFolderById(folderId); var files = folder.getFiles(); var blobs = []; while (files.hasNext()) { blobs.push(files.next().getBlob()); } var zipBlob = Utilities.zip(blobs, folder.getName() + ".zip"); var fileId = DriveApp.createFile(zipBlob).getId(); var url = "https://drive.google.com/uc?export=download&id=" + fileId; Logger.log(url); }
结果:
按如下所示返回zip文件的直接链接.
Result:
The direct link of the zip file is returned as follows.
https://drive.google.com/uc?export=download&id=###
注意:
- 很遗憾,在当前阶段,不能使用Google Apps脚本将文件夹包括在zip数据中.
- 在此示例脚本中,zip文件的文件名是文件夹名称.因此,请根据您的情况对其进行修改.
- 如果您想在不登录Google的情况下下载zip文件,请共享该文件.
这篇关于将文件夹下载为Zip Google Drive API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!