swift
compressutils.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2018 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
4 #include "misc/compressutils.h"
5 
6 #include <QFileInfo>
7 #include <QProcess>
8 
9 #include "config/buildconfig.h"
10 #include "misc/fileutils.h"
11 #include "misc/stringutils.h"
12 #include "misc/swiftdirectories.h"
13 
14 using namespace swift::config;
15 
16 namespace swift::misc
17 {
18  QByteArray CCompressUtils::lengthHeader(qint32 size)
19  {
20  // Length header, unsigned, big-endian, 32-bit integer
21  QByteArray lengthHeader;
22  QDataStream stream(&lengthHeader, QIODevice::WriteOnly);
24  stream << size;
25  Q_ASSERT_X(lengthHeader.size() == 4, Q_FUNC_INFO, "Wrong header size");
26  return lengthHeader;
27  }
28 
29  bool CCompressUtils::zipUncompress(const QString &file, const QString &directory, QStringList *stdOutAndError)
30  {
31  const QFileInfo fi(file);
32  if (!fi.exists()) { return false; }
33  if (fi.suffix() != "zip")
34  {
35  if (stdOutAndError) { stdOutAndError->push_back("Not a zip file"); }
36  return false;
37  }
38 
40  const QString d = directory.isEmpty() ? directory : win ? CFileUtils::toWindowsLocalPath(directory) : directory;
41  const QString f = win ? CFileUtils::toWindowsLocalPath(file) : file;
42 
43  QProcess zipProcess;
44 
46  {
47  zipProcess.setProgram("powershell");
48  QStringList args;
49  args << "-Command";
50  args << "Expand-Archive";
51  args << "-Path" << f;
52  if (!d.isEmpty()) { args << "-DestinationPath" << d; }
53  args << "-Force";
54  zipProcess.setArguments(args);
55  }
56  else
57  {
58  zipProcess.setProgram("unzip");
59  QStringList args;
60  args << f;
61  if (!d.isEmpty()) { args << "-d" << d; }
62  zipProcess.setArguments(args);
63  }
64  return runZipProcess(&zipProcess, stdOutAndError);
65  }
66 
67  bool CCompressUtils::runZipProcess(QProcess *zipProcess, QStringList *stdOutAndError)
68  {
69  zipProcess->start();
70 
71  // If process does not even start, e.g. because unzip program found.
72  if (!zipProcess->waitForStarted())
73  {
74  if (stdOutAndError)
75  {
76  stdOutAndError->push_back("unzip");
77  stdOutAndError->push_back("Command not found");
78  }
79  return false;
80  }
81 
82  // If process does not finish. Very unlikely.
83  if (!zipProcess->waitForFinished())
84  {
85  if (stdOutAndError)
86  {
87  stdOutAndError->push_back("unzip");
88  stdOutAndError->push_back("Process did not finish.");
89  }
90  return false;
91  }
92 
93  if (stdOutAndError)
94  {
95  stdOutAndError->clear();
96  const QString pStdout = zipProcess->readAllStandardOutput();
97  const QString pStderr = zipProcess->readAllStandardError();
98  stdOutAndError->push_back(pStdout);
99  stdOutAndError->push_back(pStderr);
100  }
101 
102  return zipProcess->exitStatus() == QProcess::NormalExit && zipProcess->exitCode() == 0;
103  }
104 } // namespace swift::misc
static constexpr bool isRunningOnWindowsNtPlatform()
Running on Windows NT platform?
static QByteArray lengthHeader(qint32 size)
Length header.
Free functions in swift::misc.
qsizetype size() const const
void setByteOrder(QDataStream::ByteOrder bo)
bool exists(const QString &path)
QString suffix() const const
void clear()
void push_back(QList< T >::parameter_type value)
int exitCode() const const
QProcess::ExitStatus exitStatus() const const
QByteArray readAllStandardError()
QByteArray readAllStandardOutput()
void setArguments(const QStringList &arguments)
void setProgram(const QString &program)
void start(QIODeviceBase::OpenMode mode)
bool waitForFinished(int msecs)
bool waitForStarted(int msecs)
bool isEmpty() const const