ho la necessità di leggere e decomprimere un file gz e poi assegnare il contenuto decompresso a una stringa (buf).
attualmente sto usando questo codice , ma la stringa finale contiene un risultato parziale , non tutto il file gz viene decompresso.
(ad esempio se il file decompresso dovrebbere contenere la stringa "ciao a tutti" , buf contiene solo "ciao a")
Codice: Seleziona tutto
void Extract(string path,string dir )
{
string buf;
gzFile file;
file = gzopen ( path.c_str() , "r");
if (! file)
{
cerr << "gzip error: " << path << ": " << strerror(errno) << "\n";
exit(1);
}
while (1) {
int err;
int bytes_read;
unsigned char buffer[LENGTH];
bytes_read = gzread (file, buffer, LENGTH - 1);
buffer[bytes_read] = '\0';
buf += (char*)buffer;
if (bytes_read < LENGTH - 1)
{
if (gzeof (file)) {
break;
}
else {
const char * error_string;
error_string = gzerror (file, & err);
if (err) {
cerr << "error: " << error_string << "\n";
exit(1);
}
}
}
}
gzclose (file);
Bfa P;
P.Set(buf);
P.Extract(dir);
}
