21#include "../io/logger.hpp"
23#include <wx/zipstrm.h>
24#include <wx/tarstrm.h>
25#include <wx/zstream.h>
26#include <wx/wfstream.h>
34#include "../../kernel.hpp"
50 std::ifstream file(sArchiveFileName, std::ios_base::binary);
52 static const std::string ZIPHEADER(
"\x50\x4b\x03\x04");
53 static const std::string GZHEADER(
"\x1f\x8b\x08");
54 static const std::string TARMAGIC(
"ustar");
58 if (sArchiveFileName.find(
'.') != std::string::npos)
59 sExt =
toLowerCase(sArchiveFileName.substr(sArchiveFileName.rfind(
'.')));
63 if (!file.good() || file.eof())
65 if (sExt ==
".zip" || sExt ==
".xlsx" || sExt ==
".ods")
67 else if (sExt ==
".tar")
69 else if (sExt ==
".gz")
71 else if (sExt ==
".zz")
77 char magicNumber[6] = {0,0,0,0,0,0};
79 file.read(magicNumber, 4);
80 g_logger.
debug(
"magicNumber = '" + std::string(magicNumber) +
"'");
83 if (magicNumber == ZIPHEADER)
85 else if (GZHEADER == std::string(magicNumber).substr(0, 3))
89 file.seekg(257, std::ios_base::beg);
90 file.read(magicNumber, TARMAGIC.length());
91 g_logger.
debug(
"magicNumber = '" + std::string(magicNumber) +
"'");
93 if (magicNumber == TARMAGIC)
111 std::ifstream gzip(sArchiveFileName, std::ios_base::binary);
113 if (!gzip.good() || gzip.eof())
117 gzip.seekg(3, std::ios_base::beg);
124 gzip.seekg(10, std::ios_base::beg);
132 gzip.seekg(xlen, std::ios_base::cur);
135 std::string fileName;
168 void pack(
const std::vector<std::string>& vFileList,
const std::string& sTargetFile,
Type type)
180 wxFFileOutputStream out(sTargetFile);
181 wxZipOutputStream outzip(out, 6);
183 for (
size_t i = 0; i < vFileList.size(); i++)
189 std::string sDirectory = vFileList[i] +
"/*";
190 std::vector<std::string> vFiles =
getFileList(sDirectory, _option, 1);
192 while (vFiles.size() ||
getFolderList(sDirectory, _option).size() > 2)
194 for (
size_t j = 0; j < vFiles.size(); j++)
198 wxFFileInputStream file(vFiles[j]);
199 wxZipEntry* newEntry =
new wxZipEntry(vFiles[j].substr(vFileList[i].find_last_of(
"/\\")+1));
201 outzip.PutNextEntry(newEntry);
214 wxFFileInputStream file(vFileList[i]);
215 wxZipEntry* newEntry =
new wxZipEntry(vFileList[i].substr(vFileList[i].find_last_of(
"/\\")+1));
217 outzip.PutNextEntry(newEntry);
225 wxFFileOutputStream out(sTargetFile);
226 wxTarOutputStream outtar(out);
228 for (
size_t i = 0; i < vFileList.size(); i++)
234 std::string sDirectory = vFileList[i] +
"/*";
235 std::vector<std::string> vFiles =
getFileList(sDirectory, _option, 1);
237 while (vFiles.size() ||
getFolderList(sDirectory, _option).size() > 2)
239 for (
size_t j = 0; j < vFiles.size(); j++)
243 wxFFileInputStream file(vFiles[j]);
244 wxTarEntry* newEntry =
new wxTarEntry(vFiles[j].substr(vFileList[i].find_last_of(
"/\\")+1));
246 outtar.PutNextEntry(newEntry);
259 wxFFileInputStream file(vFileList[i]);
260 wxTarEntry* newEntry =
new wxTarEntry(vFileList[i].substr(vFileList[i].find_last_of(
"/\\")+1));
262 outtar.PutNextEntry(newEntry);
270 std::string sFile = vFileList.front();
271 bool tempTar =
false;
274 if (vFileList.size() > 1 || !
fileExists(sFile))
276 sFile = sTargetFile.substr(0, sTargetFile.rfind(
'.')) +
".tar";
281 wxFFileOutputStream out(sTargetFile);
282 wxZlibOutputStream outzlib(out, -1, wxZLIB_GZIP);
285 wxFFileInputStream file(sFile);
289 remove(sFile.c_str());
307 std::vector<std::string>
unpack(
const std::string& sArchiveName,
const std::string& sTargetPath)
327 std::vector<std::string> vFiles;
331 wxFFileInputStream in(sArchiveName);
332 wxZipInputStream zip(in);
333 std::unique_ptr<wxZipEntry> entry;
335 while (entry.reset(zip.GetNextEntry()), entry.get() !=
nullptr)
339 if (sTargetPath.length())
344 vFiles.push_back(entryName);
345 wxFileOutputStream stream(sTargetPath +
"/" + entry->GetName());
349 vFiles.push_back(entryName);
354 wxFFileInputStream in(sArchiveName);
355 wxTarInputStream tar(in);
356 std::unique_ptr<wxTarEntry> entry;
358 while (entry.reset(tar.GetNextEntry()), entry.get() !=
nullptr)
365 if (sTargetPath.length())
370 vFiles.push_back(entryName);
371 wxFileOutputStream stream(sTargetPath +
"/" + entry->GetName());
375 vFiles.push_back(entryName);
380 wxFFileInputStream in(sArchiveName);
381 wxZlibInputStream zlib(in);
385 if (sTargetPath.length())
387 if (!sUnpackedName.length())
389 sUnpackedName = sArchiveName.substr(0, sArchiveName.rfind(
'.'))+
".tar";
390 sUnpackedName = _fSys.
ValidizeAndPrepareName(sTargetPath +
"/" + sUnpackedName.substr(sUnpackedName.rfind(
'/')+1),
"");
396 vFiles.push_back(sUnpackedName);
398 wxFileOutputStream stream(sUnpackedName);
402 vFiles.push_back(sUnpackedName);
std::string toLowerCase(const std::string &)
Converts uppercase to lowercase letters.
void debug(const std::string &sMessage)
Convenience member function.
This class implements the basic input/ output file system and provides functionalities to work with f...
std::string ValidizeAndPrepareName(const std::string &_sFileName, const std::string &sExtension=".dat") const
This member function validizes the passed file name and creates the needed folders on-the-fly.
static NumeReKernel * getInstance()
This static member function returns a a pointer to the singleton instance of the kernel.
FileSystem & getFileSystem()
This class manages the setting values of the internal (kernel) settings of this application.
Common exception class for all exceptions thrown in NumeRe.
std::string replacePathSeparator(const std::string &)
This function replaces the Windows style path sparators to UNIX style.
bool fileExists(const string &)
This function checks, whether the file with the passed file name exists.
std::vector< std::string > unpack(const std::string &sArchiveName, const std::string &sTargetPath)
Unpacks an archive file format into its folder structure at the specified location....
static std::string getGZipFileName(const std::string &sArchiveFileName)
Static helper function to read a possible available file name in the GZIP header section.
Type detectType(const std::string &sArchiveFileName)
Detects the type of the archive based upon the magic numbers in their header sections or upon the fil...
void pack(const std::vector< std::string > &vFileList, const std::string &sTargetFile, Type type)
Pack a set of files or folders into an archive file type with the specified file name....