module Streaming.Zip (
decompress
, decompress'
, compress
, gunzip
, gunzip'
, gzip
, CompressionLevel
, defaultCompression
, noCompression
, bestSpeed
, bestCompression
, compressionLevel
, Z.defaultWindowBits
, windowBits
) where
import Data.Streaming.Zlib as Z
import Control.Exception (throwIO)
import Control.Monad (unless)
import qualified Data.ByteString as B
import Data.ByteString.Streaming
import Streaming
import qualified Data.ByteString.Streaming.Internal as I
import Data.ByteString.Streaming.Internal (ByteString (..))
decompress
:: MonadIO m
=> Z.WindowBits
-> ByteString m r
-> ByteString m r
decompress wbits p0 = do
inf <- liftIO $ Z.initInflate wbits
r <- for p0 $ \bs -> do
popper <- liftIO (Z.feedInflate inf bs)
fromPopper popper
bs <- liftIO $ Z.finishInflate inf
unless (B.null bs) (chunk bs)
return r
{-# INLINABLE decompress #-}
decompress'
:: MonadIO m
=> Z.WindowBits
-> ByteString m r
-> ByteString m (Either (ByteString m r) r)
decompress' wbits p0 = go p0 =<< liftIO (Z.initInflate wbits)
where
flush inf = do
bs <- liftIO $ Z.flushInflate inf
unless (B.null bs) (chunk bs)
go p inf = do
res <- lift (nextChunk p)
case res of
Left r -> return $ Right r
Right (bs, p') -> do
fromPopper =<< liftIO (Z.feedInflate inf bs)
flush inf
leftover <- liftIO $ Z.getUnusedInflate inf
if B.null leftover
then go p' inf
else return $ Left (chunk leftover >> p')
{-# INLINABLE decompress' #-}
compress
:: MonadIO m
=> CompressionLevel
-> Z.WindowBits
-> ByteString m r
-> ByteString m r
compress (CompressionLevel clevel) wbits p0 = do
def <- liftIO $ Z.initDeflate clevel wbits
let loop bs = case bs of
I.Chunk c rest -> do
popper <- liftIO (Z.feedDeflate def c)
fromPopper popper
loop rest
I.Go m -> I.Go (liftM loop m)
I.Empty r -> return r
r <- loop p0
fromPopper $ Z.finishDeflate def
return r
{-# INLINABLE compress #-}
newtype CompressionLevel = CompressionLevel Int
deriving (Show, Read, Eq, Ord)
defaultCompression, noCompression, bestSpeed, bestCompression :: CompressionLevel
defaultCompression = CompressionLevel (-1)
noCompression = CompressionLevel 0
bestSpeed = CompressionLevel 1
bestCompression = CompressionLevel 9
compressionLevel :: Int -> CompressionLevel
compressionLevel n
| n >= 0 && n <= 9 = CompressionLevel n
| otherwise = error "CompressionLevel must be in the range 0..9"
windowBits :: Int -> WindowBits
windowBits = WindowBits
gunzip
:: MonadIO m
=> ByteString m r
-> ByteString m r
gunzip = decompress gzWindowBits
{-# INLINABLE gunzip #-}
gunzip'
:: MonadIO m
=> ByteString m r
-> ByteString m (Either (ByteString m r) r)
gunzip' = decompress' gzWindowBits
{-# INLINE gunzip' #-}
gzip
:: MonadIO m
=> CompressionLevel
-> ByteString m r
-> ByteString m r
gzip clevel = compress clevel gzWindowBits
{-# INLINE gzip #-}
gzWindowBits :: Z.WindowBits
gzWindowBits = Z.WindowBits 31
for bs0 op = loop bs0 where
loop bs = case bs of
I.Chunk c rest -> op c >> loop rest
I.Go m -> I.Go (liftM loop m)
I.Empty r -> return r
{-# INLINABLE for #-}
fromPopper :: MonadIO m
=> Z.Popper
-> ByteString m ()
fromPopper pop = loop
where
loop = do
mbs <- liftIO pop
case mbs of
PRDone -> I.Empty ()
PRError e -> I.Go (liftIO (throwIO e))
PRNext bs -> I.Chunk bs loop
{-# INLINABLE fromPopper #-}