@@ -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