Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell98 |
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, Integer, [FileFlag]) (StateT Zip IO) a
- data ZipStat = ZipStat {}
- withArchive :: [OpenFlag] -> FilePath -> Archive a -> IO a
- withEncryptedArchive :: [OpenFlag] -> String -> FilePath -> Archive a -> IO a
- getZip :: Archive Zip
- numFiles :: [FileFlag] -> Archive Integer
- fileName :: [FileFlag] -> Integer -> Archive FilePath
- nameLocate :: [FileFlag] -> FilePath -> Archive (Maybe Integer)
- fileNames :: [FileFlag] -> Archive [FilePath]
- fileSize :: [FileFlag] -> FilePath -> Archive Integer
- fileSizeIx :: [FileFlag] -> Integer -> Archive Integer
- fileStat :: [FileFlag] -> FilePath -> Archive ZipStat
- fileStatIx :: [FileFlag] -> Integer -> Archive ZipStat
- deleteFile :: [FileFlag] -> FilePath -> Archive ()
- deleteFileIx :: Integer -> Archive ()
- renameFile :: [FileFlag] -> FilePath -> FilePath -> Archive ()
- renameFileIx :: Integer -> ByteString -> [FileFlag] -> Archive ()
- addFile :: FilePath -> ZipSource -> Archive Int
- addFileWithFlags :: [FileFlag] -> ByteString -> ZipSource -> Archive Int
- addDirectory :: FilePath -> Archive Int
- addDirectoryWithFlags :: [FileFlag] -> ByteString -> Archive Int
- replaceFile :: [FileFlag] -> FilePath -> ZipSource -> Archive ()
- replaceFileIx :: Integer -> ZipSource -> Archive ()
- setFileCompression :: [FileFlag] -> FilePath -> ZipCompMethod -> Archive ()
- setFileCompressionIx :: Integer -> ZipCompMethod -> Archive ()
- sourceBuffer :: Enum a => [a] -> Archive ZipSource
- sourceFile :: FilePath -> Integer -> Integer -> Archive ZipSource
- sourceZip :: [FileFlag] -> Zip -> Integer -> Integer -> Integer -> Archive ZipSource
- data PureSource a st szt = PureSource {}
- sourcePure :: (Enum a, Storable a, Storable st, Integral szt) => PureSource a st szt -> Archive ZipSource
- getComment :: [FileFlag] -> Archive (Maybe String)
- setComment :: String -> Archive ()
- removeComment :: Archive ()
- getFileComment :: [FileFlag] -> FilePath -> Archive (Maybe String)
- getFileCommentIx :: [FileFlag] -> Integer -> Archive (Maybe ByteString)
- setFileComment :: [FileFlag] -> FilePath -> String -> Archive ()
- setFileCommentIx :: Integer -> ByteString -> [FileFlag] -> Archive ()
- removeFileComment :: [FileFlag] -> FilePath -> Archive ()
- removeFileCommentIx :: Integer -> Archive ()
- unchangeFile :: [FileFlag] -> FilePath -> Archive ()
- unchangeFileIx :: Integer -> Archive ()
- unchangeArchive :: Archive ()
- unchangeAll :: Archive ()
- fromFile :: [FileFlag] -> FilePath -> Entry a -> Archive a
- fromFileIx :: [FileFlag] -> Integer -> Entry a -> Archive a
- readBytes :: Enum a => Integer -> Entry [a]
- skipBytes :: Integer -> Entry ()
- readContents :: Enum a => Entry [a]
- fileContents :: Enum a => [FileFlag] -> FilePath -> Archive [a]
- fileContentsIx :: Enum a => [FileFlag] -> Integer -> Archive [a]
- data OpenFlag
- data FileFlag
- data ZipCompMethod
- 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
- | ErrENCRNOTSUPP
- | ErrRDONLY
- | ErrNOPASSWD
- | ErrWRONGPASSWD
- 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, Integer, [FileFlag]) (StateT Zip IO) a Source
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
.
:: [OpenFlag] | Checks for consistency or existence. |
-> String | Encryption password. |
-> FilePath | Filename of the zip archive. |
-> Archive a | Action to don with the archive. |
-> IO a |
Top-level wrapper for operations with an open encrypted archive.
withEncryptedArchive
opens and closes the file automatically.
On error it throws ZipError
.
:: [FileFlag] |
|
-> Archive Integer |
Get the number of entries in the archive.
:: [FileFlag] |
|
-> Integer | 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 Integer) |
|
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 Integer | File size. |
Get size of a file in the archive.
:: [FileFlag] |
|
-> Integer | Position index of a file in the archive. |
-> Archive Integer | 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] |
|
-> Integer | Position index of a file in the archive. |
-> Archive ZipStat | Information about the file. |
Get information about a file in the archive (by index).
:: [FileFlag] | Filename lookup mode (see |
-> FilePath | Filename. |
-> Archive () |
Delete file from the archive.
Delete file (referenced by position index) from the archive.
:: [FileFlag] | Filename lookup mode (see |
-> FilePath | Old name. |
-> FilePath | New name. |
-> Archive () |
Rename file in the archive.
:: Integer | Position index of a file in the archive. |
-> ByteString | New name. |
-> [FileFlag] | Name encoding flags.
|
-> 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.
:: [FileFlag] | Can be a combination of |
-> ByteString | Name of the file to create. |
-> ZipSource | Source where file data is obtained from. |
-> Archive Int | Position index of the new file. |
Add a directory to the archive.
:: [FileFlag] | Can be one of filename encoding flags:
'FileENC_GUESS (default), |
-> ByteString | 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 (see |
-> FilePath | File to replace. |
-> ZipSource | Source where the new file data is obtained from. |
-> Archive () |
Replace a file in the archive.
:: Integer | 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).
:: [FileFlag] | Filename lookup mode (see |
-> FilePath | Filename. |
-> ZipCompMethod | Compression method.
As of libzip 0.11, the following methods are supported:
|
-> Archive () |
Set compression method for a file in the archive.
:: Integer | Position index of a file in the archive. |
-> ZipCompMethod | Compression method.
As of libzip 0.11, the following methods are supported:
|
-> Archive () |
Set compression method for a file in the archive.
sourceBuffer :: Enum a => [a] -> Archive ZipSource Source
Create a data source. Note: input is converted to [Word8]
internally.
:: FilePath | File to open. |
-> Integer | Offset from the beginning of the file. |
-> Integer | The number of bytes to read. If |
-> Archive ZipSource |
Create a data source from a file.
:: [FileFlag] |
|
-> Zip | Source archive. |
-> Integer | Position index of a file in the source archive. |
-> Integer | Offset from the beginning of the file. |
-> Integer | The number of bytes to read. If |
-> Archive ZipSource |
Create a data source from a file in the zip archive.
data PureSource a st szt 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, Integral szt) => PureSource a st szt -> Archive ZipSource Source
Create a data source from a PureSource
.
Note: input of [a]
is converted to [Word8]
internally.
:: [FileFlag] | Can be a combination of |
-> Archive (Maybe String) |
Get zip archive comment.
removeComment :: Archive () Source
Remove zip archive comment.
Get comment for a file in the archive.
:: [FileFlag] | Comment lookup flags.
|
-> Integer | Position index of the file. |
-> Archive (Maybe ByteString) |
Get comment for a file in the archive (referenced by position index).
:: [FileFlag] | Filename lookup mode (see |
-> FilePath | Filename. |
-> String | New file comment. |
-> Archive () |
Set comment for a file in the archive.
:: Integer | Position index of a file in the archive. |
-> ByteString | New file comment. |
-> [FileFlag] | Comment encoding flags.
|
-> Archive () |
Set comment for a file in the archive (referenced by position index).
:: [FileFlag] | Filename lookup mode (see |
-> FilePath | Filename. |
-> Archive () |
Remove comment for a file in the archive.
Remove comment for a file in the archive (referenced by position index).
:: [FileFlag] | Filename lookup mode (see |
-> FilePath | Filename. |
-> Archive () |
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] |
|
-> Integer | 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 :: Integer -> 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] -> Integer -> 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. |
TruncateFlag | If archive exists, ignore its current content. |
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. |
FileENCRYPTED | Read encrypted data (implies FileCOMPRESSED). |
FileENC_GUESS | Guess string encoding (default). |
FileENC_RAW | Get unmodified string. |
FileENC_STRICT | Follow specification strictly. |
FileLOCAL | In local header. |
FileCENTRAL | In central directory. |
FileENC_UTF_8 | String is UTF-8 encoded. |
FileENC_CP437 | String is CP437 encoded. |
FileOVERWRITE | When adding files: if file name exists, overwrite. |
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. |
ErrENCRNOTSUPP | Encryption method not supported. |
ErrRDONLY | Read-only archive. |
ErrNOPASSWD | No password provided. |
ErrWRONGPASSWD | Wrong password provided. |
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.