replace zip.js with JSzip

Signed-off-by: Fredrik Adelöw <freben@gmail.com>
This commit is contained in:
TRIGUBF
2023-08-23 17:06:22 +02:00
committed by Fredrik Adelöw
parent 400beacb70
commit 149c6f03ca
3 changed files with 45 additions and 29 deletions
+1 -1
View File
@@ -73,7 +73,6 @@
"@rjsf/validator-ajv8": "5.7.3",
"@types/react": "^16.13.1 || ^17.0.0",
"@uiw/react-codemirror": "^4.9.3",
"@zip.js/zip.js": "^2.7.24",
"classnames": "^2.2.6",
"event-source-polyfill": "^1.0.31",
"git-url-parse": "^13.0.0",
@@ -81,6 +80,7 @@
"immer": "^9.0.1",
"json-schema": "^0.4.0",
"json-schema-library": "^7.3.9",
"jszip": "^3.10.1",
"lodash": "^4.17.21",
"luxon": "^3.0.0",
"qs": "^6.9.4",
@@ -26,7 +26,7 @@ import React, {
useRef,
useState,
} from 'react';
import * as zip from "@zip.js/zip.js";
import JSZip from 'jszip';
import {
scaffolderApiRef,
ScaffolderDryRunResponse,
@@ -198,33 +198,28 @@ export function useDryRun(): DryRun {
}
async function createZipDownload(directoryContents: { path: string; base64Content: string; executable: boolean; }[], name: string) {
// needs zip.js
// Creates a BlobWriter object where the zip content will be written.
const zipFileWriter = new zip.BlobWriter();
// Creates a ZipWriter object writing data via `zipFileWriter`, adds the entry
const zipWriter = new zip.ZipWriter(zipFileWriter);
const zip = new JSZip();
for (const d of directoryContents) {
// Decode text content from base64 to ascii
const converted = atob(d.base64Content);
// Creates a TextReader object storing the text of the entry to add in the zip (i.e. "Hello world!").
const fileReader = new zip.TextReader(converted);
await zipWriter.add(d.path, fileReader);
// add folder/file to zip
await zip.file(d.path, converted);
}
// Closes the writer.
await zipWriter.close();
zip.generateAsync({type:"blob"}).then((blob) => {
saveAs(blob, name);
}, (err) => {
throw new Error(err);
});
}
// Retrieves the Blob object containing the zip content into `zipFileBlob`
const zipFileBlob = await zipFileWriter.getData();
// Download zip
// Download zip
function saveAs(blob: Blob, name: string) {
const a = document.createElement('a');
a.href = URL.createObjectURL(zipFileBlob);
a.href = URL.createObjectURL(blob);
a.download = `dry-run-${name}.zip`;
a.click();
URL.revokeObjectURL(a.href);