1 -- | This module defines functions to read Java JAR files.
2 module Java.JAR.Archive where
4 import qualified Codec.Archive.LibZip as Zip
7 import qualified Data.ByteString.Lazy as B
9 import Java.ClassPath.Types
10 import Java.ClassPath.Common
14 readJAREntry :: (Enum a) => FilePath -> String -> IO (Maybe [a])
15 readJAREntry jarfile path = do
16 Zip.catchZipError (Just `fmap` (Zip.withArchive [] jarfile $ Zip.fileContents [] path))
17 (\_ -> return Nothing)
19 -- | Read all entires from JAR file
20 readAllJAR :: FilePath -> IO [Tree CPEntry]
21 readAllJAR jarfile = do
22 files <- Zip.withArchive [] jarfile $ Zip.fileNames []
23 return $ mapF (NotLoadedJAR jarfile) (buildTree $ filter good files)
25 good file = ".class" `isSuffixOf` file
27 -- | Read one class from JAR file
28 readFromJAR :: FilePath -> FilePath -> IO (Class Direct)
29 readFromJAR jarfile path = do
30 content <- Zip.withArchive [] jarfile $ Zip.fileContents [] path
31 let bstr = B.pack content
32 return $ classFile2Direct (decode bstr)