使用Google Apps脚本实用程序时如何保留文件夹结构.unzip()(How do I preserve folder structure when using Google Apps Scri

编程入门 行业动态 更新时间:2024-10-26 02:27:49
使用Google Apps脚本实用程序时如何保留文件夹结构.unzip()(How do I preserve folder structure when using Google Apps Script utilities.unzip())

在Google Apps脚本中使用utilities.unzip()时,如何保留文件夹结构?

这就是我目前使用的:

var files = Utilities.unzip(zipFile); for (var i=0; i<files.length; i++) { folder.createFile(files[i]); }

但是,它正在创建具有完整压缩路径的文件,作为其名称,在主文件夹中是平的,而不是将文件解压缩到预期的子文件夹中。

How do I preserve folder structure when using utilities.unzip() in a Google Apps Script?

This is what I am currently using:

var files = Utilities.unzip(zipFile); for (var i=0; i<files.length; i++) { folder.createFile(files[i]); }

However, it is creating the files with their full zipped path, as their name, flat in the main folder, rather then extracting the files into expected sub-folders.

最满意答案

以下代码可以帮助或至少为您提供一些想法:

/* CODE FOR DEMONSTRATION PURPOSES */
function myunzip() {
  var fullpath = [], filename;
  var files = [
    'folder0/path/to/file/file00.txt',
    'folder1/path/to/file/file01.txt',
    'folder2/path/to/file/file02.txt',
    'file03.txt'
  ];

  files.forEach(function(file) {
    fullpath = getFullPath(file);
    filename = fullpath.pop();
    getDriveFolder(fullpath).createFile(filename, fullpath.join('/'));
  });

  function getFullPath(file) {
    return file.replace(/^\/*|\/*$/g, '').replace(/^\s*|\s*$/g, '').split('/');
  }

  // http://ctrlq.org/code/19925-google-drive-folder-path
  function getDriveFolder(fullpath) {
    var name, folder, search;
    folder = DriveApp.getRootFolder();
    for (var subfolder in fullpath) {
      name = fullpath[subfolder];
      search = folder.getFoldersByName(name);
      folder = search.hasNext() ? search.next() : folder.createFolder(name);
    }
    return folder;
  }
}
/* CODE FOR DEMONSTRATION PURPOSES */

The following code can help or at least give you some ideas:

/* CODE FOR DEMONSTRATION PURPOSES */
function myunzip() {
  var fullpath = [], filename;
  var files = [
    'folder0/path/to/file/file00.txt',
    'folder1/path/to/file/file01.txt',
    'folder2/path/to/file/file02.txt',
    'file03.txt'
  ];

  files.forEach(function(file) {
    fullpath = getFullPath(file);
    filename = fullpath.pop();
    getDriveFolder(fullpath).createFile(filename, fullpath.join('/'));
  });

  function getFullPath(file) {
    return file.replace(/^\/*|\/*$/g, '').replace(/^\s*|\s*$/g, '').split('/');
  }

  // http://ctrlq.org/code/19925-google-drive-folder-path
  function getDriveFolder(fullpath) {
    var name, folder, search;
    folder = DriveApp.getRootFolder();
    for (var subfolder in fullpath) {
      name = fullpath[subfolder];
      search = folder.getFoldersByName(name);
      folder = search.hasNext() ? search.next() : folder.createFolder(name);
    }
    return folder;
  }
}
/* CODE FOR DEMONSTRATION PURPOSES */

                    
                     
          

更多推荐

本文发布于:2023-07-19 01:03:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1169660.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:脚本   文件夹   实用程序   结构   Apps

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!