Salome HOME
Updated copyright comment
[modules/shaper.git] / src / XGUI / XGUI_CompressFiles.cpp
1 // Copyright (C) 2014-2024  CEA, EDF
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "XGUI_CompressFiles.h"
21
22 #include <QFile>
23 #include <QFileInfo>
24 #include <QDir>
25 #include <QDataStream>
26
27
28
29
30 bool XGUI_CompressFiles::compress(const QString& theFile, std::list<std::string> theFiles)
31 {
32   QFile aOutFile(theFile);
33   if (aOutFile.open(QIODevice::WriteOnly)) {
34     QDataStream aOutStream(&aOutFile);
35
36     std::list<std::string>::const_iterator aIt;
37     for (aIt = theFiles.cbegin(); aIt != theFiles.cend(); ++aIt) {
38       QString aPathName((*aIt).c_str());
39       QFile aSrcFile(aPathName);
40       QFileInfo aInfo(aSrcFile);
41       quint64 aSize = aInfo.size();
42       if (aSrcFile.open(QIODevice::ReadOnly)) {
43         QString aName =
44           aPathName.right(aPathName.length() - aPathName.lastIndexOf(QDir::separator()) - 1);
45
46         aOutStream << aName << aSize << qCompress(aSrcFile.readAll());
47         aSrcFile.close();
48       }
49       else {
50         aOutFile.close();
51         return false;
52       }
53     }
54     aOutFile.close();
55   }
56   else
57     return false;
58
59   return true;
60 }
61
62
63 bool XGUI_CompressFiles::uncompress(const QString& theFile, const QString& theDir)
64 {
65   QFile aInFile(theFile);
66   QDir aDir(theDir);
67   if (!aDir.exists())
68     aDir.mkpath(theDir);
69   if (!aDir.isEmpty()) {
70     if (!aDir.removeRecursively())
71       return false;
72     aDir.mkdir(theDir);
73   }
74
75   if (aInFile.open(QIODevice::ReadOnly)) {
76     QDataStream aInStream(&aInFile);
77     do {
78       QString aFileName;
79       quint64 aSize;
80       aInStream >> aFileName;
81       aInStream >> aSize;
82
83       QFile aOutFile(theDir + QDir::separator() + aFileName);
84       if (aOutFile.open(QIODevice::WriteOnly)) {
85         QByteArray aContent;
86         aContent.resize(aSize);
87         aInStream >> aContent;
88         aOutFile.write(qUncompress(aContent));
89         aOutFile.close();
90       }
91     } while (!aInStream.atEnd());
92     aInFile.close();
93   }
94   return true;
95 }