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.
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.
Adam says
Would it be possible to use similar code for .gz files?
WeirdGeek says
Hey Adam, Yes it should work with .gz files. Please let me know if you need any help. Thanks
Enriuqe says
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.
Monica says
It is possilbe to unzip files with a password?
Monica says
It is possible to unzip files with a password?
Paula says
Hello 🙂
I used your code but unfortunately only one file is visible in the new created folder.
Thank you for your help.
JP says
Thanks for this code. I’m unable to make it work though. It is saying Execution Completed but no action is happening
Muhammad Arif says
Thanks for the article. Is there a way to use this process for password protected zip files?