PDA

View Full Version : Reading a compressed file



Shulberry
07-07-2008, 03:03 PM
Hello,

I would like to know the best way to read a text file into a string from a zipped archive. Browsing the reference docs it looks like an InputSource (http://www.clanlib.org/docs/clanlib-0.8.0/Reference/html/CL_InputSource.html) would be used but how would you get the raw data into a string when it will be different for some text files (ANSI/UNICODE etc.)?

Thanks,
shul.

Magnus Norddahl
07-09-2008, 12:45 PM
You cannot load any text file without knowing (or requiring) it to be in a specific text format.

But if we assume the text file is a 8 bit text file in the UTF-8 format, the 0.9 syntax would be:


CL_String load_text_file(CL_Zip_Archive &zip)
{
CL_IODevice file = zip.open_file("mytextfile.txt");
CL_String8 text_data(file.get_size(), ' ');
file.read(text_data.data(), text_data.length());
CL_String text = CL_StringHelp::utf8_to_text(text_data);
return text;
}

If the text file is in the local 8 bit character set, the 0.8 syntax is:


std::string load_text_file(CL_Zip_Archive &zip)
{
std::auto_ptr<CL_InputDevice> file(zip.open_file("mytextfile.txt"));
std::string text_data(file->size(), ' ');
file->read(text_data.data(), text_data.size());
return text_data;
}