【问题标题】:Zip Multiple Folders in 1 zip - Google Drive Scripts在 1 个 zip 中压缩多个文件夹 - Google Drive Scripts
【发布时间】:2014-04-09 20:59:43
【问题描述】:

我想为 Google Drive 制作一个脚本。我想每周备份我的文件夹并将它们存储在 Google Drive 的另一个文件夹中。 关于每周触发器,我已经确定了,但是我遇到了问题,因为我找不到压缩整个文件夹的方法。 我要压缩的文件夹有多个子文件夹和文档。我尝试在每个文件夹中搜索并制作文件的 zip,但我发现它很复杂,最终我会为一个文件夹创建许多 zip 文件。我到目前为止的代码是这样的:

function zipFolder(pathFolder, filename, destinyPath) {
  var date = Utilities.formatDate(new Date(), "GMT", "ddMMyyyy");
  var destiny = DocsList.getFolder(destinyPath);
  var folder = DriveApp.getFolderById(DocsList.getFolder(pathFolder).getId());
  var zip = Utilities.zip(folder, filename+date+'.zip');
  destiny.createFile(zip);
}

我收到错误消息,它无法压缩文件夹,它必须是一个 blob。我该如何解决这个问题?

谢谢!

【问题讨论】:

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


    【解决方案1】:

    您可以使用此代码:

    function zipFolder(pathFolder, filename, destinyPath) {
      var date = Utilities.formatDate(new Date(), "GMT", "ddMMyyyy");
      var destiny = DocsList.getFolder(destinyPath);
      var folder = DriveApp.getFolderById(DocsList.getFolder(pathFolder).getId());
      var zip = Utilities.zip(getBlobsPath(folder, ''), filename+date+'.zip');
      destiny.createFile(zip);
    }
    
    function getBlobsPath(reFolder, path) {
      var blobs = [];
      var files = reFolder.getFiles();
      while (files.hasNext()) {
        var file = files.next().getBlob();
        file.setName(path+file.getName());
        blobs.push(file);
      }
      var folders = reFolder.getFolders();
      while (folders.hasNext()) {
        var folder = folders.next();
        var fPath = path+folder.getName()+'/';
        blobs.push(Utilities.newBlob([]).setName(fPath)); //comment/uncomment this line to skip/include empty folders
        blobs = blobs.concat(getBlobsPath(folder, fPath));
      }
      return blobs;
    }
    

    【讨论】:

      猜你喜欢
      • 2021-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 2013-12-25
      • 1970-01-01
      相关资源
      最近更新 更多