WeirdGeek

Data Science | Machine Learning | Automation

  • Data Analytics
  • Python
  • Data Science
  • Google Apps Script
  • Machine Learning
  • Artificial Intelligence
  • SQL Server

06/10/2019 By WeirdGeek 8 Comments

Unzip files using Google Apps Script on Google Drive

Sometime you came across situations where you have the zipped files on the Google drive and you want to automate the process but required unzipped files to do the data manipulation tasks. Here in this post i will share the code which will help you to Unzip files using Google Apps Script placed on Google Drive.

Google Apps Script (GAS)

When you are working with google tools like Google Drive, Google Spreadsheets etc., the best way to work with them is to use Google Apps script, Google Colab or other supported tools or languages which help you in making your process zero touch without any worries.

Below is the code which helps you to do the same.

Code to Unzip files using Google Apps Script:

function Unzip() {
  //Add folder ID to select the folder where zipped files are placed
  var SourceFolder = DriveApp.getFolderById("SourceFolderID")
  //Add folder ID to save the where unzipped files to be placed
  var DestinationFolder = DriveApp.getFolderById("DestinationFolderID")
  //Select the Zip files from source folder using the Mimetype of ZIP
  var ZIPFiles = SourceFolder.getFilesByType(MimeType.ZIP)
 
  //Loop over all the Zip files
  while (ZIPFiles.hasNext()){
   // Get the blob of all the zip files one by one
    var fileBlob = ZIPFiles.next().getBlob();
   //Use the Utilities Class to unzip the blob
    var unZippedfile = Utilities.unzip(fileBlob);
   //Unzip the file and save it on destination folder
    var newDriveFile = DestinationFolder.createFile(unZippedfile[0]);
    }
}

Basically, in the above code we are taking the Zipped files from one folder i.e the Source Folder using the ID and unzipping the files one by one using the Utilities class and saving the unzipped files in the Destination folder.

To get more detailed knowledge about Google Apps Script, you can check the official website. Also if you want to explore more codes/posts related to Google Apps Script on our website, then you can find it here.

If you have any questions or if you need any help please get in touch with me using the comment section below.

Related posts:

  1. Loop over two list or array in Google Apps Script
  2. Find and Copy files of specific types using Google Apps Script
  3. Rename files using Google Apps Script on Google Drive
  4. Google Apps Script Code to find the days of the week

Filed Under: Data Science, Google Apps Script Tagged With: Automation, code, Google, Google Apps Script, Google Drive

Comments

  1. Adam says

    10/12/2019 at 11:22 am

    Would it be possible to use similar code for .gz files?

    Reply
    • WeirdGeek says

      12/01/2020 at 8:06 am

      Hey Adam, Yes it should work with .gz files. Please let me know if you need any help. Thanks

      Reply
  2. Enriuqe says

    13/07/2021 at 6:09 pm

    What can I do if one zip file has 2 elements?
    This line of code gives you only the first element inside of a zip
    var newDriveFile = DestinationFolder.createFile(unZippedfile[0]);

    Thank you for your help.

    Reply
  3. Monica says

    19/10/2021 at 7:29 am

    It is possilbe to unzip files with a password?

    Reply
  4. Monica says

    19/10/2021 at 7:30 am

    It is possible to unzip files with a password?

    Reply
  5. Paula says

    17/01/2022 at 3:32 am

    Hello 🙂

    I used your code but unfortunately only one file is visible in the new created folder.

    Thank you for your help.

    Reply
  6. JP says

    28/04/2022 at 2:18 am

    Thanks for this code. I’m unable to make it work though. It is saying Execution Completed but no action is happening

    Reply
  7. Muhammad Arif says

    07/12/2022 at 9:03 am

    Thanks for the article. Is there a way to use this process for password protected zip files?

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Subscribe to my Blog !!

Enter your email below to subscribe my blog and get the latest post right in your inbox.

  • Home
  • Terms
  • Privacy
  • Contact Us

Copyright © 2025 · WeirdGeek · All trademarks mentioned are the property of their respective owners.