6

我很难弄清楚这一点。这是我正在尝试的:

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)

为什么我得到那个例外?

Codec.Compression.GZip.decompress我也从zlib 包中尝试过,但我无法得到要解决的类型,String而不是ByteString.

4

1 回答 1

9

ByteStringto的转换String取决于压缩文件的字符编码,但假设它是 ASCII 或 Latin-1,这应该有效:

import Codec.Compression.GZip (decompress)
import qualified Data.ByteString.Lazy as LBS
import Data.ByteString.Lazy.Char8 (unpack)

readGZipFile :: FilePath -> IO String
readGZipFile path = fmap (unpack . decompress) $ LBS.readFile path

如果您需要使用其他编码(如 UTF-8),请替换unpack为适当的解码函数,例如Data.ByteString.Lazy.UTF8.toString.

当然,如果你解压的文件不是文本文件,最好还是保存为ByteString.

于 2012-04-10T01:15:42.053 回答