Is there a way to create a zip file from multiple files on google drive with the API?

后端 未结 1 1503
执念已碎
执念已碎 2021-01-14 01:50

The Google Drive web interface allows you to download a single .zip file if you download a directory. However, I find no way to do that with the API. Is it possible to creat

相关标签:
1条回答
  • 2021-01-14 02:29

    How about this workaround?

    Unfortunately, there are no APIs for directly creating a zip file from outside in Google APIs. But I think that there is a workaround. There is zip() method of Class Utilities in Google Apps Script (GAS). And there is Web Apps as a service for using GAS from outside. I think that what you want to do can be achieved by combining them.

    Flow :

    1. Create a GAS script for creating zip files using zip() method of Class Utilities.
    2. Deploy the script as Web Apps.
    3. You can launch the script from outside using GET and POST method of your applications. When you access Web Apps, also you can give some parameters and data.

    Reference :

    • zip() method in Class Utilities
    • Web Apps

    If this was not useful for you, I'm sorry.

    Edit :

    How about a following sample script? This is a simple sample script for creating a zip file for files in a folder.

    • When Google Docs of Spreadsheet, Document and Slide are included in the folder, they are converted to Excel, Word and Powerpont format, respectively.
    • When standalone scripts are included in a folder, they are converted to text data.
    • Other types (images, text data and so son) are not converted.

    These are the same to the web interface.

    In order to use this script

    Please do the following flow.

    1. Copy and paste the sample script to the script editor. And save it.
    2. Deploy Web Apps
      • On script editor
        • Publish -> Deploy as web app
        • At "Project version", create new version.
        • At "Execute the app as:", select "Me".
        • At "Who has access to the app:", select "Anyone, even anonymous".
        • Click Deploy button.
    3. Test the Web Apps using curl command as follows.
      • If you can get the file ID of created zip file, it means that the script works.

    Sample script :

    function doGet(e) {
      var files = DriveApp.getFolderById(e.parameter.folderid).getFiles();
      var fileIds = [];
      while (files.hasNext()) {
        fileIds.push(files.next().getId());
      }
      return ContentService.createTextOutput(zipping(fileIds));
    }
    
    function zipping(fileIds) {
      var zipfilename = "sample.zip";
      var blobs = [];
      var mimeInf = [];
      var accesstoken = ScriptApp.getOAuthToken();
      fileIds.forEach(function(e) {
          try {
              var file = DriveApp.getFileById(e);
              var mime = file.getMimeType();
              var name = file.getName();
          } catch (er) {
              return er
          }
          var blob;
          if (mime == "application/vnd.google-apps.script") {
              blob = UrlFetchApp.fetch("https://script.google.com/feeds/download/export?id=" + e + "&format=json", {
                method: "GET",
                headers: {"Authorization": "Bearer " + accesstoken},
                muteHttpExceptions: true
              }).getBlob().setName(name);
          } else if (~mime.indexOf('google-apps')) {
              mimeInf =
                  mime == "application/vnd.google-apps.spreadsheet" ? ["application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", name + ".xlsx"] : mime == "application/vnd.google-apps.document" ? ["application/vnd.openxmlformats-officedocument.wordprocessingml.document", name + ".docx"] : mime == "application/vnd.google-apps.presentation" ? ["application/vnd.openxmlformats-officedocument.presentationml.presentation", name + ".pptx"] : ["application/pdf", name + ".pdf"];
              blob = UrlFetchApp.fetch("https://www.googleapis.com/drive/v3/files/" + e + "/export?mimeType=" + mimeInf[0], {
                  method: "GET",
                  headers: {"Authorization": "Bearer " + accesstoken},
                  muteHttpExceptions: true
              }).getBlob().setName(mimeInf[1]);
          } else {
              blob = UrlFetchApp.fetch("https://www.googleapis.com/drive/v3/files/" + e + "?alt=media", {
                  method: "GET",
                  headers: {"Authorization": "Bearer " + accesstoken},
                  muteHttpExceptions: true
              }).getBlob().setName(name);
          }
          blobs.push(blob);
      });
      var zip = Utilities.zip(blobs, zipfilename);
      return DriveApp.createFile(zip).getId();
    }
    

    Sample curl command :

    curl -L "https://script.google.com/macros/s/#####/exec?folderid=### folder ID ###"
    

    Note :

    • If you modified the script, please redeploy Web Apps as a new version. By this, the latest script is reflected.
    • As a limitation of the sample script, this sample script checks only files in a folder. If you want to retrieve folders in a folder, you can see the sample script here.
    • After you got the file ID of created ZIP file, you can download it using Drive API as you know.

    If this was useful for you, I'm glad.

    0 讨论(0)
提交回复
热议问题