首页 > 解决方案 > Google 脚本 - 解压缩 Mimetype 应用程序/x-zip

问题描述

我经常下载一个 .zip 文件(不是由我创建的),它是 Mimetype "application/x-zip",它没有被 Utilities.unzip(file) 处理,并出现以下错误。我希望有人能够帮助我解析这个文件。

谢谢。

var thisFile = DriveApp.getFileById(newestFileId);  
   //Successfully gets the file as verified by other processes
var thisBlob = thisFile.getBlob();
var createMe = thisFolder.createFile("workingb.zip", thisBlob, "application/zip");  
   //Creates a 4-byte file with the text "Blob"
var createMe = thisFolder.createFile("workingf.zip", thisFile, "application/zip");  
   //Creates an 11-byte file with the text the same as the filename.ext
Logger.log(thisFile.getMimeType());  
   // "application/x-zip"
 var test1 = thisFile.getAs("application/zip");  
   //Exception: Converting from application/x-zip to application/zip is not supported
var thisUnzip = Utilities.unzip(test1); 
   //Exception: Invalid argument 

标签: google-apps-script

解决方案


这个改装怎么样?我认为您的情况有两种模式。

模式一:

如果文件名的扩展名是.zip,那么这个修改怎么样?

修改后的脚本:
var thisFile = DriveApp.getFileById(newestFileId);
var thisBlob = thisFile.getBlob();
var convertedBlob = thisBlob.setContentTypeFromExtension();
var thisUnzip = Utilities.unzip(convertedBlob);

模式二:

如果文件名没有扩展名或扩展名是除 之外的值.zip,那么这个修改怎么样?

修改后的脚本:
var thisFile = DriveApp.getFileById(newestFileId);  
var thisBlob = thisFile.getBlob();
var convertedBlob = thisBlob.setContentType("application/zip");
var thisUnzip = Utilities.unzip(convertedBlob);

参考:

如果这些不是你想要的,我很抱歉。


推荐阅读