使用apps脚本在Google云端硬盘中创建一个zip文件

时间:2012-11-06 20:41:29

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

我在google drive文件夹中有一个包含少量文件的文件夹。我想制作一个谷歌应用程序脚本,压缩该文件夹中的所有文件,并在同一文件夹中创建zip文件。我发现了一个有Utilities.zip()函数的视频。但是没有API参考。如何使用它?提前谢谢。

5 个答案:

答案 0 :(得分:10)

实际上它比那更容易。文件已经是Blob(任何具有getBlob()的东西都可以传递给任何需要Blob的函数)。所以代码看起来像这样:

var folder = DocsList.getFolder('path/to/folder');
folder.createFile(Utilities.zip(folder.getFiles(), 'newFiles.zip'));

此外,如果您在文件夹中有多个具有相同名称的文件,它将无法使用... Google云端硬盘文件夹支持该功能,但Zip文件不支持。

使用多个具有相同名称的文件:

var folder = DocsList.getFolder('path/to/folder');
var names = {};
folder.createFile(Utilities.zip(folder.getFiles().map(function(f){
  var n = f.getName();
  while (names[n]) { n = '_' + n }
  names[n] = true;
  return f.getBlob().setName(n);
}), 'newFiles.zip'));

答案 1 :(得分:10)

由于DocsList已被弃用,您可以使用以下代码压缩包含文件和子文件夹的整个文件夹,并保留其结构:

var folder = DriveApp.getFolderById('<YOUR FOLDER ID>');
var zipped = Utilities.zip(getBlobs(folder, ''), folder.getName()+'.zip');
folder.getParents().next().createFile(zipped);

function getBlobs(rootFolder, path) {
  var blobs = [];
  var files = rootFolder.getFiles();
  while (files.hasNext()) {
    var file = files.next().getBlob();
    file.setName(path+file.getName());
    blobs.push(file);
  }
  var folders = rootFolder.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(getBlobs(folder, fPath));
  }
  return blobs;
}

getBlobs函数生成文件夹中所有文件的数组,并将每个文件名更改为它在拉链时保持结构的相对路径。

要压缩包含多个具有相同名称的项目的文件夹,请使用此getBlob功能:

function getBlobs(rootFolder, path) {
  var blobs = [];
  var names = {};
  var files = rootFolder.getFiles();
  while (files.hasNext()) {
    var file = files.next().getBlob();
    var n = file.getName();
    while(names[n]) { n = '_' + n }
    names[n] = true;
    blobs.push(file.setName(path+n));
  }
  names = {};
  var folders = rootFolder.getFolders();
  while (folders.hasNext()) {
    var folder = folders.next();
    var n = folder.getName();
    while(names[n]) { n = '_' + n }
    names[n] = true;
    var fPath = path+n+'/';
    blobs.push(Utilities.newBlob([]).setName(fPath)); //comment/uncomment this line to skip/include empty folders
    blobs = blobs.concat(getBlobs(folder, fPath));
  }
  return blobs;
}

答案 2 :(得分:2)

确实没有API参考。您可以在Apps脚本issue tracker上打开有关此问题的问题请求。但是从代码完成的内容中推断,这是我的理解:

var folder = DocsList.getFolder('path/to/folder');
var files = folder.getFiles();
var blobs = [];
for( var i in files )
  blobs.push(files[i].getBlob());
var zip = Utilities.zip(blobs, 'newFiles.zip');
folder.createFile(zip);

但是我没有测试过这段代码,所以我不知道它是否会起作用。此外,它可能仅适用于未转换为Google格式的文件,或者仅适用于那些或其子集的文件。好吧,如果您试一试并找到了解决方法,请与我们分享。你肯定会遇到的一个限制是文件大小,如果zip文件变得“太大”,它可能不会起作用......是的,你也必须测试这个限制。

答案 3 :(得分:1)

我能够使用@Hafez发布的代码,但我需要修改它,因为它不适合我。我添加了前三行,因为我需要文件夹ID,这是一个字符串值,而不是文件夹的名称。

var folderName = DriveApp.getFoldersByName("<folderName>");
var theFolder = folderName.next();
var folderID =theFolder.getId();
var folder = DriveApp.getFolderById(folderID);
var zipped = Utilities.zip(getBlobs(folder, ''), folder.getName()+'.zip');
folder.getParents().next().createFile(zipped);

function getBlobs(rootFolder, path) {
  var blobs = [];
  var files = rootFolder.getFiles();
  while (files.hasNext()) {
    var file = files.next().getBlob();
    file.setName(path+file.getName());
    blobs.push(file);
  }
  var folders = rootFolder.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(getBlobs(folder, fPath));
  }
  return blobs;
}

我遇到的唯一奇怪的事情是,当我运行脚本时,它会显示TypeError: Cannot call method "getFiles" of undefined. (line 10, file "Code")。当我碰巧看到这个脚本所在的地方时,还有5个完整的zip文件。它有效,但我仍然得到了这个错误。奇怪......但这段代码对我有用。感谢这个主题上的每个人。干杯!

答案 4 :(得分:0)

如果 Hafez 解决方案没有奏效,您会收到此错误

<块引用>

类型错误:无法读取未定义的属性“getFiles”

尝试这样做

/**
 * Creates a zipFile of the mentioned document ID and store it in Drive. You can search the zip by folderName.zip
 */

function createAndSendDocument() {
    var files = DriveApp.getFolderById("DOCUMENT ID CAN BE FIND IN THE URL WHEN A DOCUMENT IS OPENED");
    var folder = files;
    var zipped = Utilities.zip(getBlobs(folder, ''), folder.getName() + '.zip');
    folder.getParents().next().createFile(zipped);

}

function getBlobs(rootFolder, path) {
    var blobs = [];
    var files = rootFolder.getFiles();

    while (files.hasNext()) {
        var file = files.next().getBlob();
        file.setName(path+file.getName());
        blobs.push(file);
    }

    var folders = rootFolder.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(getBlobs(folder, fPath));
    }

    return blobs;
}