-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
60 lines (50 loc) · 1.92 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
document.addEventListener("DOMContentLoaded", () => {
const fileInput = document.getElementById("fileInput");
const extractButton = document.getElementById("extractButton");
const fileList = document.getElementById("fileList");
fileInput.addEventListener("change", (event) => {
const file = event.target.files[0];
if (file && file.name.endsWith(".zip")) {
extractButton.disabled = false;
} else {
extractButton.disabled = true;
alert("Please select a valid .zip file.");
}
});
extractButton.addEventListener("click", async () => {
const file = fileInput.files[0];
if (!file) return;
const zip = new JSZip();
try {
const contents = await zip.loadAsync(file);
fileList.innerHTML = "";
for (const [fileName, fileData] of Object.entries(contents.files)) {
if (fileData.dir) continue; // Skip directories
const fileContent = await zip.file(fileName).async("blob");
// Create a row with file name and download button
const fileRow = document.createElement("div");
fileRow.classList.add("file-row");
// File name display
const fileNameElement = document.createElement("p");
fileNameElement.textContent = fileName;
// Download button
const downloadButton = document.createElement("button");
downloadButton.textContent = "Download";
// Handle download
downloadButton.addEventListener("click", () => {
const link = document.createElement("a");
link.href = URL.createObjectURL(fileContent);
link.download = fileName;
link.click();
});
// Append to file row
fileRow.appendChild(fileNameElement);
fileRow.appendChild(downloadButton);
// Append row to file list
fileList.appendChild(fileRow);
}
} catch (error) {
alert("Error extracting files: " + error.message);
}
});
});