Monadic interface to libzip
.
Most of the operations on zip archive happen within Archive
monad
(see withArchive
).
Partial reading of the files in the archive may be performed from
within Entry
monad (see fromFile
).
Both Archive
and Entry
are monad transformers over IO
, and allow
for IO with single and double lift
ing respectingly.
Note: LibZip does not handle text encodings. Even if its API accepts
String
s (e.g. in sourceBuffer
), character codes above 255 should
not be used. The user is responsible of proper encoding the text
data.
Examples
List files in the zip archive:
import System.Environment (getArgs) import Codec.Archive.LibZip main = do (zipfile:_) <- getArgs files <- withArchive [] zipfile $ fileNames [] mapM_ putStrLn files
Create a zip archive and a add file to the archive:
import System.Environment (getArgs) import Codec.Archive.LibZip main = do (zipfile:_) <- getArgs withArchive [CreateFlag] zipfile $ do zs <- sourceBuffer "Hello World!" addFile "hello.txt" zs
Extract and print a file from the zip archive:
import System.Environment (getArgs) import Codec.Archive.LibZip main = do (zipfile:file:_) <- getArgs bytes <- withArchive [] zipfile $ fileContents [] file putStrLn bytes
See also an implementation of a simple zip archiver hzip.hs
in the
examples/
directory of the source distribution.
- type Archive a = StateT Zip IO a
- type Entry a = StateT (ZipFile, Int, [FileFlag]) (StateT Zip IO) a
- data ZipStat = ZipStat {}
- withArchive :: [OpenFlag] -> FilePath -> Archive a -> IO a
- getZip :: Archive Zip
- numFiles :: Archive Int
- fileName :: [FileFlag] -> Int -> Archive FilePath
- nameLocate :: [FileFlag] -> FilePath -> Archive (Maybe Int)
- fileNames :: [FileFlag] -> Archive [FilePath]
- fileSize :: [FileFlag] -> FilePath -> Archive Int
- fileSizeIx :: [FileFlag] -> Int -> Archive Int
- fileStat :: [FileFlag] -> FilePath -> Archive ZipStat
- fileStatIx :: [FileFlag] -> Int -> Archive ZipStat
- deleteFile :: [FileFlag] -> FilePath -> Archive ()
- deleteFileIx :: Int -> Archive ()
- renameFile :: [FileFlag] -> FilePath -> FilePath -> Archive ()
- renameFileIx :: Int -> FilePath -> Archive ()
- addFile :: FilePath -> ZipSource -> Archive Int
- addDirectory :: FilePath -> Archive Int
- replaceFile :: [FileFlag] -> FilePath -> ZipSource -> Archive ()
- replaceFileIx :: Int -> ZipSource -> Archive ()
- sourceBuffer :: Enum a => [a] -> Archive ZipSource
- sourceFile :: FilePath -> Int -> Int -> Archive ZipSource
- sourceZip :: [FileFlag] -> Zip -> Int -> Int -> Int -> Archive ZipSource
- data (Enum a, Storable a, Storable st) => PureSource a st = PureSource {}
- sourcePure :: (Enum a, Storable a, Storable st) => PureSource a st -> Archive ZipSource
- getComment :: [FileFlag] -> Archive (Maybe String)
- setComment :: String -> Archive ()
- removeComment :: Archive ()
- getFileComment :: [FileFlag] -> FilePath -> Archive (Maybe String)
- getFileCommentIx :: [FileFlag] -> Int -> Archive (Maybe String)
- setFileComment :: [FileFlag] -> FilePath -> String -> Archive ()
- setFileCommentIx :: Int -> String -> Archive ()
- removeFileComment :: [FileFlag] -> FilePath -> Archive ()
- removeFileCommentIx :: Int -> Archive ()
- unchangeFile :: [FileFlag] -> FilePath -> Archive ()
- unchangeFileIx :: Int -> Archive ()
- unchangeArchive :: Archive ()
- unchangeAll :: Archive ()
- fromFile :: [FileFlag] -> FilePath -> Entry a -> Archive a
- fromFileIx :: [FileFlag] -> Int -> Entry a -> Archive a
- readBytes :: Enum a => Int -> Entry [a]
- skipBytes :: Int -> Entry ()
- readContents :: Enum a => Entry [a]
- fileContents :: Enum a => [FileFlag] -> FilePath -> Archive [a]
- fileContentsIx :: Enum a => [FileFlag] -> Int -> Archive [a]
- data OpenFlag
- = CreateFlag
- | ExclFlag
- | CheckConsFlag
- data FileFlag
- data ZipCompMethod
- = CompDEFAULT
- | CompSTORE
- | CompSHRINK
- | CompREDUCE_1
- | CompREDUCE_2
- | CompREDUCE_3
- | CompREDUCE_4
- | CompIMPLODE
- | CompDEFLATE
- | CompDEFLATE64
- | CompPKWARE_IMPLODE
- | CompBZIP2
- | CompLZMA
- | CompTERSE
- | CompLZ77
- | CompWAVPACK
- | CompPPMD
- data ZipEncryptionMethod
- data ZipError
- = ErrOK
- | ErrMULTIDISK
- | ErrRENAME
- | ErrCLOSE
- | ErrSEEK
- | ErrREAD
- | ErrWRITE
- | ErrCRC
- | ErrZIPCLOSED
- | ErrNOENT
- | ErrEXISTS
- | ErrOPEN
- | ErrTMPOPEN
- | ErrZLIB
- | ErrMEMORY
- | ErrCHANGED
- | ErrCOMPNOTSUPP
- | ErrEOF
- | ErrINVAL
- | ErrNOZIP
- | ErrINTERNAL
- | ErrINCONS
- | ErrREMOVE
- | ErrDELETED
- catchZipError :: IO a -> (ZipError -> IO a) -> IO a
- lift :: MonadTrans t => forall m a. Monad m => m a -> t m a
Types
type Entry a = StateT (ZipFile, Int, [FileFlag]) (StateT Zip IO) aSource
Monadic computation to read from open archive entries.
See fromFile
and fromFileIx
.
File statistics expressed in native Haskell types.
ZipStat | |
|
Archive operations
:: [OpenFlag] | Checks for consistency or existence. |
-> FilePath | Filename of the zip archive. |
-> Archive a | Action to do with the archive. |
-> IO a |
Top-level wrapper for operations with an open
archive. withArchive
opens and closes the file
automatically. On error it throws ZipError
.
:: [FileFlag] |
|
-> Int | Position index of a file in the archive. |
-> Archive FilePath | Name of the file in the archive. |
Get name of an entry in the archive by its index.
:: [FileFlag] | Filename lookup mode. |
-> FilePath | Name of the file in the archive. |
-> Archive (Maybe Int) |
|
Locate an entry (get its index) in the archive by its name.
:: [FileFlag] |
|
-> Archive [FilePath] |
Get names of all entries (files and directories) in the archive.
:: [FileFlag] | Filename lookup mode, |
-> FilePath | Name of the file in the archive. |
-> Archive Int | File size. |
Get size of a file in the archive.
:: [FileFlag] |
|
-> Int | Position index of a file in the archive. |
-> Archive Int | File size. |
Get size of a file in the archive (by index).
:: [FileFlag] | Filename lookup mode, |
-> FilePath | Name of the file in the archive. |
-> Archive ZipStat | Infomation about the file. |
Get information about a file in the archive.
:: [FileFlag] |
|
-> Int | Position index of a file in the archive. |
-> Archive ZipStat | Information about the file. |
Get information about a file in the archive (by index).
Delete file from the archive.
Delete file (referenced by position index) from the archive.
Rename file in the archive.
Rename file (referenced by position index) in the archive.
:: FilePath | Name of the file to create. |
-> ZipSource | Source where file data is obtained from. |
-> Archive Int | Position index of the new file. |
Add a file to the archive.
:: FilePath | Directory's name in the archive. |
-> Archive Int | Position index of the new directory entry. |
Add a directory to the archive.
:: [FileFlag] | Filename lookup mode. |
-> FilePath | File to replace. |
-> ZipSource | Source where the new file data is obtained from. |
-> Archive () |
Replace a file in the archive.
:: Int | Position index of a file in the archive. |
-> ZipSource | Source where the new file data is obtained from |
-> Archive () |
Replace a file in the archive (referenced by position index).
sourceBuffer :: Enum a => [a] -> Archive ZipSourceSource
Create a data source. Note: input is converted to [Word8]
internally.
:: FilePath | File to open. |
-> Int | Offset from the beginning of the file. |
-> Int | The number of bytes to read. If |
-> Archive ZipSource |
Create a data source from a file.
:: [FileFlag] |
|
-> Zip | Source archive. |
-> Int | Position index of a file in the source archive. |
-> Int | Offset from the beginning of the file. |
-> Int | The number of bytes to read. If |
-> Archive ZipSource |
Create a data source from a file in the zip archive.
data (Enum a, Storable a, Storable st) => PureSource a st Source
Wrapper for a user-provided pure function to be used with sourcePure
.
Data size should be known in advance (srcSize
).
The function should support reading by chunks (readSrc
).
PureSource | |
|
sourcePure :: (Enum a, Storable a, Storable st) => PureSource a st -> Archive ZipSourceSource
Create a data source from a PureSource
.
Note: input of [a]
is converted to [Word8]
internally.
:: [FileFlag] |
|
-> Archive (Maybe String) |
Get zip archive comment.
removeComment :: Archive ()Source
Remove zip archive comment.
Get comment for a file in the archive.
Get comment for a file in the archive (referenced by position index).
Set comment for a file in the archive.
Set comment for a file in the archive (referenced by position index).
Remove comment for a file in the archive.
Remove comment for a file in the archive (referenced by position index).
Undo changes to a file in the archive.
Undo changes to a file in the archive (referenced by position index).
unchangeArchive :: Archive ()Source
Undo global changes to zip archive (revert changes to the archive comment and global flags).
unchangeAll :: Archive ()Source
Undo all changes in a zip archive.
File reading operations
:: [FileFlag] | Filename lookup mode,
|
-> FilePath | Name of the file in the arhive. |
-> Entry a | Action with the file. |
-> Archive a |
Wrapper for operations with a file in the archive. fromFile
is normally
called from within an Archive
action (see also withArchive
).
fromFile
can be replaced with fileContents
to read an entire file at
once.
:: [FileFlag] |
|
-> Int | Position index of a file in the archive. |
-> Entry a | Action with the file. |
-> Archive a |
Wrapper for operations with a file in the archive. File is referenced
by index (position). fromFileIx
is normally called from within
an Archive
action (see also withArchive
). fromFileIx
can be replaced
with fileContentsIx
to read an entire file at once.
Read at most n
bytes from the file.
skipBytes :: Int -> Entry ()Source
Skip n
bytes from the open file. Note: this is not faster than reading.
fileContents :: Enum a => [FileFlag] -> FilePath -> Archive [a]Source
Read entire file. Shortcut for readContents
from within Archive
monad.
fileContentsIx :: Enum a => [FileFlag] -> Int -> Archive [a]Source
Read entire file (referenced by position index). Shortcut for
readContents
from within Archive
monad.
Flags and options
Flags for opening an archive.
CreateFlag | Create an archive if it does not exist. |
ExclFlag | Error if the archive already exists. |
CheckConsFlag | Check archive's consistency and error on failure. |
Flags for accessing files in the archive.
Please consult libzip
documentation about their use.
FileNOCASE | Ignore case on name lookup. |
FileNODIR | Ignore directory component. |
FileCOMPRESSED | Read the compressed data. |
FileUNCHANGED | Read the original data, ignore changes. |
FileRECOMPRESS | Force recompression of data. |
data ZipCompMethod Source
Compression methods.
CompDEFAULT | Better of deflate or store. |
CompSTORE | Stored (uncompressed). |
CompSHRINK | Shrunk. |
CompREDUCE_1 | Reduced with factor 1 |
CompREDUCE_2 | Reduced with factor 2 |
CompREDUCE_3 | Reduced with factor 3 |
CompREDUCE_4 | Reduced with factor 4 |
CompIMPLODE | Imploded. |
CompDEFLATE | Deflated. |
CompDEFLATE64 | Deflate64. |
CompPKWARE_IMPLODE | PKWARE imploding. |
CompBZIP2 | Compressed using BZIP2 algorithm. |
CompLZMA | LZMA (EFS) |
CompTERSE | Compressed using IBM TERSE (new). |
CompLZ77 | IBM LZ77 z Architecture (PFS). |
CompWAVPACK | WavPack compressed data. |
CompPPMD | PPMd version I, Rev 1. |
data ZipEncryptionMethod Source
Encryption methods.
EncryptNONE | Not encrypted. |
EncryptTRAD_PKWARE | Traditional PKWARE encryption. |
EncryptUNKNOWN | Unknown algorithm. |
Exception handling
libzip
error codes.
ErrOK | No error. |
ErrMULTIDISK | Multi-disk zip archives not supported. |
ErrRENAME | Renaming temporary file failed. |
ErrCLOSE | Closing zip archive failed. |
ErrSEEK | Seek error. |
ErrREAD | Read error. |
ErrWRITE | Write error. |
ErrCRC | CRC error. |
ErrZIPCLOSED | Containing zip archive was closed. |
ErrNOENT | No such file. |
ErrEXISTS | File already exists. |
ErrOPEN | Can't open file. |
ErrTMPOPEN | Failure to create temporary file. |
ErrZLIB | Zlib error. |
ErrMEMORY | Malloc error. |
ErrCHANGED | Entry has been changed. |
ErrCOMPNOTSUPP | Compression method not supported. |
ErrEOF | Premature EOF. |
ErrINVAL | Invalid argument. |
ErrNOZIP | Not a zip archive. |
ErrINTERNAL | Internal error. |
ErrINCONS | Zip archive inconsistent. |
ErrREMOVE | Can't remove file. |
ErrDELETED | Entry has been deleted. |
Re-exports
lift :: MonadTrans t => forall m a. Monad m => m a -> t m a
Lift a computation from the argument monad to the constructed monad.