Reputation: 379
I'm trying to unzip DMARC-reports sent to my e-mail as attachments. It works fine with zip-files, but not with gz-files.
In my code I first get the correct emails by subjects. If the subject is correct this script is run:
var attachments = message.getAttachments();
for(var k in attachments){
var attachment = attachments[k];
var attachmentBlob = attachment.copyBlob();
var vedleggsnavn = attachment.getName();
Logger.log(vedleggsnavn)
var vedleggstype = attachment.getContentType();
Logger.log(vedleggstype)
if(vedleggstype=='application/gzip'){
Logger.log("ja gzip");
var files = Utilities.ungzip(attachmentBlob);
}
if(vedleggstype=='application/zip'){
Logger.log("ja zip");
var files = Utilities.unzip(attachmentBlob);
}
If the type of attachment is a application/zip it is decompressed and saved in my Google Drive. If it's a application/gzip I get an error. This is my log:
I hope someone might help me figure out how to unzip gz-files, I didn't find any tutorials on using ungzip. (I found a place where they wrote gunzip, but it doesn't look like that is right.)
I found this: 'Invalid Argument' error with un-gzipping Blob in Apps script But I didn't understand it.
I was told my code is not sufficient. I thought since it works for zip, it was enough to only post the code where the problem happens, in order to keep it minimal. But now I'll post my complete function:
function lagreVedlegg(){
var folderID= ''; //I deleted my ID
var tittelen = "Report Domain: dotl.no";
var tittelen2 = "Report domain: dotl.no";
var tittelLengde = tittelen.length;
var eldreEnn = new Date(2021,4,21); //huska at telling starter på 0, så måned 4 er mai
var root = DriveApp.getRootFolder();
var parentFolder = DriveApp.getFolderById(folderID);
var antall = 20;
var threads = GmailApp.getInboxThreads(0, antall);
//Logger.log(threads)
for(var i in threads){
var thread = threads[i];
var message = thread.getMessages()[0]; // Get first message
//Logger.log(message)
var tittel = message.getSubject();
var tittelKort = tittel.substring(0,tittelLengde);
Logger.log(tittelKort)
var avsender = message.getFrom();
//Logger.log(avsender)
var dato = message.getDate();
//Logger.log(dato)
//Logger.log(eldreEnn)
if((tittelKort==tittelen || tittelKort==tittelen2) && dato > eldreEnn){
Logger.log(avsender); // Log from address of the message
var attachments = message.getAttachments();
for(var k in attachments){
var attachment = attachments[k];
var attachmentBlob = attachment.copyBlob();
var vedleggsnavn = attachment.getName();
Logger.log(vedleggsnavn)
var vedleggstype = attachment.getContentType();
Logger.log(vedleggstype)
if(vedleggstype=='application/gzip'){
Logger.log("ja gzip");
attachment.setContentType('application/x-gzip');
var attachmentBlob = attachment.copyBlob();
var files = Utilities.ungzip(attachmentBlob);
}
if(vedleggstype=='application/zip'){
Logger.log("ja zip");
var files = Utilities.unzip(attachmentBlob);
}
Logger.log(files)
files.forEach(function(file) {
Logger.log(file)
//Logger.log(parentFolder)
parentFolder.createFile(file);
})
}
thread.moveToArchive();
Logger.log(tittel + "flyttet")
}
}
}
Upvotes: 2
Views: 1104
Reputation: 2861
This seems to be a bug. Utilities.unzip
only seems to work with files with MIME type application/x-gzip
and do not support the modern application/gzip
. There is a bug report on their issue tracker, already. Click the white star (☆) to give it more priority.
As a workaround you can set the content type to application/x-gzip
:
function ungzipAttahcment() {
const message = GmailApp.getMessageById('...')
const attachment = message.getAttachments()[0]
attachment.setContentType('application/x-gzip')
const blob = Utilities.ungzip(attachment)
}
Upvotes: 8