我正在从一个网站上下载一个.zip。它包含一个.txt文件。我想访问txt中的数据并将其写入电子表格。我可以直接访问它而不解压zip,或者解压zip,将txt保存到Google Drive文件夹,并在保存后访问它。
当我使用Utilities.unzip()
时,我永远不能让它解压缩文件,并且通常以“无效参数”错误结束。在下面的代码中,else
之前的最后一节包含解压缩命令。它成功地将文件保存到正确的Google文件夹,但我无法将其解压缩。
function myFunction() {
// define where to gather data from
var url = '<insert url here>';
var filename = "ReportUploadTesting05.zip";
var response = UrlFetchApp.fetch(url, {
// muteHttpExceptions: true,
// validateHttpsCertificates: false,
followRedirects: true // Default is true anyway.
});
// get spreadsheet for follow up info
var Sp = SpreadsheetApp.getActiveSpreadsheet();
if (response.getResponseCode() === 200) {
// get folder details of spreadsheet for saving future files
var folderURL = getParentFolder(Sp);
var folderID = getIdFromUrl(folderURL);
var folder = DriveApp.getFolderById(folderID);
// save zip file
var blob = response.getBlob();
var file = folder.createFile(blob);
file.setName(filename);
file.setDescription("Downloaded from " + url);
var fileID = file.getId();
Logger.log(fileID);
Logger.log(blob)
// extract zip (not working)
file.setContent('application/zip')
var fileUnzippedBlob = Utilities.unzip(file); // invalid argument error occurs here
var filename = 'unzipped file'
var fileUnzipped = folder.createFile(fileUnzippedBlob)
fileUnzipped.setName(filename)
}
else {
Logger.log(response.getResponseCode());
}
}
我已经按照Utilities page上的说明操作了。我可以让他们的例子起作用。我曾尝试在我的电脑上创建.zip,将其上传到Google Drive,但尝试打开它失败了。显然,使用解压包有一些微妙之处,但我没有注意到。
你能帮我理解一下吗?
发布于 2019-09-19 05:16:51
我在测试中遇到了同样的“无效参数”错误,所以不要使用:
file.setContent('application/zip')
我使用:
file.setContentTypeFromExtension();
这就解决了我的问题。此外,正如@tukusejssirs提到的,一个压缩文件可以包含多个文件,因此unzip()返回一个blobs数组(如文档中所述的here。这意味着您需要遍历文件,或者如果您知道只有一个文件,则显式引用它在数组中的位置,如下所示:
var fileUnzipped = folder.createFile(fileUnzippedBlob[0])
下面是我的整个脚本,它涵盖了这两个问题:
/**
* Fetches a zip file from a URL, unzips it, then uploads a new file to the user's Drive.
*/
function uploadFile() {
var url = '<url goes here>';
var zip = UrlFetchApp.fetch('url').getBlob();
zip.setContentTypeFromExtension();
var unzippedFile = Utilities.unzip(zip);
var filename = unzippedFile[0].getName();
var contentType = unzippedFile[0].getContentType();
var csv = unzippedFile[0];
var file = {
title: filename,
mimeType: contentType
};
file = Drive.Files.insert(file, csv);
Logger.log('ID: %s, File size (bytes): %s', file.id, file.fileSize);
var fileId = file.id;
// Move the file to a specific folder within Drive (Link: https://drive.google.com/drive/folders/<folderId>)
var folderId = '<folderId>';
var folder = DriveApp.getFolderById(folderId);
var driveFile = DriveApp.getFileById(fileId);
folder.addFile(driveFile);
}
发布于 2018-08-09 23:13:18
我想你的问题的答案可以在这里找到。Is there a size limit to a blob for Utilities.unzip(blob) in Google Apps Script? 如果下载超过100MB,则无法下载完整文件。因此,它将不会是正确的zip格式。抛出“无法解压缩文件”错误。
发布于 2019-02-05 02:17:50
我认为从文件(在本例中是.zip
文件)创建blob需要.next()
;否则它不适用于我。
还要注意,.zip
文件可能包含多个文件,因此我包含了一个for
循环。
无论如何,我的工作/测试解决方案/脚本如下:
function unzip(folderName, fileZipName){
// Variables
// var folderName = "folder_name";
// var fileZipName = "file_name.zip";
var folderId = getFolderId(folderName);
var folder = DriveApp.getFolderById(folderId);
var fileZip = folder.getFilesByName(fileZipName);
var fileExtractedBlob, fileZipBlob, i;
// Decompression
fileZipBlob = fileZip.next().getBlob();
fileZipBlob.setContentType("application/zip");
fileExtractedBlob = Utilities.unzip(fileZipBlob);
for (i=0; i < fileExtractedBlob.length; i++){
folder.createFile(fileExtractedBlob[i]);
}
}
https://stackoverflow.com/questions/48406765
复制相似问题