-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjszipdownload.html
74 lines (64 loc) · 2.15 KB
/
jszipdownload.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<html>
<head>
<script src="jszip/dist/jszip.js"></script>
<script src="jszip/vendor/FileSaver.js"></script>
<script>
var fileURLs = ['https://raw.githubusercontent.com/FortuneStreetModding/fortunestreetmodding.github.io/main/_maps/Abwarten/Abwarten.yaml', 'https://raw.githubusercontent.com/FortuneStreetModding/fortunestreetmodding.github.io/main/_maps/Abwarten/Abwarten0.frb'];
var zip = new JSZip();
var count = 0;
downloadFile(fileURLs[count], onDownloadComplete);
function downloadFile(url, onSuccess) {
var xhr = new XMLHttpRequest();
xhr.onprogress = calculateAndUpdateProgress;
xhr.open('GET', url, true);
xhr.responseType = "blob";
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
if (onSuccess) onSuccess(xhr.response);
}
}
xhr.send();
}
function onDownloadComplete(blobData){
if (count < fileURLs.length) {
blobToBase64(blobData, function(binaryData){
// add downloaded file to zip:
var fileName = fileURLs[count].substring(fileURLs[count].lastIndexOf('/')+1);
zip.file(fileName, binaryData, {base64: true});
if (count < fileURLs.length -1){
count++;
downloadFile(fileURLs[count], onDownloadComplete);
}
else {
// all files have been downloaded, create the zip
var promise = zip.generateAsync({type:"blob"});
promise.then(
function(blob) {
saveAs(blob, "hello.zip");
},
);
}
});
}
}
function blobToBase64(blob, callback) {
var reader = new FileReader();
reader.onload = function() {
var dataUrl = reader.result;
var base64 = dataUrl.split(',')[1];
callback(base64);
};
reader.readAsDataURL(blob);
}
function calculateAndUpdateProgress(evt) {
if (evt.lengthComputable) {
// ...
}
}
</script>
</head>
<body>
<script>
</script>
</body>
</html>