使用Google App脚本从Google云端硬盘解压缩文件 [英] Using Google App Script Unzip a file from Google Drive

查看:225
本文介绍了使用Google App脚本从Google云端硬盘解压缩文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个脚本来解压缩已上传到google驱动器的文件,并将zip文件的内容放回我的google驱动器中.

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.

我遇到了麻烦.它运行无误,但上传的文件为空.我是Google App Script的新手,因此非常感谢您的帮助.谢谢.

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)
  }

推荐答案

SpreadsheetApp.create需要一个字符串,并用于使用传递的字符串创建新的电子表格.请参考以下代码以解压缩文件并将其上传到Google驱动器中.

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.

以下功能将以原始格式上传解压缩的文件.

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())
}

以下功能将上传解压缩的文件,并将其转换为Google驱动器格式

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)
   }
}

这篇关于使用Google App脚本从Google云端硬盘解压缩文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆