{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE ViewPatterns #-}
module Codec.Archive.ZTar
( Compression(..)
, create
, create'
, createFrom
, createFrom'
, extract
, extract'
) where
import qualified Data.ByteString.Lazy as BS
import Path (Dir, File, Path, toFilePath)
import qualified Codec.Archive.ZTar.GZip as GZip
import qualified Codec.Archive.ZTar.Tar as Tar
import qualified Codec.Archive.ZTar.Zip as Zip
data Compression
= NoCompression
| GZip
| Zip
deriving (Show)
create :: Compression
-> FilePath
-> FilePath
-> IO ()
create compression archive dir = createFrom compression archive dir ["."]
create' :: Compression -> Path b0 File -> Path b1 Dir -> IO ()
create' compression (toFilePath -> archive) (toFilePath -> dir) = create compression archive dir
createFrom :: Compression
-> FilePath
-> FilePath
-> [FilePath]
-> IO ()
createFrom compression = case compression of
NoCompression -> Tar.create
GZip -> GZip.create
Zip -> Zip.create
createFrom' :: Compression -> Path b0 File -> Path b1 Dir -> [FilePath] -> IO ()
createFrom' compression (toFilePath -> archive) (toFilePath -> dir) paths =
createFrom compression archive dir paths
extract :: FilePath
-> FilePath
-> IO ()
extract archive dir = BS.readFile archive >>= \case
Tar.TarFormat -> Tar.extract archive dir
GZip.GZipFormat -> GZip.extract archive dir
Zip.ZipFormat -> Zip.extract archive dir
"" -> fail $ "Trying to extract empty file: " ++ archive
_ -> fail $ "Could not recognize archive format: " ++ archive
extract' :: Path b1 File -> Path b0 Dir -> IO ()
extract' (toFilePath -> archive) (toFilePath -> dir) = extract archive dir