Tiwenty
Tiwenty

Reputation: 763

Get files (other than text) from .zip with libzip

I am learning C++ and decided to train me by making a little program that extract files from zip, like text files, images, or even other zip files (but I don't want to extract them directly, one thing a time) with the libzip library. So I made my program, but now I have a problem. It extracts well text files, but not files like images or zip. It detects them, gives me exact names and sizes, but once extracted, they are just a few bytes. (but they are located where they should).

Here is my code: http://pastie.org/6221955

So if someone could help me to extract files that aren't texts from zip, it would be great! Thank you!

Upvotes: 0

Views: 1414

Answers (2)

Udo Klein
Udo Klein

Reputation: 6882

The issue is with the << operator. You output a character array / string. Strings in C are null terminated. Thus the first binary 0 will terminate your output.

Upvotes: 1

Mahmoud Al-Qudsi
Mahmoud Al-Qudsi

Reputation: 29519

You're reading and writing binary data as a textual string. The problem is that strings use the presence of a NULL character (0-byte) to indicate end-of-string. Binary data can (and definitely does) contain zeros all over the place, not just at the end.

You need to use ofstream's .write (buffer, <size in bytes>) to write to the disk; by manually specifying the size in bytes, you force it read that many bytes instead of stopping at the first instance of a NULL character.

Upvotes: 2

Related Questions