Class QArchive::MemoryCompressor
The QArchive::MemoryCompressor class helps to create archives in memory. This compresses files in archive formats supported by libarchive.
Header: | #include < QArchive/QArchive > |
qmake: | include(QArchive/QArchive.pri) |
Class Name | MemoryCompressor |
Namespace | QArchive |
Inherits: | QObject |
This class belongs to QArchive namespace, so make sure to include it.
Note: All functions in this class is reentrant.
Public Functions
MemoryCompressor(QObject *parent = nullptr, bool singleThreaded = true) |
MemoryCompressor(short, QObject *parent = nullptr, bool singleThreaded = true) |
Slots
void | setArchiveFormat(short) |
void | setPassword(const QString&) |
void | setBlockSize(int) |
void | addFiles(const QString&) |
void | addFiles(const QStringList&) |
void | addFiles(const QString&, const QString&) |
void | addFiles(const QStringList&, const QStringList&) |
void | removeFiles(const QString&) |
void | removeFiles(const QStringList&) |
void | removeFiles(const QString&, const QString&) |
void | removeFiles(const QStringList&, const QStringList&) |
void | clear(void) |
void | start(void) |
void | pause(void) |
void | resume(void) |
void | cancel(void) |
Signals
void | progress(QString, int, int, qint64, qint64) |
void | started(void) |
void | finished(QBuffer*) |
void | paused(void) |
void | resumed(void) |
void | canceled(void) |
void | error(short errorCode , const QString& file) |
Member Functions Documentation
MemoryCompressor(QObject *parent = nullptr , bool singleThreaded = true)
Constructs MemoryCompressor and sets the given QObject as the parent.
By default the MemoryCompressor is constructed without any parent and runs in a single thread.
The MemoryCompressor can be made to run in a different thread if singleThreaded
is set
to false.
Default archive format is ZIP.
QCoreApplication app(argc , argv);
// Default construction , no parent and single threaded
QArchive::MemoryCompressor Compressor;
// With parent and single threaded
auto PCompressor = new QArchive::MemoryCompressor(&app);
// With parent and non-single threaded
auto PDCompressor = new QArchive::MemoryCompressor(&app , false);
MemoryCompressor(short archiveFormat , QObject *parent = nullptr , bool singleThreaded = true)
This is an overloaded constructor. This a short integer which is assumed to be the format of the archive to be written with respect to the archive format codes.
QCoreApplication app(argc , argv);
// no parent and single threaded
QArchive::MemoryCompressor Compressor(QArchive::SevenZipFormat);
// With parent and single threaded
auto PCompressor = new QArchive::MemoryCompressor(QArchive::SevenZipFormat , &app);
// With parent and non-single threaded
auto PDCompressor = new QArchive::MemoryCompressor(QArchive::SevenZipFormat ,
&app ,
false);
void setArchiveFormat(short archiveFormat)
[SLOT]
Sets the format of the archive which is to be written with respect to the archive format codes.
QArchive::MemoryCompressor Compressor;
Compressor.setArchiveFormat(QArchive::SevenZipFormat);
void setPassword(const QString &password)
[SLOT]
Sets password for the archive which is to be written. Only Zip format is supported for now.
QArchive::MemoryCompressor Compressor;
Compressor.setArchiveFormat(QArchive::ZipFormat);
Compressor.setPassword("Test123"); // Ignored if format is not zip.
void setBlockSize(int size)
[SLOT]
Sets the blocksize for the archive which is to be written.
void addFiles(const QString &file)
[SLOT]
Adds a single file to the archive which is to be written. The entry name for this file will be assumed as QFileInfo(file).fileName()
void addFiles(const QStringList &files)
[SLOT]
Adds a list of file(s) to the archive which is to be written. For each file in the list the entry name will be assumed as QFileInfo(file).fileName(). If entry name already exists in the compressor then it will be ignored silently.
void addFiles(const QString &entryName , const QString &file)
[SLOT]
Adds a single file to the archive which is to be written under the given entry name.
QArchive::MemoryCompressor Compressor(QArchive::SevenZipFormat);
Compressor.addFiles(/*entry name =*/"Data.txt" ,
/*Actual path=*/"Data/Test.txt");
/*
* Data/Test.txt will be compressed in the archive
* as Data.txt without any parent since the entry
* name also does not contain any.
*/
void addFiles(const QStringList &entryName , const QStringList &file)
[SLOT]
Adds a list of file(s) to the archive which is to be written under the given corresponding entry name(s).
QArchive::MemoryCompressor Compressor(QArchive::SevenZipFormat);
Compressor.addFiles(/*entry name(s)=*/ QStringList() << "Data.txt"
<< "Data2.txt" ,
/*Actual path=*/ QStringList() << "Data/Test.txt"
<< "Data/Test2.txt"
);
/*
* "Data/Test.txt" file will be compressed as "Data.txt" and
* "Data/Test2.txt" file will be compressed as "Data2.txt".
*/
void removeFiles(const QString &file)
[SLOT]
Removes a single file from the archive which is to be written.
As of v2.1.x this file string is assumed to refer the entry name. In previous versions this file refers to the path of the file as given in addFiles()
void removeFiles(const QStringList &files)
[SLOT]
Removes a list of file(s) from the archive which is to be written.
As of v2.1.x this file list is assumed to refer the entry name. In previous versions this file refers to the path of the file as given in addFiles()
void removeFiles(const QString &entryName , const QString &file)
[SLOT]
(OBSOLETE)
Removes a single file from the archive which is to be written with respect to the entry name.
QArchive::MemoryCompressor Compressor(QArchive::SevenZipFormat);
Compressor.addFiles(/*entry name =*/"Data.txt" ,
/*Actual path=*/"Data/Test.txt");
/*
* Has to be same as addFiles data in order to
* remove it. (OBSOLETE, not needed in v2.1.x)
*/
Compressor.removeFiles(/*entry name =*/ "Data.txt",
/*Actual path=*/ "Data/Test.txt");
// As of v2.1.x the actual path is not actually respected at all.
// Only the entry name is used. So it's better to just use
// removeFiles("Data.txt");
As of v2.1.x you don't need to know the path of the file you added. Only entry name is respected. This function continues to work but it's not recommended. The given path is ignored.
void removeFiles(const QStringList &entryName , const QStringList &file)
[SLOT]
(OBSOLETE)
Removes a list of file(s) from the archive which is to be written with respect to the corresponding entry name(s).
QArchive::MemoryCompressor Compressor("Test.7z");
Compressor.addFiles(/*entry name(s)=*/ QStringList() << "Data.txt"
<< "Data2.txt" ,
/*Actual path=*/ QStringList() << "Data/Test.txt"
<< "Data/Test2.txt"
);
/* You have to use the exact same data to remove it. */
Compressor.removeFiles(/*entry name(s)=*/ QStringList() << "Data.txt"
<< "Data2.txt" ,
/*Actual path=*/ QStringList() << "Data/Test.txt"
<< "Data/Test2.txt"
);
// As of v2.1.x the actual path is not actually respected at all.
// Only the entry name is used. So it's better to just use
// removeFiles(QStringLists() << "Data.txt" << "Data2.txt");
As of v2.1.x you don't need to know the path of the file you added. Only entry name is respected. This function continues to work but it's not recommended. The given path is ignored.
void clear(void)
[SLOT]
Clears the internal cache.
void start(void)
[SLOT]
Starts the compression of the archive.
void pause(void)
[SLOT]
Pauses the compression of the archive. This slot is async and thus you need to wait for the paused signal , Which confirms that the pause call was successfull.
void resume(void)
[SLOT]
Resumes the compression of the archive. This slot is async and thus you need to wait for the resumed signal, Which confirms that the resume call was successfull.
void cancel(void)
[SLOT]
Cancels the compression of the archive. This slot is async and thus you need to wait for the canceled signal , Which confirms that the cancel call was successfull.
void progress(QString file , int processedEntries , int totalEntries , qint64 bytesProcessed, qint64 bytesTotal)
[SIGNAL]
Emits the progress of the compression.
The signal emits a total of 5 parameters in which the first one is the file that has been compressed , the second is the number of files that are compressed , the third is the number of total files that is stagged for compression, the fourth is the number of bytes processed and the last one is the total number of bytes required for this compression.
void started(void)
[SIGNAL]
Emitted when the compression is started.
void finished(QBuffer *archive)
[SIGNAL]
Emitted when the compression is finished successfully. The argument is a QBuffer pointer which is the newly created archive in memory. It is closed by default. This QBuffer* is not owned by the memory compressor. You have to delete it with deleteLater().
void paused(void)
[SIGNAL]
Emitted when the compression is paused successfully.
void resumed(void)
[SIGNAL]
Emitted when the compression is resumed successfully.
void canceled(void)
[SIGNAL]
Emitted when the compression is canceled successfully.
errorCode , const QString& file)
error(short[SIGNAL]
Emitted when something goes wrong with an archive. Refer the error codes.