我目前正在嘗試弄清楚如何在Haskell中列出(gzipped)TAR檔案。 Codec.Archive.Tar
似乎是正確的選擇,但我不知道如何map
entryPath
超過Entries
monoid。在Haskell中列出TAR檔案
我們假設TAR包含條目(僅文件)a.txt, b.txt, c.txt
並且名爲foo.tar.gz
。這裏是我的代碼來讀取文件:
import qualified Codec.Archive.Tar as Tar
import qualified Data.ByteString.Lazy as BS
import qualified Codec.Compression.GZip as GZip
foldEntryToPath :: Tar.Entry -> [String] -> [String]
foldEntryToPath entry list = list ++ [show $ Tar.entryPath entry]
-- Converts TAR errors to a string.
entryFailMapper :: String -> [String]
entryFailMapper err = [err]
main = do
fileContent <- fmap GZip.decompress $ BS.readFile "foo.tar.gz"
entries <- fmap Tar.read fileContent :: Tar.Entries
-- Here I don't know how to correctly apply fmap
entryPaths <- Tar.foldEntries foldEntryToPath [] entryFailMapper entries :: [String]
-- This should print ["a.txt", "b.txt", "c.txt"]
print entryPaths
這是一個被runghc
印刷錯誤:
readtar.hs:14:49:
Expecting one more argument to `Tar.Entries'
In an expression type signature: Tar.Entries
In a stmt of a 'do' block:
entries <- fmap Tar.read fileContent :: Tar.Entries
In the expression:
do { fileContent <- fmap GZip.decompress
$ BS.readFile "foo.tar.gz";
entries <- fmap Tar.read fileContent :: Tar.Entries;
entryPaths <- Tar.foldEntries
foldEntryToPath [] (\ x -> [...]) entries ::
[String];
print entryPaths }
到目前爲止,我有哈斯克爾知之甚少,但是通過閱讀the docs我不知道爲什麼Tar.Entries
是一個類型類(是否正確的術語,當它說expecting n more arguments to <type>
?)或什麼是正確的類型使用。
任何幫助將不勝感激!
注意,這不是LIB的最新版本。 – Vektorweg
@Vektorweg謝謝,我沒有注意到(我更新的鏈接!)。然而,這是隻是文檔鏈接(通過谷歌首先發現他們),我安裝了使用'cabal安裝tar'它實際安裝了0.4.0.1 –