Skip to content

Commit ed69bcc

Browse files
authored
Merge pull request #3 from oka4shi/feat/decompress-deflate
Deflate圧縮されたzipファイルに対応
2 parents d793a9b + f754547 commit ed69bcc

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

Readme.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,3 @@ npmのworkspaceを使用しています。
5050
`https://<GitHubユーザー名>.github.io/artifact-viewer/?owner=<GitHubユーザー名>&repo=<リポジトリ名>&artifact_id=<アーティファクトID>`にアクセスする。
5151

5252
現在の制約として、Artifactの中身にはpdfが1ファイルだけ含まれている必要があります。
53-
また、Artifactのcompression-levelは`0`である必要があります。

app/src/main.ts

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,6 @@ const main = async () => {
8080
relativeLocalHeaderOffset: view.getUint32(offset + 42, true),
8181
};
8282

83-
// 現在は圧縮されていないときのみ対応
84-
if (metadata.compressionMethod !== 0) {
85-
return `サポートされていない圧縮方式です: ${metadata.compressionMethod}`;
86-
}
87-
8883
const fileNameBinary = new Uint8Array(
8984
buf,
9085
offset + 46,
@@ -111,11 +106,34 @@ const main = async () => {
111106
const fileData = new Uint8Array(
112107
buf,
113108
fileDataOffset,
114-
metadata.compressedSize - 1,
109+
metadata.compressedSize,
115110
);
116111

112+
console.log("Compression Method: ", metadata.compressionMethod);
113+
let decompressedFile: BlobPart;
114+
switch (metadata.compressionMethod) {
115+
case 0:
116+
// 圧縮されていないときはそのまま
117+
decompressedFile = fileData;
118+
break;
119+
case 8:
120+
// deflate圧縮されている場合はDecompressionStreamで解凍
121+
const zipBlob = new Blob([fileData]);
122+
const stream: ReadableStream<Uint8Array> = zipBlob.stream();
123+
const compressedStream = stream.pipeThrough(
124+
new DecompressionStream("deflate-raw"),
125+
);
126+
127+
const res = new Response(compressedStream);
128+
decompressedFile = await res.blob();
129+
130+
break;
131+
default:
132+
return `サポートされていない圧縮方式です: ${metadata.compressionMethod}`;
133+
}
134+
117135
// blobに変換して遷移
118-
const blob = new Blob([fileData], { type: "application/pdf" });
136+
const blob = new Blob([decompressedFile], { type: "application/pdf" });
119137
const blobUrl = window.URL.createObjectURL(blob);
120138
window.location.href = blobUrl;
121139
};

0 commit comments

Comments
 (0)