.zip file read/write

I was looking for a lib from which I can read and modify .zip files. After doing a bit of research, libzip seemed like the frontrunner:

http://nih.at/libzip/

However it has one fatal flaw: the only way to open a .zip is by supplying a path name. This means you cannot load from memory, or load from another .zip, or something like that. It also has a few other problems (like not being able to specify you want read-only permissions -- it always assumes you want write permissions). Plus, only taking a const char* path name makes it not Unicode friendly.

So it's totally a bust. Completely unacceptable for what I'm doing. Which saddens me because otherwise it was a really nice lib.

My question is -- is there another, similar lib which has what I need? Specifically I'm looking for:

- obtain a list of files in the .zip
- able to read multiple files in the .zip at a time
- add/replace files/directories (via custom callbacks)
- rename files/directories
- .zip file can be opened via custom callbacks (from memory, or by some other means)
- able to delete files/directories

I really don't want to have to write something myself. Any help appreciated!
I don't suppose there's any chance of converting the archive to a format that can be handled by a library that doesn't suck so hard? Like .7z?

Or how about adding a function to the library to let it load from a buffer? I don't think that should be too hard.
That library is for dealing with ZIP files -- you can use zlib directly...

However, check out the CodeProject Zip Utils:
http://www.codeproject.com/KB/files/zip_utils.aspx

Helios, what's wrong with zip?
helios: If you have a link to a lib which deals with .7z files I'd be happy to give it a whirl. I'm still interested in .zip though, due to it's wide usage. Also .7z is a joint archive format which isn't preferable in all situations (specifically the ones I'll be targetting). .7z decompresses a lot slower and I'll be needing to do this in realtime.

I don't really want to modify the library, either. Guess I'm just a wimp that way.

---------------------------
Duoas: I'm aware it's for .zip files. That's what I want. I'll definately check that link out. Thanks!


In any event I e-mailed libzip maintainers about my concerns. We'll see if anything comes of it.



EDIT --

Zip Utils falls short in many ways. Don't see a way to delete/rename files from the zip. TCHAR nonsense. No way to add a new file from a callback. Same problem as libzip -- can't open a .zip from a callback (though you can supply a raw buffer... that sort of works -- but ew)

Also it seems to be for Windows only (.cpp files #include <windows.h>) so it's pretty much worthless. I guess that explains the TCHAR and HANDLE crap though.
Last edited on
If you have a link to a lib which deals with .7z files I'd be happy to give it a whirl.
This has happened to me quite a few times, actually. I always assume there's one, for some reason.
De/Compression time is mostly affected by the algorithm used. LZMA is very resource-intensive, particularly when compressing, but it's also the best general purpose loss-less compression algorithm, today.

Guess I'm just a wimp that way.
Yes, yes you are. :-)

zlib definitely has some code to handle .zip (/contrib/minizip/), although I haven't checked its capabilities.
Last edited on
I've checked minizip in the zlib bundle. It's very minimal and is only good for reading, not modifying.

My main concern with modifying the library is submitting the changes to the maintainers. I've actually never done anything like that before =x. But I don't want to have to distribute a custom version of a lib with my lib.

I'm heading on a road trip to visit family on tuesday, and it'll take about a week. If I don't hear back by the libzip maintainers by then, and if no better lib surfaces, I'll try my hand at adding the functionality myself.


EDIT

actually -- I'll have another look at minizip. Maybe I just only looked at the unzip code?

EDIT2

Yeah whoops. There is zip code! Haw. Right under my nose the entire time. I'll skim this over tomorrow and see if I can't make use of it.

Thanks guys. Other lib suggestions still welcome. libs for other compression formats also welcome!
Last edited on
If you only need compression without archiving, libbz2 is worth checking out. Specially since I have a have a couple of functions here to abstract de/compression:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
char *compressBuffer_BZ2(char *src,unsigned long srcl,unsigned long *dstl){
	unsigned long l=srcl,realsize=l;
	char *dst=new char[l];
	while (BZ2_bzBuffToBuffCompress(dst,(unsigned int *)&l,src,srcl,1,0,0)==BZ_OUTBUFF_FULL){
		delete[] dst;
		l*=2;
		realsize=l;
		dst=new char[l];
	}
	if (l!=realsize){
		char *temp=new char[l];
		memcpy(temp,dst,l);
		delete[] dst;
		dst=temp;
	}
	*dstl=l;
	return dst;
}

char *decompressBuffer_BZ2(char *src,unsigned long srcl,unsigned long *dstl){
	unsigned long l=srcl,realsize=l;
	char *dst=new char[l];
	while (BZ2_bzBuffToBuffDecompress(dst,(unsigned int *)&l,src,srcl,1,0)==BZ_OUTBUFF_FULL){
		delete[] dst;
		l*=2;
		realsize=l;
		dst=new char[l];
	}
	if (l!=realsize){
		char *temp=new char[l];
		memcpy(temp,dst,l);
		delete[] dst;
		dst=temp;
	}
	*dstl=l;
	return dst;
}

Dirty, ugly, and inefficient? Yes, but it gets the job done.*

You could also combine libbz2 with libtar and get a middle ground between .zip and .7z.



*If you excuse the easy joke, "that's what women say about me".
Last edited on
Sorry, this was a case of something I googled that seemed to meet your parameters. I've always used zlib directly...

I emphasized 'files', not 'zip'. I don't think you are stupid.

It looks like at least one other person has had the same problem with libzip...
http://nih.at/listarchive/libzip-discuss/msg00006.html

Alas.
Last edited on
Topic archived. No new replies allowed.