PDA

View Full Version : Backing up a file.



munna
30th September 2006, 11:49
Hi,

I have a application that uses Sqlite to store data and also the option to backup this data.

I wrote the following code but is not working.




QString filter = tr("Database Backup (*.dbb)");
QString fileName = QFileDialog::getSaveFileName(this,tr("Backup Database"),QDir::homePath(),filter);

if(!fileName.isEmpty()){
QString filePath = QDir::homePath();
filePath.append("/database");
QFile sourceFile(filePath);
if(!sourceFile.open(QIODevice::ReadOnly)){
//Error Message
return;
}
QFile destFile(fileName);
if(!destFile.open(QIODevice::WriteOnly)){
//Error Message
return;
}
QTextStream sourceStream(&sourceFile);
QTextStream destStream(&destFile);
QByteArray data;
sourceStream>>data;
data = qCompress(data);
destStream<<data;
sourceFile.close();
destFile.close();
}



The destination file is getting created but it's size is only 10 Bytes.

Can someone please tell me what is wrong with the code ?

Thanks a lot.

wysota
30th September 2006, 11:55
Is this a text file? Because if not, you shouldn't use QTextStream. And even if it is a text file, you shouldn't use QTextStream with the stream operator (you'll lose whitespaces, etc.).

I guess this would be a bit better:


QByteArray data;
while(!sourceFile.atEnd()){
data = sourceFile.read(64*1024); // read 64kB
data = qCompress(data);
destFile.write(data);
}

munna
30th September 2006, 12:11
With your code, how will the reverting back of the data work ?

Can I use the following code to get back the original file




QByteArray umcompressedData;
while(!compressedFile.atEnd()){
umcompressedData = compressedFile.read(64*1024); // read 64kB. Can I do this ??
umcompressedData = qUncompress(umcompressedData);
destFile.write(umcompressedData);
}



I think reverting back the data will be a problem.

What can I do ?

Thanks.

wysota
30th September 2006, 13:06
Try to uncompress with 64k blocks. zlib works on streams, afaik, so there is a chance this will work. But if it doesn't just use sourceFile.readAll() instead of reading 64k at a time. It'll use more memory but will certainly work correctly when uncompressing.