Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Google App Script Unzip a file from Google Drive

I need a script that will unzip files that I have uploaded to my google drive and place the contents of the zip file back in my google drive.

I am having trouble with it. It runs with no errors, but the file it uploads is empty. I am very new to Google App Script, so any help would be greatly appreciated. Thanks.

function unZipIt() {
  var theFolder = DriveApp.getFolderById('0B9jgHw-WmzvfRS1ZZEhTc3Byak0')
  var theFile = theFolder.getFilesByName('Dock to Stock Weekly_Dock to Stock AMP.zip')
  var fileBlob = theFile.next().getBlob()

  fileBlob.setContentType("application/zip")

  var unZippedfile = Utilities.unzip(fileBlob)
  var fileId = SpreadsheetApp.create(unZippedfile).getId();  
  var file = DriveApp.getFileById(fileId);
  DriveApp.addFile(file)
  }
like image 559
Christopher Bell Avatar asked Jul 24 '17 15:07

Christopher Bell


People also ask

Can you unzip files in Google Drive app?

Files by Google allows you to extract and view contents of compressed files. Note: Only . zip files are supported.


1 Answers

SpreadsheetApp.create expects a string and it is used to create new spreadsheet with the passed string. Refer the below code to unzip and upload the file in the Google drive.

Edit 1: The below function will upload the unzipped file in its native format.

function unZipIt() {
  var theFolder = DriveApp.getFolderById('0B9jgHw-WmzvfRS1ZZEhTc3Byak0');
  var theFile = theFolder.getFilesByName('Dock to Stock Weekly_Dock to Stock AMP.zip');
  var fileBlob = theFile.next().getBlob();
  fileBlob.setContentType("application/zip");
  var unZippedfile = Utilities.unzip(fileBlob);
  var newDriveFile = DriveApp.createFile(unZippedfile[0]);
  Logger.log(newDriveFile.getId())
}

Edit 2: The below functions will upload the unzipped file and will convert it into Google drive format

function unZipIt() {
  var theFolder = DriveApp.getFolderById('0B5JsAY8jN1CoWnhxU0Izemp6WW8');
  var theFile = theFolder.getFilesByName('test.zip');
  var fileBlob = theFile.next().getBlob();
  fileBlob.setContentType("application/zip");
  var unZippedfile = Utilities.unzip(fileBlob);
  var newDriveFile = DriveApp.createFile(unZippedfile[0]);
  convertToGoogleDocs(newDriveFile.getId())
}

function convertToGoogleDocs(fileId) {
  try{
    var originalFile = DriveApp.getFileById(fileId);
    var uploadFile = JSON.parse(UrlFetchApp.fetch(
      "https://www.googleapis.com/upload/drive/v2/files?uploadType=media&convert=true", 
      {
        method: "POST",
        contentType: originalFile.getMimeType(),
        payload: originalFile.getBlob().getBytes(),
        headers: {
          "Authorization" : "Bearer " + ScriptApp.getOAuthToken()
        },
        muteHttpExceptions: true
      }
    ).getContentText());

    // Remove the file extension from the new google file name
    var googleFileName = originalFile.setName(originalFile.getName().substr(0, originalFile.getName().lastIndexOf(".")));

    // Update the name of the Google file created from the original file
    DriveApp.getFileById(uploadFile.id).setName(googleFileName);
    var files = DriveApp.getFileById(uploadFile.id);
  }
   catch(e){
      Logger.log(e)
   }
}
like image 153
Ritesh Nair Avatar answered Sep 18 '22 17:09

Ritesh Nair