-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
36 lines (31 loc) · 1.54 KB
/
script.js
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
function zipFolder() {
const folderInput = document.getElementById('folderInput');
const statusText = document.getElementById('status');
if (folderInput.files.length === 0) {
statusText.textContent = 'กรุณาเลือกโฟลเดอร์ที่จะบีบอัด!';
return;
}
const folderName = folderInput.files[0].webkitRelativePath.split('/')[0];
const zip = new JSZip();
for (const file of folderInput.files) {
const pathParts = file.webkitRelativePath.split('/');
const relativePath = pathParts.slice(1).join('/');
if (file.type === '') {
zip.folder(relativePath);
} else {
zip.file(relativePath, file);
}
}
zip.generateAsync({ type: 'blob' }).then(function (blob) {
const zipFileName = `${folderName}.zip`;
const downloadLink = document.createElement('a');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = zipFileName;
downloadLink.click();
URL.revokeObjectURL(downloadLink.href);
statusText.textContent = 'โฟลเดอร์ถูกบีบอัดเรียบร้อยเเล้ว! ';
}).catch(function (error) {
console.error('เกิดข้อผิดพลาดขึ้น :', error);
statusText.textContent = 'เกิดข้อผิดพลาดระหว่างบีบอัด กรุณาลองอีกครั้งนะ.';
});
}