3

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!

Daniel
  • 631
  • 2
  • 7
  • 14
  • There's a couple linked [in this post](http://stackoverflow.com/questions/5362250/what-easy-zlib-tutorials-are-there) – Kevin Dec 01 '11 at 03:51
  • 1
    I have checked these links:1)http://bobobobo.wordpress.com/2008/02/23/how-to-use-zlib/ 2)http://www.codeguru.com/cpp/cpp/algorithms/compression/article.php/c11735, single file compress/decompress only. Thanks any way. :) – Daniel Dec 01 '11 at 04:07
  • What I wanna do here is: zip multi-files locally and send buffers through a pipe (tcp or udp for example) and then save zipped file at the opposite side of zipper. – Daniel Dec 01 '11 at 04:30

2 Answers2

7

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.

caf
  • 233,326
  • 40
  • 323
  • 462
  • OK, I will check this lib later. Is there any simple functions, like add file head or so to simply use zlib? – Daniel Dec 01 '11 at 04:14
  • @Daniel: Only what you roll yourself. – caf Dec 01 '11 at 04:43
  • 2
    ...or if you look around, there's stuff like minizip built on zlib: http://www.winimage.com/zLibDll/minizip.html – HostileFork says dont trust SE Dec 01 '11 at 07:08
  • Thanks caf. I have checked libtar. Yes, it can pack multi-files. And demo code is really easy to handle. And I think that I didn't make myself much more clearly. So I have add a description about what I wanna do (see above updated question). I need to pack multi-files locally and send to remote PC. But I don't wanna any temporarily files. Is there any way to do so? – Daniel Dec 01 '11 at 07:08
  • minizip seems like a windows lib. Is that ture? I'm on Linux platform. – Daniel Dec 01 '11 at 07:12
  • Anyway, It seems that it's quite clear here. two problems here. compress/decompress, zip file format. ^_^ – Daniel Dec 01 '11 at 08:29
5

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.

Sebastian
  • 1,243
  • 1
  • 18
  • 34
  • Awesome work @sebastian, Simple to use and works smoothly without giving any pains for the user. – iamrameshkumar Jun 24 '17 at 20:44
  • 1
    @Ram thanks, glad you find it useful. Make sure to check out the latest changes, now it compiles on almost every platform, macOS, unix, windows, embbeded systems. – Sebastian Jun 25 '17 at 02:35