Commits

Sergey Astanin committed ab4950b

LibZip-0.0.2 API changes

* added readZipFileHead which reads only first n bytes from the file;
* added readZipFile' and readZipFileHead' (prime-versions)
which return [Word8] directly instead of building strict ByteStrings;
* export also low-level API similar to C library API (but without zip_ prefix);
* re-export Word8 type;

  • Participants
  • Parent commits 447b310
  • Tags 0.0.2

Comments (0)

Files changed (1)

File Codec/Archive/LibZip.chs

 -- | Partial Haskell bindings to @libzip@ library.
 -- These bindings provide read-only access to zip archives.
 -- Produced with @c2hs@ for @libzip@ ver. 0.9.
+--
+-- API is not stabilized yet, especially high-level part. If you use it, please
+-- let me know. Feedback and suggestions are welcome.
 module Codec.Archive.LibZip
   (-- * Types
    Zip,ZipFile,OpenFlag(..),FileFlag(..),ZipError(..)
-   -- * High-level Haskell functions
-   ,withZip,getFiles,getFileSize,readZipFile
+   ,Word8
+   -- * High-level interface
+   -- These functions are intended to reduce boiler-plate. They acquire and
+   -- release relevant resources automatically (open and close archives, files).
+   ,withZip,getFiles,getFileSize
+   ,readZipFile,readZipFile',readZipFileHead,readZipFileHead'
+   -- * Low-level direct bindings to @libzip@ functions
+   -- These functions correspond to their C namesakes with @zip_@ prefix.
+   -- Please refer to @libzip@ documentation to learn about their semantics.
+   ,open,close,get_num_files,get_name
+   ,fopen,fclose,fread
    -- * Utilities
    ,catchZipError,isFile,isDir
   ) where
   } deriving (Show,Eq) #}
 
 -- | Flags for 'getFiles', 'getFileSize', 'readZipFile', ...
+-- Please consult @libzip@ documentation about their use.
 {# enum define FileFlag
   { ZIP_FL_NOCASE as FileNOCASE
   , ZIP_FL_NODIR as FileNODIR
       then E.throwIO ErrINTERNAL -- FIXME
       else fromIntegral <$> {# get zip_stat_t->size #} st
 
--- | Read uncompressed file from the archive.
+-- | Read uncompressed file from the archive. Produce a strict ByteString.
 readZipFile :: Zip -- ^ zip archive
          -> String -- ^ name of the file in the archive
          -> [FileFlag] -- ^ file name mode
          -> IO B.ByteString
-readZipFile z fname flags = do
+readZipFile z fname flags = return . B.pack =<< readZipFile' z fname flags
+
+-- | Read uncompressed file from the archive. Produce a list of 'Word8'.
+readZipFile' :: Zip -- ^ zip archive
+         -> String -- ^ name of the file in the archive
+         -> [FileFlag] -- ^ file name mode
+         -> IO [Word8]
+readZipFile' z fname flags = do
   sz <- getFileSize z fname flags
+  readZipFileHead' z fname flags sz
+
+-- | Read beginning of the uncompressed file from the archive. Produce a list of 'Word8'.
+readZipFileHead' :: Zip -- ^ zip archive
+         -> String -- ^ name of the file in the archive
+         -> [FileFlag] -- ^ file name mode
+         -> Int -- ^ how many bytes to read
+         -> IO [Word8]
+readZipFileHead' z fname flags n = do
   f <- fopen z fname flags
-  bytes <- fread f sz
+  bytes <- fread f n
   fclose f
-  return $ B.pack bytes
+  return bytes
+
+-- | Read beginning of the uncompressed file from the archive. Produce a strict ByteString.
+readZipFileHead :: Zip -- ^ zip archive
+         -> String -- ^ name of the file in the archive
+         -> [FileFlag] -- ^ file name mode
+         -> Int -- ^ how many bytes to read
+         -> IO B.ByteString
+readZipFileHead z fname flags n = return . B.pack =<< readZipFileHead' z fname flags n
 
 -- Helpers
 
 isDir :: String -> Bool
 isDir = not . isFile
 
+lastMay :: [a] -> Maybe a
 lastMay [] = Nothing
 lastMay xs = Just $ last xs