【问题标题】:Using Google App Script Unzip a file from Google Drive使用 Google App Script 从 Google Drive 解压文件
【发布时间】:2021-07-09 03:36:50
【问题描述】:

我需要一个脚本来解压缩我上传到我的谷歌驱动器的文件并将压缩文件的内容放回我的谷歌驱动器中。

我遇到了麻烦。它运行没有错误,但它上传的文件是空的。我对 Google App Script 非常陌生,因此将不胜感激任何帮助。谢谢。

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

【问题讨论】:

    标签: google-apps-script google-drive-api zip unzip


    【解决方案1】:

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

    编辑1:下面的函数会上传解压后的原始格式文件。

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

    编辑2:以下函数将上传解压后的文件并将其转换为谷歌驱动格式

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-18
      相关资源
      最近更新 更多