module Codec.Epub2.Archive
( mkEpubArchive
, readArchive
, writeArchive
)
where
import Codec.Archive.Zip
import Control.Monad
import qualified Data.ByteString.Lazy as B
import Data.List
import System.Directory
import System.FilePath
getRecursiveContents :: FilePath -> IO [FilePath]
getRecursiveContents parent = do
fullContents <- getDirectoryContents parent
let contents = filter (not . isPrefixOf ".") fullContents
paths <- forM contents $ \name -> do
let path = parent </> name
isDirectory <- doesDirectoryExist path
if isDirectory
then getRecursiveContents path
else return [path]
return $ concat paths
mkEpubArchive :: FilePath -> IO Archive
mkEpubArchive rootDir = do
setCurrentDirectory rootDir
let mimetype = ["mimetype"]
allFiles <- getRecursiveContents "."
let restFiles = allFiles \\ mimetype
flip (addFilesToArchive [OptRecursive]) restFiles >=>
flip (addFilesToArchive []) ["mimetype"]
$ emptyArchive
readArchive :: FilePath -> IO Archive
readArchive = fmap toArchive . B.readFile
writeArchive :: FilePath -> Archive -> IO ()
writeArchive zipPath = (B.writeFile zipPath) . fromArchive