1

I have emails coming in containing zip files. How could I unzip and save these files to a specific folder on Google Drive using Google Apps Script? The below code part works perfectly with simple excel/pdf files.

var root = DriveApp.getRootFolder();
  for(var i in threads){
    var mesgs = threads[i].getMessages();
    for(var j in mesgs){
      var attachments = mesgs[j].getAttachments();
      for(var k in attachments){
        var attachment = attachments[k];
        var isDefinedType = checkIfDefinedType_(attachment);
        if(!isDefinedType) continue;
        var attachmentBlob = attachment.copyBlob();
                
var existingFile = DriveApp.getFilesByName(attachment.getName());
if (existingFile.hasNext()) {
  var file = existingFile.next();
  file.setTrashed(true);
}
var file = DriveApp.createFile(attachmentBlob);
parentFolder.addFile(file);
root.removeFile(file);

1 Answer 1

1

Answer:

You can add files to a folder using Utilities.unzip().

Code Example:

var attachmentBlob = attachment.copyBlob();
var files = Utilities.unzip(attachmentBlob);

files.forEach(function(file) {
  DriveApp.createFile(file)
  parentFolder.addFile(file);
  root.removeFile(file);
});

References:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.