Skip to content

Commit

Permalink
feat: zipファイルの処理をjszipからzip.jsに移行
Browse files Browse the repository at this point in the history
  • Loading branch information
TK11235 committed Sep 5, 2024
1 parent 367a3ee commit 059e10e
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 110 deletions.
1 change: 0 additions & 1 deletion angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
"crypto-js/sha256",
"file-saver",
"hammerjs",
"jszip",
"lzbase62",
"msgpack-lite",
"skyway-js",
Expand Down
121 changes: 40 additions & 81 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
"@angular/platform-browser-dynamic": "^17.0.8",
"@angular/router": "^17.0.8",
"@skyway-sdk/core": "^1.6.1",
"@zip.js/zip.js": "^2.7.52",
"base-x": "^4.0.0",
"bcdice": "^4.5.0",
"crypto-js": "^4.2.0",
"file-saver": "^2.0.5",
"hammerjs": "^2.0.8",
"js-yaml": "^4.1.0",
"jszip": "^3.10.1",
"lzbase62": "^2.0.0",
"msgpack-lite": "^0.1.26",
"rxjs": "~7.8.0",
Expand Down
54 changes: 27 additions & 27 deletions src/app/class/core/file-storage/file-archiver.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BlobReader, BlobWriter, ZipReader, ZipWriter } from '@zip.js/zip.js';
import { saveAs } from 'file-saver';
import JSZip from 'jszip';

import { EventSystem } from '../system';
import { XmlUtil } from '../system/util/xml-util';
Expand Down Expand Up @@ -123,20 +123,15 @@ export class FileArchiver {

private async handleZip(file: File) {
if (!(0 <= file.type.indexOf('application/') || file.type.length < 1)) return;
let zip = new JSZip();
try {
zip = await zip.loadAsync(file);
} catch (reason) {
console.warn(reason);
return;
}
let zipEntries: JSZip.JSZipObject[] = [];
zip.forEach((relativePath, zipEntry) => zipEntries.push(zipEntry));
for (let zipEntry of zipEntries) {

let zipReader = new ZipReader(new BlobReader(file));
let entries = await zipReader.getEntries();

for (let entry of entries) {
try {
let arraybuffer = await zipEntry.async('arraybuffer');
console.log(zipEntry.name + ' 解凍...');
await this.load([new File([arraybuffer], zipEntry.name, { type: MimeType.type(zipEntry.name) })]);
let blob = await entry.getData(new BlobWriter());
console.log(entry.filename + ' 解凍...');
await this.load([new File([blob], entry.filename, { type: MimeType.type(entry.filename) })]);
} catch (reason) {
console.warn(reason);
}
Expand All @@ -149,19 +144,24 @@ export class FileArchiver {
if (!files) return;
let saveFiles: File[] = files instanceof FileList ? toArrayOfFileList(files) : files;

let zip = new JSZip();
for (let file of saveFiles) {
zip.file(file.name, file);
}

let blob = await zip.generateAsync({
type: 'blob',
compression: 'DEFLATE',
compressionOptions: {
level: 6
}
}, updateCallback);
saveAs(blob, zipName + '.zip');
let zipWriter = new ZipWriter(new BlobWriter('application/zip'), { bufferedWrite: true });

let sumProgress = 0;
let sumTotal = 0;
await Promise.all(Array.from(saveFiles).map(async file => {
let prevProgress = 0;
sumTotal += file.size;
zipWriter.add(file.name, new BlobReader(file), {
async onprogress(progress, total) {
sumProgress += progress - prevProgress;
prevProgress = progress;
let percent = sumProgress * 100 / sumTotal;
updateCallback({ percent: percent, currentFile: file.name });
}
});
}));

saveAs(await zipWriter.close(), zipName + '.zip');
}
}

Expand Down

0 comments on commit 059e10e

Please sign in to comment.