Skip to content

Commit

Permalink
Slight optimization of getCompressedData.
Browse files Browse the repository at this point in the history
We don't need to build a list of decompressed chunks,
because we only care about finding the end of the compressed data.
  • Loading branch information
jgm committed Mar 7, 2024
1 parent 7bf034d commit c4f8de6
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions src/Codec/Archive/Zip.hs
Original file line number Diff line number Diff line change
Expand Up @@ -969,8 +969,9 @@ fromString :: String -> B.ByteString
fromString = TL.encodeUtf8 . TL.pack

data DecompressResult =
DecompressSuccess [S.ByteString] B.ByteString
-- chunks in reverse, remainder
DecompressSuccess B.ByteString -- bytes remaining
-- (we just discard decompressed chunks, because we only
-- want to know where the compressed data ends)
| DecompressFailure ZlibInt.DecompressError

getCompressedData :: CompressionMethod -> Get B.ByteString
Expand Down Expand Up @@ -1003,20 +1004,16 @@ getCompressedData NoCompression = do
getCompressedData Deflate = do
remainingBytes <- lookAhead getRemainingLazyByteString
let result = ZlibInt.foldDecompressStreamWithInput
(\bs res ->
case res of
DecompressSuccess chunks remainder
-> DecompressSuccess (bs:chunks) remainder
x -> x)
(DecompressSuccess [])
(\_bs res -> res)
DecompressSuccess
DecompressFailure
(ZlibInt.decompressST ZlibInt.rawFormat
ZlibInt.defaultDecompressParams{
ZlibInt.decompressAllMembers = False })
remainingBytes
case result of
DecompressFailure err -> fail (show err)
DecompressSuccess _chunks afterCompressedBytes ->
DecompressSuccess afterCompressedBytes ->
-- Consume the compressed bytes; we don't do anything with
-- the decompressed chunks. We are just decompressing as a
-- way of finding where the compressed data ends.
Expand Down

0 comments on commit c4f8de6

Please sign in to comment.