Commits

Sergey Astanin committed 8a29e34

use zip_(file|dir)_add instead of deprecated API, expose add(File|Directory)WithFlags

  • Participants
  • Parent commits c579843
  • Branches 0.11-bindings-libzip

Comments (0)

Files changed (1)

File LibZip/Codec/Archive/LibZip.hs

     , fileStat, fileStatIx
     , deleteFile, deleteFileIx
     , renameFile, renameFileIx
-    , addFile, addDirectory
+    , addFile, addFileWithFlags
+    , addDirectory, addDirectoryWithFlags
     , replaceFile, replaceFileIx
     , sourceBuffer, sourceFile, sourceZip
     , PureSource(..), sourcePure
 addFile :: FilePath   -- ^ Name of the file to create.
         -> ZipSource  -- ^ Source where file data is obtained from.
         -> Archive Int  -- ^ Position index of the new file.
-addFile name src = do
+addFile name src =
+  let utf8name = UTF8.fromString name
+  in  addFileWithFlags [FileENC_UTF_8] utf8name src
+
+addFileWithFlags
+    :: [FileFlag]   -- ^ Can be a combination of 'FileOVERWRITE' and/or one of
+                    -- filename encoding flags: 'FileENC_GUESS' (default),
+                    -- 'FileENC_UTF_8', 'FileENC_CP437'.
+    -> BS.ByteString   -- ^ Name of the file to create.
+    -> ZipSource       -- ^ Source where file data is obtained from.
+    -> Archive Int     -- ^ Position index of the new file.
+addFileWithFlags flags namebytes src = do
   z <- getZip
-  lift $ withCString name $ \name' -> do
-    i <- c'zip_add z name' src
+  lift $ BS.useAsCString namebytes $ \name' -> do
+    i <- c'zip_file_add z name' src (combine flags)
     if i < 0
        then c'zip_source_free src >> get_error z >>= E.throwIO
        else return $ fromIntegral i
 -- | Add a directory to the archive.
 addDirectory :: FilePath     -- ^ Directory's name in the archive.
              -> Archive Int  -- ^ Position index of the new directory entry.
-addDirectory name = do
+addDirectory name =
+  let utf8name = UTF8.fromString name
+  in  addDirectoryWithFlags [FileENC_UTF_8] utf8name
+
+-- | Add a directory to the archive.
+addDirectoryWithFlags
+    :: [FileFlag]        -- ^ Can be one of filename encoding flags:
+                         -- 'FileENC_GUESS (default), 'FileENC_UTF_8', 'FileENC_CP437'.
+    -> BS.ByteString     -- ^ Directory's name in the archive.
+    -> Archive Int       -- ^ Position index of the new directory entry.
+addDirectoryWithFlags flags name = do
   z <- getZip
-  r <- lift $ withCString name $ c'zip_add_dir z
+  r <- lift $ BS.useAsCString name $
+       \name'-> c'zip_dir_add z name' (combine flags)
   if r < 0
      then lift $ get_error z >>= E.throwIO
      else return (fromIntegral r)