The Google Drive web interface allows you to download a single .zip file if you download a directory. However, I find no way to do that with the API. Is it possible to creat
How about this workaround?
Unfortunately, there are no APIs for directly creating a zip file from outside in Google APIs. But I think that there is a workaround. There is zip()
method of Class Utilities in Google Apps Script (GAS). And there is Web Apps as a service for using GAS from outside. I think that what you want to do can be achieved by combining them.
zip()
method of Class Utilities.If this was not useful for you, I'm sorry.
How about a following sample script? This is a simple sample script for creating a zip file for files in a folder.
These are the same to the web interface.
Please do the following flow.
function doGet(e) {
var files = DriveApp.getFolderById(e.parameter.folderid).getFiles();
var fileIds = [];
while (files.hasNext()) {
fileIds.push(files.next().getId());
}
return ContentService.createTextOutput(zipping(fileIds));
}
function zipping(fileIds) {
var zipfilename = "sample.zip";
var blobs = [];
var mimeInf = [];
var accesstoken = ScriptApp.getOAuthToken();
fileIds.forEach(function(e) {
try {
var file = DriveApp.getFileById(e);
var mime = file.getMimeType();
var name = file.getName();
} catch (er) {
return er
}
var blob;
if (mime == "application/vnd.google-apps.script") {
blob = UrlFetchApp.fetch("https://script.google.com/feeds/download/export?id=" + e + "&format=json", {
method: "GET",
headers: {"Authorization": "Bearer " + accesstoken},
muteHttpExceptions: true
}).getBlob().setName(name);
} else if (~mime.indexOf('google-apps')) {
mimeInf =
mime == "application/vnd.google-apps.spreadsheet" ? ["application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", name + ".xlsx"] : mime == "application/vnd.google-apps.document" ? ["application/vnd.openxmlformats-officedocument.wordprocessingml.document", name + ".docx"] : mime == "application/vnd.google-apps.presentation" ? ["application/vnd.openxmlformats-officedocument.presentationml.presentation", name + ".pptx"] : ["application/pdf", name + ".pdf"];
blob = UrlFetchApp.fetch("https://www.googleapis.com/drive/v3/files/" + e + "/export?mimeType=" + mimeInf[0], {
method: "GET",
headers: {"Authorization": "Bearer " + accesstoken},
muteHttpExceptions: true
}).getBlob().setName(mimeInf[1]);
} else {
blob = UrlFetchApp.fetch("https://www.googleapis.com/drive/v3/files/" + e + "?alt=media", {
method: "GET",
headers: {"Authorization": "Bearer " + accesstoken},
muteHttpExceptions: true
}).getBlob().setName(name);
}
blobs.push(blob);
});
var zip = Utilities.zip(blobs, zipfilename);
return DriveApp.createFile(zip).getId();
}
curl -L "https://script.google.com/macros/s/#####/exec?folderid=### folder ID ###"
If this was useful for you, I'm glad.