Ref T258, added compress utils for better encapsulation

This commit is contained in:
Klaus Basan
2018-02-22 13:53:13 +01:00
parent 131e39db93
commit e53260a817
3 changed files with 142 additions and 9 deletions

View File

@@ -12,6 +12,7 @@
#include "blackcore/webdataservices.h"
#include "blackmisc/logmessage.h"
#include "blackmisc/fileutils.h"
#include "blackmisc/compressutils.h"
using namespace BlackMisc;
using namespace BlackMisc::Json;
@@ -344,16 +345,10 @@ namespace BlackCore
if (!ok) break; // malformed size
if (size < 1) break;
// Length header, unsigned, big-endian, 32-bit integer
QByteArray lengthHeader;
QDataStream stream(&lengthHeader, QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::BigEndian);
stream << size;
QByteArray ba;
ba.append(content.mid(contentIndex));
ba = QByteArray::fromBase64(ba);
ba.insert(0, lengthHeader); // adding 4 bytes length header
ba.insert(0, CCompressUtils::lengthHeader(size)); // adding 4 bytes length header
byteData = qUncompress(ba);
}
while (false);
@@ -399,13 +394,13 @@ namespace BlackCore
QHttpPart CDatabaseUtils::getJsonTextMultipart(const QJsonObject &json, bool compress)
{
const QByteArray bytes(QJsonDocument(json).toJson(QJsonDocument::Compact));
return getJsonTextMultipart(bytes, compress);
return CDatabaseUtils::getJsonTextMultipart(bytes, compress);
}
QHttpPart CDatabaseUtils::getJsonTextMultipart(const QJsonArray &json, bool compress)
{
const QByteArray bytes(QJsonDocument(json).toJson(QJsonDocument::Compact));
return getJsonTextMultipart(bytes, compress);
return CDatabaseUtils::getJsonTextMultipart(bytes, compress);
}
QHttpPart CDatabaseUtils::getJsonTextMultipart(const QByteArray &bytes, bool compress)

View File

@@ -0,0 +1,95 @@
/* Copyright (C) 2018
* swift project Community / Contributors
*
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
#include "compressutils.h"
#include "blackconfig/buildconfig.h"
#include "fileutils.h"
#include "directoryutils.h"
#include "stringutils.h"
#include <QFileInfo>
#include <QProcess>
using namespace BlackConfig;
namespace BlackMisc
{
QByteArray CCompressUtils::lengthHeader(qint32 size)
{
// Length header, unsigned, big-endian, 32-bit integer
QByteArray lengthHeader;
QDataStream stream(&lengthHeader, QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::BigEndian);
stream << size;
Q_ASSERT_X(lengthHeader.size() == 4, Q_FUNC_INFO, "Wrong header size");
return lengthHeader;
}
bool CCompressUtils::zip7Uncompress(const QString &file, const QString &directory, bool wait, QStringList *stdOutAndError)
{
const QFileInfo fi(file);
if (!fi.exists()) { return false; }
const bool win = CBuildConfig::isRunningOnWindowsNtPlatform();
const QString d =
d.isEmpty() ? d :
win ? CFileUtils::toWindowsLocalPath(directory) : directory;
const QString f = win ? CFileUtils::toWindowsLocalPath(file) : file;
// 7za.exe x -o"P:\Temp\XPlane" c:\Users\Foo\Downloads\xswiftbus-allos-0.8.4.802111947.7z
const QString cmd = win ? QStringLiteral("7za.exe") : QStringLiteral("7za");
QStringList args;
args << "x";
args << "-aoa";
if (!d.isEmpty()) { args << "-o" + d; }
args << f;
if (wait)
{
QProcess zipProcess;
zipProcess.start(cmd, args);
const bool finished = zipProcess.waitForFinished();
if (zipProcess.exitStatus() != QProcess::NormalExit) { return false; }
if (!finished) { return false; }
const int r = zipProcess.exitCode();
const QString pStdout = zipProcess.readAllStandardOutput();
const QString pStderr = zipProcess.readAllStandardError();
if (stdOutAndError)
{
stdOutAndError->clear();
stdOutAndError->push_back(pStdout);
stdOutAndError->push_back(pStderr);
}
return r == 0;
}
else
{
QProcess *p = new QProcess();
p->start(cmd, args);
return true;
}
}
bool CCompressUtils::hasZip7()
{
// just display info
const bool win = CBuildConfig::isRunningOnWindowsNtPlatform();
const QString cmd = win ? QStringLiteral("7za.exe") : QStringLiteral("7za");
QStringList args;
args << "i";
QProcess zipProcess;
zipProcess.start(cmd, args);
const bool finished = zipProcess.waitForFinished();
if (zipProcess.exitStatus() != QProcess::NormalExit) { return false; }
if (!finished) { return false; }
const int r = zipProcess.exitCode();
return r == 0;
}
} // ns

View File

@@ -0,0 +1,43 @@
/* Copyright (C) 2018
* swift project Community / Contributors
*
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
//! \file
#ifndef BLACKMISC_COMPRESSUTILS_H
#define BLACKMISC_COMPRESSUTILS_H
#include "blackmisc/blackmiscexport.h"
#include <QByteArray>
#include <QStringList>
namespace BlackMisc
{
//! Compress utilities
class BLACKMISC_EXPORT CCompressUtils
{
public:
//! Length header
//! \remark 4 bytes -> 32bit
static QByteArray lengthHeader(qint32 size);
//! Unzip my using 7zip
//! \remark relies on external 7zip command line
static bool zip7Uncompress(const QString &file, const QString &directory, bool wait, QStringList *stdOutAndError = nullptr);
//! External program existing?
//! \remark relies on external 7zip command line
static bool hasZip7();
private:
//! Ctor
CCompressUtils() {}
};
} // ns
#endif // guard