简体   繁体   中英

How do I zip a directory or multple files with zlib, using C/C++?

I did search for this topic, but I didn't find any relevant clue for this.

Can anyone give me some tips or demo code that can solve the problem?

Thanks in advance.

---FYI---

What I wanna do here is to zip files and upload to remote PC. I think it'll take the following steps:

a) initialize a zipped file head and send to remote PC and save that zipped file head.

b) open file to read a portion of file data and zip the file data locally.

c) send zipped data through a pipe (tcp or udp for example) to remote PC.

d) save the data from pipe, which is zipped, on the remote PC.

e) if there are multiple files, come back to b)

e) when all files is zipped and transferred to remote PC, then close zipped file.

Two question here:

a) compress/decompress

b) File format

Thanks guys!

zlib zips a single stream. If you want to zip multiple files, you need to do one of two things:

  • Define a format (or use an existing format) that combines multiple files into one stream, then zip that; or
  • Zip each file individually, then use some format to combine those into one output file.

If you take the first option, using the existing tar format to combine the files, you will be producing a .tar.Z file which can be extracted with standard tools, so this is a good way to go. You can use libtar to generate a tar archive.

I have built a wrapper around minizip adding some features that I needed and making it nicer to use it. Is does use the latest c++11 and is developed using Visual Studio 2013 (should be portable, but i havent tested it on unix)

There's a full description here: https://github.com/sebastiandev/zipper

but is as simple as you can get:

Zipper zipper("ziptest.zip");
zipper.add("somefile.txt");
zipper.add("myFolder");
zipper.close();

you can zip entire folders, streams, vectors, etc. Also a nice feature is doing everything entirely in memory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM