Created
November 28, 2012 01:21
-
-
Save tos-kamiya/4158413 to your computer and use it in GitHub Desktop.
expand a zip file's contents to a directory which has the same name to the zip file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE OverloadedStrings #-} | |
{-# LANGUAGE ExtendedDefaultRules #-} | |
{-# OPTIONS_GHC -fno-warn-type-defaults #-} | |
import Control.Monad (forM_) | |
import qualified Data.Text.Lazy as LT (append, pack, Text) | |
import Filesystem.Path (append, basename, filename) | |
import Shelly | |
import System.Environment (getArgs) | |
default (LT.Text) | |
garbageFilePatterns = ["*/.DS_Store", "*/Thumbs.db", "__MACOSX/*"] | |
expandToDirectoryOfBasename f = do | |
unless (hasExt "zip" f) $ errorExit ("not a zip file: " ++< f) | |
let d = basename f | |
whenM (test_e d) $ errorExit ("directory already exists: " ++< d) | |
run_ "unzip" $ ["-d", toTextIgnore d, toTextIgnore f] | |
++ concatMap (¥x -> ["-x", x]) garbageFilePatterns | |
return d | |
where a ++< p = LT.append a $ toTextIgnore p | |
removeIntermediateRedundantDir d = do | |
contents <- ls d | |
when (length contents == 1) $ do | |
let c0 = contents !! 0 | |
whenM (test_d c0) $ do | |
cts <- ls c0 | |
forM_ cts $ ¥f -> do | |
(mv f) . (append d) =<< (relativeTo c0 f) | |
rm_rf c0 | |
main :: IO () | |
main = shelly $ silently $ do | |
args <- liftIO $ map (fromText . LT.pack) <$> getArgs | |
when (null args) $ errorExit "specify zip files" | |
forM_ args $ ¥f -> do | |
d <- expandToDirectoryOfBasename f | |
removeIntermediateRedundantDir d |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Revised. I'm not satisfied this revision ( eec0a2 ), because of the ugry string file-path manipulation at line 15-20.