I have base64 string that is a zip file. I need to extract the file the zip file and to do so I was trying to utilize a library noted in an answer here on Stack Overflow called fflate:
attachmentsFlat.forEach(async (attachment) => {
attachment = await emailStore.getAttachment(props.mailbox, attachment.email_id, attachment.id)
if(attachment.file_name.split('.').pop() == 'zip') {
const decompressed = fflate.decompressSync(attachment.content_bytes);
console.log(decompressed)
}
However, this results in an error:
fflate.js?v=2017d673:285 Uncaught (in promise) TypeError: dat.subarray is not a function
at inflt (fflate.js?v=2017d673:285:21)
at inflateSync (fflate.js?v=2017d673:1103:10)
at Module.decompressSync (fflate.js?v=2017d673:1421:160)
at EmailFilesAssignment.vue:150:37
If this library does not work I am open to other libraries that can take base64 and unzip the files. I just cannot find many example of unzip files but more so zipping and using base64 is a special example I cannot find.
I also attempted to do this via JSZip but I am unsure how to get the files out asynchronously. I have tried the following:
attachmentsFlat.forEach(async (attachment) => {
attachment = await emailStore.getAttachment(props.mailbox, attachment.email_id, attachment.id)
if(attachment.file_name.split('.').pop() == 'zip') {
const zipFiles = await uncompressZipAttachment(attachment)
console.log(zipFiles) // shows [] and 8 entries in console
console.log(zipFiles[0]) //always returns undefined
}
})
const dataURLtoFile = (base64String: string, contentType: string | undefined, filename: string) => {
let mime = contentType
let bstr = atob(base64String)
let n = bstr.length
let u8arr = new Uint8Array(n)
while(n--){
u8arr[n] = bstr.charCodeAt(n);
}
return new File([u8arr], filename, {type:mime});
}
const uncompressZipAttachment = async (attachment : FileAttachment) : Promise<any> => {
let files : File[] = []
const decompressedZipFiles = await jsZip.loadAsync(dataURLtoFile(attachment.content_bytes, attachment.content_type, attachment.file_name))
decompressedZipFiles.forEach(async (filename) => {
let fileData = await decompressedZipFiles.files[filename].async("blob");
files.push(new File([fileData], filename))
})
return files
}