在Haskell中解压缩GZip
我很难弄明白这一点。以下是我正在尝试的:在Haskell中解压缩GZip,haskell,Haskell,我很难弄明白这一点。以下是我正在尝试的: 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) 为什么我会得到那个例外 我还尝试了从中解压缩Co
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.decompression
,但我无法将类型转换为String
,而不是ByteString
从ByteString
转换为String
取决于压缩文件的字符编码,但假设它是ASCII或拉丁语-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
,例如
当然,如果要解压缩的文件不是文本文件,最好通过testring将其保存为
文件,这不是一个完整的答案,但可能readFile
正在尝试解码test.html.gz
,就好像它是系统编码中的文本编码一样。改为使用二进制读取。