I’m having a hard time figuring this out. Here’s what I’m trying:
ghci> :m +System.FileArchive.GZip -- From the "MissingH" package
ghci> fmap decompress $ readFile "test.html.gz"
*** Exception: test.html.gz: hGetContents: invalid argument (invalid byte sequence)
Why am I getting that exception?
I also tried Codec.Compression.GZip.decompress from the zlib package, but I can’t get the types to work out to String instead of ByteString.
The conversion from
ByteStringtoStringdepends on the character encoding of the compressed file, but assuming it’s ASCII or Latin-1, this should work:If you need to work with some other encoding like UTF-8, replace
unpackwith an appropriate decoding function, e.g.Data.ByteString.Lazy.UTF8.toString.Of course, if the file you’re decompressing isn’t a text file, it’s better to keep it as a
ByteString.