Class QArchive::DiskCompressor
The QArchive::DiskCompressor class helps to create archives that based on hard drive, (i.e) The storage of the computer. This compresses files in archive formats supported by libarchive.
Header: | #include < QArchive/QArchive > |
qmake: | include(QArchive/QArchive.pri) |
Class Name | DiskCompressor |
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
DiskCompressor(QObject *parent = nullptr, bool singleThreaded = true) |
DiskCompressor(const QString&, QObject *parent = nullptr, bool singleThreaded = true) |
DiskCompressor(const QString&, short, QObject *parent = nullptr, bool singleThreaded = true) |
Slots
void | setFileName(const QString&) |
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(void) |
void | paused(void) |
void | resumed(void) |
void | canceled(void) |
void | error(short errorCode , const QString& file) |
Member Functions Documentation
DiskCompressor(QObject *parent = nullptr , bool singleThreaded = true)
Constructs DiskCompressor and sets the given QObject as the parent.
By default the DiskCompressor is constructed without any parent and runs in a single thread.
The DiskCompressor can be made to run in a different thread if singleThreaded
is set
to false.
QCoreApplication app(argc , argv);
// Default construction , no parent and single threaded
QArchive::DiskCompressor Compressor;
// With parent and single threaded
auto PCompressor = new QArchive::DiskCompressor(&app);
// With parent and non-single threaded
auto PDCompressor = new QArchive::DiskCompressor(&app , false);
DiskCompressor(const QString &archivePath , QObject *parent = nullptr , bool singleThreaded = true)
This is an overloaded constructor. This expects a QString which is assumed to be the file path of the archive which is to be written. In this case , The format is guessed using the given archive path. One can also explicitly set the format later using setArchiveFormat method.
QCoreApplication app(argc , argv);
QString archivePath("/tmp/SomeArchive.zip");
// Default construction , no parent and single threaded
QArchive::DiskCompressor Compressor(archivePath);
// With parent and single threaded
auto PCompressor = new QArchive::DiskCompressor(archivePath , &app);
// With parent and non-single threaded
auto PDCompressor = new QArchive::DiskCompressor(archivePath , &app , false);
DiskCompressor(const QString &archivePath , short archiveFormat , QObject *parent = nullptr , bool singleThreaded = true)
This is an overloaded constructor. This expects a QString which is assumed to be the file path of the archive which is to be written and a short integer which is assumed to be the format of the archive to be written with respect to the archive format codes. In this case , The format will not be guessed and uses the given archive format only regardless of the extension.
QCoreApplication app(argc , argv);
QString archivePath("/tmp/SomeArchive.data");
// Default construction , no parent and single threaded
QArchive::DiskCompressor Compressor(archivePath , QArchive::ZipFormat);
// With parent and single threaded
auto PCompressor = new QArchive::DiskCompressor(archivePath , QArchive::ZipFormat , &app);
// With parent and non-single threaded
auto PDCompressor = new QArchive::DiskCompressor(archivePath ,
QArchive::ZipFormat ,
&app ,
false);
void setFileName(const QString &archivePath)
[SLOT]
Sets the path to archive which is to be written.
QArchive::DiskCompressor Compressor;
Compressor.setFileName("Test.7z");
void setArchiveFormat(short archiveFormat)
[SLOT]
Sets the format of the archive which is to be written with respect to the archive format codes , regardless of the filename extension.
QArchive::DiskCompressor Compressor;
Compressor.setFileName("Test.data");
Compressor.setArchiveFormat(QArchive::ZipFormat);
void setPassword(const QString &password)
[SLOT]
Sets password for the archive which is to be written. Only Zip format is supported for now.
QArchive::DiskCompressor Compressor;
Compressor.setFileName("Test.data");
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::DiskCompressor Compressor("Test.7z");
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::DiskCompressor Compressor("Test.7z");
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::DiskCompressor Compressor("Test.7z");
Compressor.addFiles(/*entry name =*/"Data.txt" ,
/*Actual path=*/"Data/Test.txt");
/*
* Has to be same as addFiles data in order to
* remove it.
*/
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::DiskCompressor 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.
The total bytes might be zero if you are compressing empty files, so please mind that.
void started(void)
[SIGNAL]
Emitted when the compression is started.
void finished(void)
[SIGNAL]
Emitted when the compression is finished successfully.
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.