我目前正在尝试弄清楚如何在 Haskell 中列出(gzipped)TAR 存档。 Codec.Archive.Tar
似乎是这项任务的正确选择,但我不知道如何map
entryPath
超过Entries
幺半群。
假设 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 }
到目前为止,我对 Haskell 知之甚少,但通过阅读 the docs不知道为什么Tar.Entries
是一个类型类(expecting n more arguments to <type>
是正确的术语吗?)或者正确使用的类型是什么。
任何帮助将不胜感激!
最佳答案
我认为 foldEntryToPath
需要修复:
foldEntryToPath :: Tar.Entry -> [String] -> [String]
foldEntryToPath entry list = (show $ Tar.entryPath entry) : list
在main
中:
fileContent <- fmap GZip.decompress $ BS.readFile "foo.tar.gz"
let entries = Tar.read fileContent
let entryPaths = Tar.foldEntries foldEntryToPath [] entryFailMapper entries
print entryPaths
关于haskell - 在 Haskell 中列出 TAR 存档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21247862/