Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Haskell中解压缩GZip_Haskell - Fatal编程技术网

在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
,就好像它是系统编码中的文本编码一样。改为使用二进制读取。