Sergey Astanin avatar Sergey Astanin committed da1bc89

use UTF-8 for archive comments (should work on any platform) + test

Comments (0)

Files changed (2)

LibZip/Codec/Archive/LibZip.hs

 import Control.Monad.State.Strict
     (StateT(..), MonadState(..), MonadTrans(..), lift, liftM)
 import Foreign.C.Error (Errno(..), eINVAL)
-import Foreign.C.String (withCString, withCStringLen, peekCString)
+import Foreign.C.String (withCString, peekCString)
 import Foreign.C.Types (CInt, CULLong)
 import Foreign.Marshal.Alloc (alloca)
 import Foreign.Marshal.Array (allocaArray, peekArray, withArrayLen, pokeArray)
   | otherwise = return (-1)
 
 -- | Get zip archive comment.
-getComment :: [FileFlag]  -- ^ 'FileUNCHANGED' can be used.
+getComment :: [FileFlag]  -- ^ Can be a combination of 'FileUNCHANGED' and/or
+                          -- one of 'FileENC_GUESS' (default), 'FileENC_STRICT' (CP-437).
            -> Archive (Maybe String)
 getComment flags = do
   z <- getZip
          return (c,n)
   if  c == nullPtr
     then return Nothing
-    else lift $ peekCString c >>= return . Just . take (fromIntegral n)
+    else lift $ BS.packCStringLen (c, fromIntegral n) >>= return . Just . UTF8.toString
 
 -- | Set zip archive comment.
 setComment :: String   -- ^ Comment message.
            -> Archive ()
 setComment msg = do
   z <- getZip
-  r <- lift $ withCStringLen msg $ \(msg',i') ->
+  let utf8msg = UTF8.fromString msg
+  r <- lift $ BS.useAsCStringLen utf8msg $ \(msg',i') ->
        c'zip_set_archive_comment z msg' (fromIntegral i')
   if r < 0
      then lift $ get_error z >>= E.throwIO

LibZip/Tests/MonadicTests.hs

       -- Haskell LibZip reflects it by returning Just "" rather than Nothing.
       (c1, c2, c2_added, c2_removed) @?= (Just "", Just "", Just com, Just "")
 
+  , "set/get Unicode archive comment" ~: do
+      tmpzip <- getTmpFileName "test_LibZip_Unicode_comment.zip"
+      mkArchive tmpzip [("hello/",""), ("hello/world.txt", world_txt)]
+      let unicodeComment = "Привет, мир!"
+      withArchive [] tmpzip $ setComment unicodeComment
+      comment <- withArchive [] tmpzip $ getComment []
+      removeFile tmpzip
+      comment @?= (Just unicodeComment)
+
   , "set/get/remove file comment" ~: do
       tmpzip <- getTmpFileName "test_LibZip_file_comment.zip"
       let world_path = "hello/world.txt"
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.