How to write a zip file using Haskell LibZip?

limp_chimp

I'm trying to figure out a dead-simple task using LibZip in Haskell: how do I open an archive foo.zip, decompress it, recompress it, and save it to a new archive bar.zip? With the Zip library, this is easy:

{-# LANGUAGE OverloadedStrings #-}

import Codec.Archive.Zip (toArchive, fromArchive)
import qualified Data.ByteString.Lazy as B
import System.Environment

saveZipAs :: FilePath -> FilePath -> IO () 
saveZipAs source dest = do
    arch <- fmap toArchive $ B.readFile source
    putStrLn "Archive info: " >> print arch
    B.writeFile dest $ fromArchive arch

LibZip, on the other hand, provides no clear way to do this (that I can see). It only seems to be able to instantiate a zip file with withArchive (which is an issue in and of itself, because a file you want to open might not be on disk), and I don't see a way to do any kind of "save as" operation, nor to extract the compressed bytes as a ByteString or otherwise (as in Zip). LibZip is supposedly faster than Zip, so I want to at least give it a try, but it seems much more obscure (and also impure, carrying around an IO everywhere it goes, where it is really only needed at the beginning and the end, if ever). Can anyone give me some tips?

Side note: it really boggles the mind how people can spend such huge amounts of time writing a library, only to document it so poorly that no one can use it. Library writers, please don't do this!

nponeccop

Your link is somehow to an old version of the library, and the very last version of the library seems to have haddock compilation bugs.

Here are file reading functions in a newer version:

http://hackage.haskell.org/package/LibZip-0.10.2/docs/Codec-Archive-LibZip.html#g:3

The reverse process seems to be addFile/sourceBuffer and related functions.

Here is full source code of zip repacking:

import Codec.Archive.LibZip
import Codec.Archive.LibZip.Types

main = readZip "foo.zip" >>= writeZip "bar.zip"

readZip :: FilePath -> IO [(FilePath, ZipSource)]
readZip zipName = withArchive [] zipName $ do
        nn <- fileNames []
        ss <- mapM (\n -> sourceFile n 0 (-1)) nn
        return $ zip nn ss

writeZip :: FilePath -> [(FilePath, ZipSource)] -> IO ()
writeZip zipName zipContent = withArchive [CreateFlag] zipName $ do
        mapM_ (uncurry addFile) zipContent

Few refactorings still can be done: liftM2 zip can be used in readZip, and function composition . in writeZip.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to Write a File to the Root Directory of a Zip File in Python

From Dev

Read lines from a file inside a zip archive using Haskell's zip-conduit

From Dev

C# - Using protobuf to write directly into a zip file

From Dev

How to read file from ZIP using InputStream?

From Dev

Extracting single file from zip archive using Haskell

From Dev

How to create a zip file using shell script?

From Dev

Haskell - write zip function using list comprehension

From Dev

How add a file to an existing zip file using Golang

From Dev

How to create zip file using TrueZip?

From Dev

Write at the beginning of a file in Haskell

From Dev

How to unzip a zip file using scala?

From Dev

How write 'Ñ' in a file using python

From Dev

How to make a .zip file using zlib

From Dev

How to Write to a file using StreamWriter?

From Dev

Unzipping a file to disk using libzip

From Dev

How to let user download zip file using vaadin file downloader

From Dev

How to write zip file into httpresponse

From Dev

How to write a zip file using Haskell LibZip?

From Dev

IllegalStateException: zip file closed during file write

From Dev

how to create csvs and write to zip file in Google App Engine (python)?

From Dev

How to write received zip file content using ruby on rails

From Dev

Write to text file in IDE Haskell

From Dev

Unzipping with libzip loses file metadata

From Dev

How to write to a file using an FPGA

From Dev

How to set password to a zip file using Jquery

From Dev

Cpp force write zip using libzip

From Dev

How to download a zip file using the command line

From Dev

How to write (<*>) using (=<<) in Haskell

From Dev

Write surrogate pairs to file using Haskell

Related Related

HotTag

Archive