views:

179

answers:

4

Is there something like a class that might be used to store Files and directories in, just like the way Zip files might be used?

Since I haven't found any "real" class to write Zip files (real class as in real class), It would be nice to be able to store Files and Directories in a container-like file.

A perfect API would probably look like this:

int main()
{
    ContainerFile cntf("myContainer.cnt", ContainerFile::CREATE);
    cntf.addFile("data/some-interesting-stuff.txt");
    cntf.addDirectory("data/foo/");
    cntf.addDirectory("data/bar/", ContainerFile::RECURSIVE);
    cntf.close();
}

... I hope you get the Idea. Important Requirements are:

  • The Library must be crossplatform
  • anything *GPL is not acceptable in this case (MIT and BSD License are)

I already played with the thought of creating an Implentation based on SQLite (and its ability to store binary blobs). Unfortunately, it seems impossible to store Directory structures in a SQLite Database, which makes it pretty much useless in this case.

Is it useless to hope for such a class library?

A: 

You might like to check out http://www.cs.unc.edu/Research/compgeom/gzstream/

If you are making your own then redis may be a better choice than SQLite as I believe it handles binary data better.

shuttle87
gzip is not zip
nebukadnezzar
What is wrong with gz or tar or bz2 if you are looking for some other container format anyways?
sixlettervariables
@nebukadnezzar: Edit your question to state that you want to store your files in zip format specifically. With mention of storing things in SQLite it seemed as though you were asking for any data file format to store persistent data.
shuttle87
@sixlettervariables: neither bzip2 nor gzip are container formats. read up on bzip2 and gzip, and how they're implemented.
nebukadnezzar
@shuttle87: I never mentioned I'd want to use zipfiles exclusively. using zipfiles was merely an example.
nebukadnezzar
Your zip example threw me, `libtar` is a good choice then. However, I doubt you'll have a C++ library for it. HDF5 is another choice, as it stores hierarchical data easily, it just won't have wide support.
sixlettervariables
@sixlettervariables: I tried to write a C++ wrapper around libtar once, but libtar is *super*lowlevel. abstracting it is certainly not a trivial task. It is beyond me why there is no java.util.zip.ZipFile in C++ yet. Qazip is nice, but depends completely on Qt (which is not an option for applications that don't use Qt at all). zziplib is oddly implemented and seems to be GPL'd (or LGPL'd - both are not an option right now). Creating archives in C++ is just a major pain in the ass, really.
nebukadnezzar
+2  A: 

In an SQLite db you can store directory-like structures... you just have to have a "Directories" table, with one entry for each directory, having at least an index and a "parent" field (which holds the index of another directory, or 0 if it has no parent). Then you can have a "Files" table, which contains file attributes, the index of the parent directory and the file content.

That's it, now you have your directory tree in a relational DB.

Matteo Italia
+2  A: 

Someone pointed me to PhysicsFS, which has an API similar to what you describe, but it's a pure C API that does everything you need. A trivial object-oriented wrapper could be easily written.

greyfade
PhysicsFS looks useful :)
Chris Dennett
nice, it looks very promising! thank you for the link!
nebukadnezzar
it looks like you can only read archives with it, but not create them... ah well.
nebukadnezzar
@nebukadnezzar: Discussing it with the guy that mentioned it, it could probably be hacked to add that feature, it's what I wanted as well. But it's close enough to give it a good look.
greyfade
A: 

I took the time to write a tiny, yet working wrapper around libarchive. I'm not exactly familiar with all features of Libarchive, but the result fits what I needed:

archive_wrapper.cpp @ gist.github.com

It uses libmars for strings, etc. But I guess it wouldn't be too hard to replace the mars::mstring occurances with std::string. And of course this wrapper is available under the MIT/X11 License (just as libmars), which means you can do whatever you want with it. ;-)

nebukadnezzar
I know it's evil and everything, but since it's the only solution that actually fits what I needed.. I accepted my own answer. I'm still very grateful for all posted answers!
nebukadnezzar