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 create a multi file zip on drive with the API?
Update: Tanakie's code works! This is great! However, I'm only able to get it working in my personal account. I have two G-Suite admin accounts and I get the error:
"Sorry, unable to open the file at this time.
Please check the address and try again."
I have confirmed this works fine in multiple free personal google accounts. Any idea why it will not work when using a paid google account?
------- UPDATE ------
If you get the "Sorry, unable to open the file at this time" error, wait a day, that error will fix itself.
Here's my final solution only with a POST. Pretty much as Tanaike says below.
Google Script:
function doPost(e) {
var zipFileName = e.parameter.zipFileName
var fileIds = e.parameter.ids.split(",");
return ContentService.createTextOutput(zipping(fileIds, zipFileName));
}
function zipping(fileIds, zipfilename) {
var blobs = [];
var mimeInf = [];
var accesstoken = ScriptApp.getOAuthToken();
fileIds.forEach(function(fileID) {
try {
var file = DriveApp.getFileById(fileID);
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=" + fileID + "&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/" + fileID + "/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/" + fileID + "?alt=media", {
method: "GET",
headers: {"Authorization": "Bearer " + accesstoken},
muteHttpExceptions: true
}).getBlob().setName(name);
}
blobs.push(blob);
});
var zip = Utilities.zip(blobs, zipfilename);
var driveFolder = DriveApp.getFoldersByName("zipFiles").next();
return driveFolder.createFile(zip).getId();
}
php code to call it:
$url = 'https://script.google.com/a/domain.com/macros/s/### script id ####/exec';
$googleIDs = array();
foreach($documents AS $document){
$googleIDs[] = $document->google_file_id;
}
$data['zipFileName'] = date('c') . ".zip";
$data['ids'] = join(',', $googleIDs);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array( "Content-type: multipart/form-data" ));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$result = curl_exec($ch); // This returns the Google file ID of the zip file.
curl_close($ch);
HOWEVER, As in my comment below, this was a useless, futile exercise as Google limits files created by scripts to 10M. I'm off to find a solution that does not depend on Google... Wish I'd known that before I started.