0

I want to do the following:

a) zip all files inside a folder which is defined by its folder Id and return the FileId of the zipped archive

b) Create a download link from the zip's File Id so i can embed the link into my Html template so i can let users (with the right permissions to the shared drive where the .zip file is stored) download it by clicking on the link.

Now i am stuck with a)

My code so far is derived from this post: Creating a zip file inside Google Drive with Apps Script

But it seems like some of the solutions there are deprecated (f.ex. DocsList). As i could not find up-to-date solutions from the recent 2 years that work for me i thought that it is worth to look now into the zipping case.

/*****
 ***   This Function returns all blobs from a folder
 */
function getBlobs(folderId) {
  let blobs = [];
  folder = DriveApp.getFolderById(folderId)
  let files = folder.getFiles();

  while (files.hasNext()) {
      let file = files.next().getBlob();
      blobs.push(file);
  return blobs;
  }
}


/*****
 ***   This Function zips all blobs in a folder and returns the zip file ID
 */
function zipFilesInFolder(blobs, filename) {
  let zip = Utilities.zip(blobs, filename + '.zip');
  zipFileId = zip.getId()
  Logger.log("{zipFilesInFolder} [zipFileId]: " + zipFileId)
  return zipFileId;
}

filename = "myzip"
folderId = "123456789a94..."

blobs = getBlobs(folderId)
zipFileId = zipFilesInFolder(blobs, filename)
  • About `Now i am stuck with a)`, your question is to resolve this? If my understanding is correct, I think that your how do you want to do about your 2nd goal `b)`? In your script, I think that `return blobs;` is should be put in the last line. And, `zip` of `let zip = Utilities.zip(blobs, filename + '.zip');` is Blob object. `zipFileId = zip.getId()` cannot be used. In order to retrieve the file ID, it is required to create it as a file using `createFile()` method. But, I'm not sure about the output folder from your question. I apologize for this. – Tanaike Jan 07 '23 at 08:50
  • Sorry for being unclear with my question & Thank you Tanaike for your immidiate solution! I edited my now working code as above. – Jakob Czapski Jan 07 '23 at 10:53
  • Thank you for replying. I'm glad your issue was resolved. When you post your answer, please post it as an answer rather than you updated your question. – Tanaike Jan 07 '23 at 12:15
  • 1
    Thank you Tanaike for the hint, i edited my post. Have a nice Sunday! – Jakob Czapski Jan 08 '23 at 12:11

1 Answers1

1

THANK YOU Tanaike!

i fixed my code after following your kind suggestions:

/*****
 ***   This Function returns all blobs from a folder
 */
function getBlobs(folderId) {
  let blobs = [];
  
  // Get the folder containing the files to be zipped by its folderId
  folder = DriveApp.getFolderById(folderId)
  // Get the files from that folder 
  let files = folder.getFiles();

  // Iterate through the files and add each ones to the array of blobs 
  while (files.hasNext()) {
      let file = files.next().getBlob();
      blobs.push(file);
  }
  return blobs;
}

  
/*****
 ***   This Function zips all blobs in a folder and returns the zip file ID
 */
function zipFilesInFolder(folderId, blobs, filename) {
  
  // Get the folder containing the files to be zipped by its folderId
  folder = DriveApp.getFolderById(folderId)
  let zipped = Utilities.zip(blobs, filename + '.zip');
  
  // Create the .zip Archive File and in the parent folder 
  // of the folder containing the files which were zipped
  // and return its FileId
  zippedFile = folder.getParents().next().createFile(zipped);
  zipFileId = zippedFile.getId();
  
  Logger.log("{zipFilesInFolder} [zipFileId]: " + zipFileId)
  return zipFileId;
}

And now the a) part works like a charm! Thank you for your fast reply!