Zipping images and HTML files works fine but when adding the font file for the CSS; the font file is 1kb in size and can't open.
I've tried to just zip the font without any other files and still the same problem.
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'
]
// zip images
imageURLs.forEach((url, i) => {
JSZipUtils.getBinaryContent(url, (error, data) => {
if (error) {
throw error
}
// zip file name
imageFileName = 'image_'+i+'.jpg'
// create images folder
zip.folder('images')
.file(imageFileName, data,{binary: true})
imageURLCount++
if (imageURLCount === imageURLs.length) {
zipComplete(imageURLCount,fontFileCount)
}
})
}) // end imageURLs[] forEach
// zip font files
fontFiles.forEach((fontFile, i) => {
JSZipUtils.getBinaryContent(fontFile, (error, data) => {
if (error) {
throw error
}
// zip file name
fileName = 'font_'+i+'.ttf'
zip.file(fileName, data, {binary:true})
fontFileCount++
if (fontFileCount === fontFiles.length) {
zipComplete(imageURLCount,fontFileCount)
}
})
}) // end fontFiles[] forEach
// PS: zipComplete(imageURLCount,fontFileCount) checks if both arrays looped to the end and then trigger the 'file-saver' SaveAs()
How would I go about zipping the fonts or is it not possible to zip fonts with JSZip?
I expected to edit your sample and find a problem in your code but it turns out that nothing is wrong. Here is a working example that successfully zips a .ttf file & then downloads the zip.
I would guess that the problem lies in the content that is being returned from your .ttf URIs.
let zip = new JSZip()
let imageURLCount = 0
let fontFileCount = 0
let imageURLs = []
let fontFiles = [
'https://fontlibrary.org/assets/fonts/symbola/cf81aeb303c13ce765877d31571dc5c7/7d8d51a2e1b57d59075325384458fac6/SymbolaRegular.ttf'
]
// zip font files
fontFiles.forEach((fontFile, i) => {
JSZipUtils.getBinaryContent(fontFile, (error, data) => {
if (error) {
throw error
}
// zip file name
fileName = 'font_' + i + '.ttf'
zip.file(fileName, data, {
binary: true
})
fontFileCount++
if (fontFileCount === fontFiles.length) {
zipComplete(imageURLCount, fontFileCount)
}
})
}) // end fontFiles[] forEach
function zipComplete(imageURLCount, fontFileCount) {
zip.generateAsync({
type: "blob"
})
.then(function(content) {
// see FileSaver.js
saveAs(content, "example.zip");
});
}
// PS: zipComplete(imageURLCount,fontFileCount) checks if both arrays looped to the end and then trigger the 'file-saver' SaveAs()
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.2.2/jszip.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip-utils/0.1.0/jszip-utils.js"></script>
<script src="http://cdn.jsdelivr.net/g/filesaver.js"></script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With