QDataStream. How to skip data using serialization?
-
Could somebody advise me how to skip data using serialized stream?
There is method "skip", but it needs just amount of raw bytes. I would like to use just Qt serialization mechanism.
Some details:
-
What am I trying to do?
I want to implement small file container which encapsulates few files inside.
!http://s22.postimg.org/j8g8roukx/FAT_schema.png(file container)! -
What is problem?
That's reading of the certain file by index. For example, skip QByteArray's (from 0 to index-1) and read just QByteArray at "index" position. -
Some proof code
@#include <QFile>
#include <QDataStream>
static const int QDataStreamVersion = QDataStream::Qt_4_8;
static const quint32 Magic = 0xA0B0C0D0;
static const quint32 Version = 1;struct FatItem
{
QString fname;
quint32 index;
};QDataStream & operator>> (QDataStream & _stream, FatItem & _fatItem)
{
_stream >> _fatItem.fname;
_stream >> _fatItem.index;
return _stream;
}void openContainerAndExtractAllFiles(QString _containerPath, QString _destinationDir)
{
//open storage file
QFile container(_containerPath);
if(!container.open(QIODevice::ReadOnly))
{
qDebug() << "container"<< _containerPath << " error=" << container.errorString();
return;
}// assign stream to file QDataStream stream(&container); //check for key number quint32 magic; stream >> magic; if (magic != Magic) { qDebug() << "Storage file: bad format"; return; } //check for correct version of the storage file qint32 version; stream >> version; if (version < Version ) { qDebug() << "file version is too old"; return; } else if (version > Version ) { qDebug() << "file version is too new"; return; } // load FAT QList<FatItem> fat; stream >> fat; // extract all files and put them to destination dir for (quint32 i=0; i<fat.count() && !stream.atEnd(); i++) { // extract QByteArray fData; stream >> fData; // put QFile f(_destinationDir + fat.at(i).fname); if (!f.open(QIODevice::WriteOnly))continue; f.write(fData); f.close(); } container.close();
}@
-
-