mrkkrp / zip

Efficient library for manipulating zip archives
Other
81 stars 27 forks source link

How to add `mimetype` file as first file of the archive #81

Closed RamiroPastor closed 3 years ago

RamiroPastor commented 3 years ago

Hi, I've started using this package to create a .epub file and I must say it has been much smoother than the javascript library I tried previously (epub-zipper)

However, there is one small detail I cannot solve: the EPUB 3.2 specification states that

The first file in the OCF ZIP Container MUST be the mimetype file

And that it must not be compressed. I have achieved the "non-compressed" condition, but i don't know how to fulfill the "first file in the zip archive" condition. This is my code:

main :: IO ()
main = do
  mimetype <- mkEntrySelector "mimetype"
  createArchive "conmocion.epub" $ do
    packDirRecur Deflate (\s -> mkEntrySelector $ "META-INF/" ++ s) "./ePub/META-INF"
    packDirRecur Deflate (\s -> mkEntrySelector $ "es/" ++ s)       "./ePub/es"
    loadEntry    Store mimetype "./ePub/mimetype"

And I get the following error in the epub online validator

Mimetype file entry is missing or is not the first file in the archive.

Any idea on how to put the mimetype file as the first file in the archive?

RamiroPastor commented 3 years ago

Ok, found the solution by using the commit function from the package itself. However, I'm getting now another error because of an extra field of length 20 in the mimetype file. Depressing.

main :: IO ()
main = do
  mimetype <- mkEntrySelector "mimetype"
  createArchive "conmocion.epub" $ do
    addEntry Store "application/epub+zip" mimetype 
    commit
    packDirRecur Deflate (mkEntrySelector . ("META-INF/" ++)) "./ePub/META-INF"
    packDirRecur Deflate (mkEntrySelector . ("es/" ++)      ) "./ePub/es"
    packDirRecur Deflate (mkEntrySelector . ("assets/" ++)  ) "./ePub/assets"
mrkkrp commented 3 years ago

Does it say the name of the field?

RamiroPastor commented 3 years ago

No, and also when I run this test function:

test :: IO ()
test = do
  mimetype <- mkEntrySelector "mimetype"
  withArchive "conmocion.epub" $ do
    mDesc <- getEntryDesc mimetype
    liftIO $ putStrLn $ show $ edExtraField $ fromJust mDesc

The console output is:

ghci> test
fromList [(1,"")]
mrkkrp commented 3 years ago

I will take a look, latest this weekend.

RamiroPastor commented 3 years ago

That would be great, thanks.

mrkkrp commented 3 years ago

Can you try out #82 and see if it fixes the issue for you?

RamiroPastor commented 3 years ago

Works !!

Validación usando la versión de reglas epub 3.2.
No se han detectado errores o advertencias.
Mensajes: 0 errores fatales / 0 errores / 0 advertencias / 0 indicaciones

EPUBCheck completado

That means: 0 errors 👍

mrkkrp commented 3 years ago

Okay, I'll fix the failing test and release 1.7.2 but probably not today. Latest on Monday.

mrkkrp commented 3 years ago

Done, 1.7.2 is on Hackage.