Created
December 8, 2020 08:04
-
-
Save sticks-stuff/fd4ca155fc9397968762e70c658ffc37 to your computer and use it in GitHub Desktop.
Requires jszip and FileSaver
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var urls = [ | |
'./song.ini', | |
'./notes.chart', | |
'./album.png', | |
'./ch.dat', | |
'./song.ogg', | |
]; | |
var images = []; | |
var counter = 0; | |
// From http://stackoverflow.com/questions/6150289/how-to-convert-image-into-base64-string-using-javascript | |
async function getFileFromUrl(url, callback){ | |
const response = await fetch(url); | |
const data = await response.blob(); | |
var req = new XMLHttpRequest(); | |
req.open('HEAD', url); | |
req.onreadystatechange = function() { | |
var header = req.getResponseHeader('Content-Disposition'); | |
var filename = header.match(/filename="(.+)"/)[1]; // image.jpg | |
console.log(filename); | |
blobToBase64(data, function (dataURL) { callback(dataURL, url, filename); }) | |
}; | |
req.send(null); | |
console.log(data); | |
} | |
function createArchive(images){ | |
// Use jszip | |
var zip = new JSZip(); | |
var img = zip.folder("images"); | |
for (var i=0; i<images.length; i++) { | |
var commaIdx = images[i].data.indexOf(","); | |
img.file(images[i].filename, images[i].data.slice(commaIdx + 1), {base64: true}); | |
} | |
zip.generateAsync({type:"blob"}).then(function(file){ | |
saveAs(file, "images.zip"); | |
}) | |
} | |
var blobToBase64 = function(blob, callback) { | |
var reader = new FileReader(); | |
reader.onload = function() { | |
var dataUrl = reader.result; | |
var base64 = dataUrl.split(',')[1]; | |
callback(base64); | |
}; | |
reader.readAsDataURL(blob); | |
}; | |
for (var i = 0; i<urls.length; i++) { | |
getFileFromUrl(urls[i], function (base64Img, url, filename) { | |
images.push({ | |
url: url, | |
data: base64Img, | |
filename: filename, | |
}); | |
counter++; | |
console.log("counter" + counter); | |
console.log(images); | |
console.log(urls.length); | |
if (counter >= (urls.length * 2)) { | |
createArchive(images); | |
} | |
}); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment