Utilities.unzip doesn't work for zip files containing subfolders

651 Views Asked by At

I'm trying to write some Google Apps Script to extract a zip file which has 3 subfolders. Depending on the folder, I will then run through the various files and process them accordingly.

I cannot get Utilities.unzip to work when the zip file contains subfolders. I tested my code with a simple zip file and it worked as expected, so I'm guessing it is something I am missing or the .unzip simply does not work on subfolders.

Is there anyway to get this to work? Or can I extract the files via javascript and pass these to the apps script? I had a look at a couple of javascript libraries that do this but couldn't work out how to import these into my Google Web App.

UPDATE: Here is the code I have been using to test:

function ExtractSecurityZip() {

var iZIP =DriveApp.getFileById("1RAhI_mGQHay9boCQ9tt5T_0iqF6w5Eky").getBlob().setContentTypeFromExtension()

Utilities.unzip(iZIP)

return

}

I am not concerned about maintaining the folder structure in the zip file, I just need all the files.

Any help is greatly appreciated.

Dave

PS: If it helps, the zip file is an Oracle LCM Export, so I cannot remove the folder structure.

1

There are 1 best solutions below

8
Cooper On

This will unzip all the the files to a destination directory

function getAllZips(dstdir,zipid) {
  const dstFolder = DriveApp.getFolderById(dstdir);
  const zipFile = DriveApp.getFileById(zipid);
  const blobs = Utilities.unzip(zipFile.getBlob());
  blobs.forEach((b,i) => {
    let pA = b.getName().split('/');
    if(pA[pA.length-1]) {
      dstFolder.createFile(b).setName(pA[pA.length-1]);
    }
  });
}

Here's what looping through blobs.getName() reveals:

Name
SO Snippets/
SO Snippets/addCheckboxes
SO Snippets/appendIdtolocalStorage
SO Snippets/clearAllIdsFromLocalStorage
SO Snippets/clearIdsFromLocalStorage
SO Snippets/dataUrl
SO Snippets/delCheckedQuestion
SO Snippets/delCheckedQuestions
SO Snippets/delUnWantedQuestions
SO Snippets/getCurrentIdsInLocalStorage
SO Snippets/loadIdsIntoLocalStorage
SO Snippets/refreshPage
SO Snippets/remDupsSort
SO Snippets/removeFooter%26SideBar
SO Snippets/removeSomeIds
SO Snippets/removeSomeOlderIds
SO Snippets/viewCookies
SO Snippets/zButton

The ones that end in '/' are directories the others are files. I messed around with trying to restore the directory structure but it's a bit more work than I thought. So you might want to add the Javascript Tag to your list of tags. My guess is that one of them have already figured it out.

With regard to the question asked by andrewjames: Can you run this on your zip file so that I can see what it looks like?

function lookingInTheBlob(zipid='1QnrP-1lngoLQtNsqd373_UytVptuqoI1') {
  const zipFile = DriveApp.getFileById(zipid);
  const blobs = Utilities.unzip(zipFile.getBlob());
  let data = [];
  blobs.forEach((b,i) => {data.push([b.getName()]);});
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName('Sheet3');
  sh.clearContents();
  sh.getRange(1,1,data.length,data[0].length).setValues(data);
}