Note that one must always define a name when adding a file entry, otherwise the compresser can not decide if the file should be added with an absolute or a relative path.
Assume you are adding the file <*c:\\data\\hello.txt*> twice to a Zip:
// MUST use binary!
std::ofstream out("test.zip", std::ios::binary);
Compress c(out, true);
Poco::Path aFile("c:\\data\\hello.txt");
c.addFile(theFile, "hello.txt");
c.addFile(theFile, theFile);
c.close(); // MUST be done to finalize the Zip file
----
A Zip file stores entries internally in UNIX path style. The Zip file created above will contain the following entries:
hello.txt
data/
data/hello.txt
----
The directory entry <*data/*> was automatically added.
When adding directories recursively, the same principle applies. You specify the root directory that should be added and an optional path name where entries are added in the Zip file.
Assume you have the following directory structure:
data/
run1/
result1.txt
run2/
result2.txt
----
The following call will add all subdirectories and all files of data to the Zip but not the root entry <*data*>:
Poco::Path data("data");
data.makeDirectory();
c.addRecursive(data);
----
Or if you want the files to be added under the directory name <*20070401*> (you basically <*rename*> data to 20070401):
Note that <*makeDirectory*> does not create a directory, it simply assures that the Path is treated as a directory not as a file.
Also using NORMAL compression instead of MAXIMUM (which is the default) has the benefit of improved performance.
To get notified about the entries that are added during <*addRecursive*> register to the <!EDone!> event of the Compress object:
Poco::FIFOEvent<const ZipLocalFileHeader> EDone;
----
* Closing the Zip file: It is mandatory to manually close Compress objects. This guarantees that the Zip directory is written, thus creating a valid Zip file. It is safe to call <*close*> multiple times, only the first call takes effect.
If <*flattenDirs*> is set to true no subdirs are extracted, if <*keepIncompleteFiles*> is set to true, corrupt files (i.e. wrong CRC, wrong size on disk) will not be deleted.
!Decompress Single Files
To decompress single files you must first parse a Zip file, and then decompress the file which happens transparently inside the <*ZipInputStream*>:
std::ifstream inp("test.zip", std::ios::binary);
poco_assert (inp);
ZipArchive arch(inp);
ZipArchive::FileHeaders::const_iterator it = arch.findHeader("data/hello.txt");