{-# LANGUAGE OverloadedStrings #-}

module Text.Hastily.Unpack.Zip.ZipExtractor
    (
     extractFiles
    ) where

import           Codec.Archive.Zip
import           Control.Exception
import           Control.Exception
import qualified Data.ByteString.Lazy    as BS
import           Data.String.Conversions
import           Data.Text               (toLower)
import           System.Directory
import           System.FilePath

extractFiles :: FilePath -> IO (Either SomeException ())
extractFiles file_path = do
    let destination = (takeDirectory file_path) </> "files" in do
        try $ createDirectory destination :: IO (Either SomeException ())
        zip_string <- BS.readFile file_path
        try $ extractFilesFromArchive (options destination) (toArchive zip_string) :: IO (Either SomeException ())
    where
        options destination = [OptDestination destination, OptVerbose]