2012-04-09 5 views
5

나는 이것을 고치는데 어려움을 겪고있다. 다음은 내가 시도한 것입니다 :하스켈에서 GZip 압축 풀기

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) 

왜 예외가 발생합니까?

또한 zlib package에서 Codec.Compression.GZip.decompress을 시도했지만 ByteString 대신 String으로 해결할 수있는 유형을 얻을 수 없습니다.

+1

이것은 완전한 대답은 아니지만'readFile'은'test.html.gz'를 시스템 인코딩에서 텍스트 인코딩 된 것처럼 해독하려고합니다. 대신에 이진 읽기를 사용하십시오. –

답변

8

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을 적절한 디코딩 기능 (예 : Data.ByteString.Lazy.UTF8.toString.

물론 압축 해제하려는 파일이 텍스트 파일이 아니라면 ByteString으로 유지하는 것이 좋습니다.

+2

그렇다면 압축을 풀고 텍스트로 디코딩하십시오. – alternative