|
| 1 | +--- |
| 2 | +title: Announcing QArchive v2.1.x |
| 3 | +author: Antony J.r |
| 4 | +authorURL: https://antonyjr.in |
| 5 | +--- |
| 6 | + |
| 7 | +QArchive has come a long road. I would say that I never expected that it would actually live this long. In this **release** QArchive gets a very big upgrade. |
| 8 | + |
| 9 | +When this library was first created the only scope was hard drive based extraction and compression and libarchive seems to have a lot of optimization like zero-copy. But for a long time I've wished to have in-memory extraction and compression but was **too lazy**(sorry folks). |
| 10 | + |
| 11 | +In this release, **I present you with new classes to handle in-memory extraction and compression with ease**. Down below are the new classes which are added to the **QArchive namespace.** |
| 12 | + |
| 13 | + * QArchive::MemoryExtractor |
| 14 | + |
| 15 | + * QArchive::MemoryFile |
| 16 | + |
| 17 | + * QArchive::MemoryExtractorOutput |
| 18 | + |
| 19 | + * QArchive::MemoryCompressor |
| 20 | + |
| 21 | +Pleae see the documentation for detailed information on these classes. |
| 22 | + |
| 23 | +### A Little Demo |
| 24 | + |
| 25 | +Let me demonstrate you a small example where we compress and extract a **7Zip** archive in-memory and read the first file contents if it's text document. |
| 26 | + |
| 27 | +``` |
| 28 | +#include <QCoreApplication> |
| 29 | +#include <QDebug> |
| 30 | +#include <QBuffer> |
| 31 | +#include <QArchive/QArchive> |
| 32 | +/* #include <QArchive> // if you use add_subdirectory with cmake */ |
| 33 | +
|
| 34 | +int main(int argc, char **argv) { |
| 35 | + QCoreApplication app(argc, argv); |
| 36 | +
|
| 37 | + // Let's write our text document. |
| 38 | + QString text = QString::fromUtf8("Thanks for using QArchive."); |
| 39 | +
|
| 40 | + QBuffer document; |
| 41 | + document.open(QIODevice::WriteOnly); |
| 42 | + document.write(text.toLocal8Bit().constData()); |
| 43 | + document.close(); |
| 44 | +
|
| 45 | + QArchive::MemoryCompressor compressor(QArchive::SevenZipFormat); |
| 46 | + compressor.addFiles("Readme.txt", (QIODevice*)&document); |
| 47 | +
|
| 48 | + QIODevice *archive = nullptr; |
| 49 | + QArchive::MemoryExtractor extractor; |
| 50 | +
|
| 51 | + QObject::connect(&compressor, &QArchive::MemoryCompressor::finished, |
| 52 | + [&](QBuffer *a) { |
| 53 | + archive = (QIODevice*)a; |
| 54 | + extractor.setArchive(archive); |
| 55 | + extractor.start(); |
| 56 | + }); |
| 57 | +
|
| 58 | + QObject::connect(&extractor, &QArchive::MemoryExtractor::finished, |
| 59 | + [&](QArchive::MemoryExtractorOutput *output) { |
| 60 | + auto buffer = output->getFiles().at(0).buffer(); |
| 61 | + auto fileInfo = output->getFiles().at(0).fileInformation(); |
| 62 | +
|
| 63 | + buffer->open(QIODevice::ReadOnly); |
| 64 | +
|
| 65 | + qDebug() << "Filename:: " << fileInfo.value("FileName").toString(); |
| 66 | + qDebug() << "Contents:: " << QString(buffer.readAll()); |
| 67 | +
|
| 68 | + // Only delete the output object and |
| 69 | + // any other buffer you allocated. |
| 70 | + output->deleteLater(); |
| 71 | + archive->deleteLater(); |
| 72 | + app.quit(); |
| 73 | + }); |
| 74 | + return app.exec(); |
| 75 | +} |
| 76 | +``` |
| 77 | + |
0 commit comments