使用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 */
更多推荐
发布评论