While zipping images and HTML files works smoothly, I encountered an issue when trying to add font files for CSS. The font file is only 1kb in size, but it cannot be opened.
Even after attempting to zip the font without any other files, the problem persisted.
let zip = new JSZip()
let imageURLCount = 0
let fontFileCount = 0
let imageURLs = [
'http:site.com/image1.jpg',
'http:site.com/image2.jpg',
'http:site.com/image3.jpg',
'http:site.com/image4.jpg'
]
let fontFiles = [
'http:site.com/fontFile1.ttf',
'http:site.com/fontFile2.ttf',
'http:site.com/fontFile3.ttf',
'http:site.com/fontFile4.ttf'
]
// zipping images
imageURLs.forEach((url, i) => {
JSZipUtils.getBinaryContent(url, (error, data) => {
if (error) {
throw error
}
// naming the zipped file
imageFileName = 'image_'+i+'.jpg'
// creating a folder for images
zip.folder('images')
.file(imageFileName, data,{binary: true})
imageURLCount++
if (imageURLCount === imageURLs.length) {
zipComplete(imageURLCount,fontFileCount)
}
})
}) // end of forEach loop for imageURLs[]
// zipping font files
fontFiles.forEach((fontFile, i) => {
JSZipUtils.getBinaryContent(fontFile, (error, data) => {
if (error) {
throw error
}
// naming the zipped file
fileName = 'font_'+i+'.ttf'
zip.file(fileName, data, {binary:true})
fontFileCount++
if (fontFileCount === fontFiles.length) {
zipComplete(imageURLCount,fontFileCount)
}
})
}) // end of forEach loop for fontFiles[]
// Note: zipComplete(imageURLCount,fontFileCount) checks if both arrays have been fully iterated to trigger the 'file-saver' SaveAs()
How can I properly compress the font files using JSZip? Is it even feasible to zip fonts with JSZip?