From 600bbd21a13f3cdcb4975b3fff56fec5d7f2f12d Mon Sep 17 00:00:00 2001 From: spuds <71292624+bananaturtlesandwich@users.noreply.github.com> Date: Sat, 14 Jan 2023 15:17:24 +0000 Subject: [PATCH] gzip support --- src/entry.rs | 49 +++++++++++++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/src/entry.rs b/src/entry.rs index d46acd6..74baa1d 100644 --- a/src/entry.rs +++ b/src/entry.rs @@ -90,33 +90,38 @@ impl Entry { data.truncate(self.compressed as usize); } use io::Write; - match self.compression { - Compression::None => buf.write_all(&data)?, - Compression::Zlib => match &self.blocks { - Some(blocks) => { - for block in blocks { + macro_rules! decompress { + ($decompressor: ty) => { + match &self.blocks { + Some(blocks) => { + for block in blocks { + io::copy( + &mut <$decompressor>::new( + &data[match version >= Version::RelativeChunkOffsets { + true => block.start as usize..block.end as usize, + false => { + (block.start - data_offset) as usize + ..(block.end - data_offset) as usize + } + }], + ), + &mut buf, + )?; + } + } + None => { io::copy( - &mut flate2::read::ZlibDecoder::new( - &data[match version >= Version::RelativeChunkOffsets { - true => block.start as usize..block.end as usize, - false => { - (block.start - data_offset) as usize - ..(block.end - data_offset) as usize - } - }], - ), + &mut flate2::read::ZlibDecoder::new(data.as_slice()), &mut buf, )?; } } - None => { - io::copy( - &mut flate2::read::ZlibDecoder::new(data.as_slice()), - &mut buf, - )?; - } - }, - Compression::Gzip => todo!(), + }; + } + match self.compression { + Compression::None => buf.write_all(&data)?, + Compression::Zlib => decompress!(flate2::read::ZlibDecoder<&[u8]>), + Compression::Gzip => decompress!(flate2::read::GzDecoder<&[u8]>), Compression::Oodle => todo!(), } buf.flush()?;