Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions QXlsx/header/xlsxzipwriter_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "xlsxglobal.h"

#include <QIODevice>
#include <QScopedPointer>
#include <QString>

class QZipWriter;
Expand All @@ -25,6 +26,7 @@ class ZipWriter
void close();

private:
QScopedPointer<QIODevice> m_deviceProxy;
QZipWriter *m_writer;
};

Expand Down
3 changes: 2 additions & 1 deletion QXlsx/source/xlsxdocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1365,7 +1365,8 @@ bool Document::saveAs(const QString &name) const
* \overload
* This function writes a document to the given \a device.
*
* \warning The \a device will be closed when this function returned.
* \warning The \a device will be closed when this function returns, except for
* QSaveFile. A QSaveFile remains open and must be committed by the caller.
*/
bool Document::saveAs(QIODevice *device) const
{
Expand Down
52 changes: 51 additions & 1 deletion QXlsx/source/xlsxzipwriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,55 @@
#include <private/qzipwriter_p.h>

#include <QDebug>
#ifndef QT_NO_TEMPORARYFILE
#include <QSaveFile>
#endif

QT_BEGIN_NAMESPACE_XLSX

namespace {

class NonClosingDevice : public QIODevice
{
public:
explicit NonClosingDevice(QIODevice *device)
: m_device(device)
{
if (device->isOpen())
QIODevice::open(device->openMode());
}

bool open(OpenMode mode) override
{
if (!m_device->isOpen() && !m_device->open(mode))
return false;
return QIODevice::open(m_device->openMode());
}

bool isSequential() const override { return m_device->isSequential(); }
qint64 pos() const override { return m_device->pos(); }
qint64 size() const override { return m_device->size(); }

bool seek(qint64 position) override
{
if (!m_device->seek(position))
return false;
return QIODevice::seek(position);
}

protected:
qint64 readData(char *data, qint64 maxSize) override { return m_device->read(data, maxSize); }
qint64 writeData(const char *data, qint64 maxSize) override
{
return m_device->write(data, maxSize);
}

private:
QIODevice *m_device;
};

} // namespace

ZipWriter::ZipWriter(const QString &filePath)
{
m_writer = new QZipWriter(filePath, QIODevice::WriteOnly);
Expand All @@ -16,7 +62,11 @@ ZipWriter::ZipWriter(const QString &filePath)

ZipWriter::ZipWriter(QIODevice *device)
{
m_writer = new QZipWriter(device);
#ifndef QT_NO_TEMPORARYFILE
if (qobject_cast<QSaveFile *>(device))
m_deviceProxy.reset(new NonClosingDevice(device));
#endif
m_writer = new QZipWriter(m_deviceProxy ? m_deviceProxy.data() : device);
m_writer->setCompressionPolicy(QZipWriter::AutoCompress);
}

Expand Down