我想通過 Google Apps 腳本在 Google Drive 中下載一個 zip 檔案。
下載帶有以下代碼的示例 zip 檔案并將其保存到谷歌驅動器的檔案夾中后。
const exampleUrl = "https://www.learningcontainer.com/wp-content/uploads/2020/05/sample-zip-file.zip";
var response = UrlFetchApp.fetch(exampleUrl);
var parentFolder = DriveApp.getFolderById('1aba-tnQZxZMN7DN52eAywTU-Xs-eqOf4');
parentFolder.createFile('sample_CT.zip', response.getContentText()); // doesn't work
parentFolder.createFile('sample_C.zip', response.getContent()); // doesn't work
parentFolder.createFile('sample_B.zip', response.getBlob()); // doesn't work
parentFolder.createFile('sample.zip', response); // doesn't work
在我的機器上下載它后,我嘗試使用unzip實用程式解壓縮,但上述所有版本都給了我以下內容:
> unzip sample_CT.zip
Archive: sample_CT.zip
End-of-central-directory signature not found. Either this file is not
a zipfile, or it constitutes one disk of a multi-part archive. In the
latter case the central directory and zipfile comment will be found on
the last disk(s) of this archive.
unzip: cannot find zipfile directory in one of sample_CT.zip or
sample_CT.zip.zip, and cannot find sample_CT.zip.ZIP, period.
在圖片中,我正在比較損壞的 zip 檔案(上)和正確的(下):
破碎的:
PKu??Ω??ΩP
sample.txtUT
??Ωb??Ω^??Ωb??Ω^??Ωb??Ω^ux??Ω??ΩE??Ω1R??Ω0??Ω??Ω??ΩQ??Ω0??ΩUz. ,
??Ω??ΩXK??Ω!??Ω??Ω2??Ω??ΩV#??Ω6??Ω:
??Ω??ΩM??Ω
??Ω??Ω#ux??Ωh??ΩttPkHT—∫??ΩH??Ωb ??Ω:N??Ω>m??Ω??Ω??Ω??Ω??Ωh??Ω`{??Ωc??Ω0??ΩA??Ω??Ω(yh??Ω??Ω??Ω&??Ω??Ω??Ω{??ΩU~??ΩY??Ω~??Ω??Ω??Ω??Ω??ΩHA??Ω??Ω??Ω??Ω??Ωk8w??Ωp??Ω??Ω??Ω6??ΩIk??Ω??Ωk??Ω??Ω?k"?OJx??Ω??Ω(n?≤og??Ω_??ΩtPK[??Ωc??ΩPKu??Ω??ΩP[??Ωc??Ω
??Ω??Ωsample.txtUT
??Ωb??Ω^??Ωb??Ω^??Ωb??Ω^ux??Ω??ΩPKX
正確的:
PKu?ì??P
sample.txtUT
√áb?±^√áb?±^√áb?±^ux√?√?E?è1R√?0√a?ú√¢Q√?0?πUz. ,
√é√?XK√?!?∑√?2√∞?áV#√≠??6?ú:
?£√?M√?
√??¥#ux?≠h√∞???∏ttPkHT√??∫√≤H?≤b ??:N??>m√¥??√a√§?íh?ò`{√∫c√?0√?A√μ??(yh?????a&√?√¥√è{√ΩU~?∞Y√?~?ì??√?√≤√?HA??√?√o√ó√∑k8w√èp√π√??π6√?Ik?a√∞k?§√o?k"?OJx?∫√ò(n√′?≤?og??_√?tPK[?∞c??PKu?ì??P[?∞c??
?¥??sample.txtUT
√áb?±^√áb?±^√áb?±^ux√?√?PKX
我的文本編輯器中的影像

正如您在圖片中看到的(上面的檔案片段),一些符號不同。我不知道為什么UrlFetch在下載 zip 檔案時會更改某些位元組。UrlFetch最重要的是它占用更多空間后的檔案。
uj5u.com熱心網友回復:
這是因為腳本正在將其轉換為string. Folder.createFile()接受 a blob,但它應該是唯一的論點。如果它作為第二個引數傳遞,則其他方法簽名(例如Folder.createFile(name:string, content:string))優先并Blob轉換為String與方法簽名匹配。
parentFolder.createFile(response.getBlob().setName('TheMaster.zip'))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/512530.html
