Archive examples
Below are some examples of how you can use the Archive class to access archives.
- Creating a new archive
- Adding files to an existing archive
- Extracting files from an archive
- Iterating over an archive
- Manipulating an archive via QTextStream
- Loading a QDomDocument object directly from an archive
Creating a new archive
This example shows how to create a new archive and add a number of files to it.
bool createNewArchive( const QString& archiveName, const QStringList& fileNames )
{
return Archive( archiveName ).addFiles( fileNames );
}
Adding files to an existing archive
This example demonstrates how to add files to an existing archive. Notice that this archive has been encrypted with a password and that the archive name is set after the object is instantiated.
bool addFilesToArchive(const QString& archiveName, const QStringList& fileNames, const QString& password )
{
Archive a;
a.setArchiveName( archiveName );
if ( !password.isEmpty() )
{
a.setPassword( password );
}
return a.addFiles( fileNames );
}
Extracting files from an archive
The following example demonstrates how to extract all the files from an archive. These are extracted into the directory specified by the dir parameter.
bool decompressArchive( const QString& archiveName, const QString& dir )
{
return Archive( archiveName ).extractAll( dir );
}
Iterating over an archive
This example shows how to use the iterator to traverse an archive and extract files.
bool iteratorTest(const QString &archiveName)
{
Archive a( archiveName );
ArchiveIterator it;
for ( it = a.begin(); it != a.end(); ++it)
{
if ( !it->extractFile() )
{
qWarning("Failed to extract file %s", qPrintable(*it));
return false;
}
}
return true;
}
Manipulating an archive via QTextStream
In the following example a new archive is created and then 2 files are added to the archive using a QTextStream object.
In the latter half of the example the archive is re-opened and the first file is extracted.
bool textStream( QString& archiveName )
{
Archive a(archiveName);
// open so we can have access to an inner QIODevice
if ( !a.open())
{
qWarning("Failed to open archive %s", qPrintable(archiveName));
return false;
}
// Get a pointer to the QIODevice derived object for this archive
QIODevice* xz = a.device();
// Set the name of the first file in this archive
xz->setFileName("file1");
// Open the first file in the new archive
if ( !xz->open( QIODevice::WriteOnly ) )
{
qWarning("Archive - device open failed");
return false;
}
// Declare a QTextStream object operating on the QIODevice object
QTextStream ts(xz);
// Write some text to the file by streaming to the QTextStream object
ts << "Write some text to the text " << "stream." << endl;
ts << 12 << " more text" << endl;
ts.flush();
// Close the first file
xz->close();
// Set the file name for the second file
xz->setFileName("file2");
// Open the second file
if ( !xz->open( QIODevice::Append | QIODevice::WriteOnly ) )
{
qWarning("Archive - device open failed");
return false;
}
// Set the device for the second file to be added
ts.setDevice(xz);
// Write some text to the file
ts2 << "This is the text in the second file in this archive" << endl;
ts2.flush();
// Tidy up
a.close();
// Now re-open the archive to read back the first file from the archive we
// have just created.
if ( !a.open() )
{
qWarning("Failed to open archive %s", qPrintable(archiveName));
return false;
}
// Get a pointer to the QIODevice object for this archive
xz = z.device();
// Open the QIODevice for reading
if ( !xz->open( QIODevice::ReadOnly ) )
{
qWarning("Archive - device open failed");
return false;
}
// Set the device for reading from the archive
ts.setDevice(xz);
char tmp;
// Read the file in character by character
for ( uint i = 0; i < xz->size(); i++ )
{
ts >> tmp;
qWarning("%c", tmp);
}
// Tidy up
xz->close();
z.close();
return true;
}
Loading a QDomDocument object directly from an archive
In the following example an existing archive is opened and a XML file in the archive is loaded into QDomDocument object.
bool domDoc( QString& archiveName )
{
Archive a( archiveName );
// Allocate a DOM document and load the file named manifest.xml
QDomDocument doc;
doc.setContent( a.device( "manifest.xml" ) );
// ...
return true;
}