23 #include <mars/utils/misc.h> 24 #include <mars/interfaces/sim/ControlCenter.h> 25 #include <mars/interfaces/Logging.hpp> 38 const std::string &zipFile,
57 zipHandle=zipOpen(zipFileName.c_str(), 0);
102 const vector<string> &sourceListOfFiles) {
109 for (
unsigned int i=0; i<listOfFiles.size(); i++) {
110 zipitError=zipOpenNewFileInZip(
zipHandle, listOfFiles[i].c_str(), NULL,
111 NULL, 0, NULL, 0, NULL, Z_DEFLATED, 0);
112 if (zipitError!=ZIP_OK) {
114 remove(zipFileName.c_str());
118 std::ifstream inFile;
119 inFile.open(sourceListOfFiles[i].c_str(),
120 std::ios::out|std::ios::binary);
121 if (!inFile.is_open()) {
123 remove(zipFileName.c_str());
128 inFile.seekg(0, std::ios::end);
129 int fileLength = inFile.tellg();
130 inFile.seekg(0, std::ios::beg);
132 char *pBuffer =
new char[fileLength];
138 remove(zipFileName.c_str());
143 inFile.read(pBuffer, fileLength);
144 zipitError=zipWriteInFileInZip(
zipHandle, pBuffer, fileLength);
145 if (zipitError!=ZIP_OK) {
151 remove(zipFileName.c_str());
168 const string &sourceFileNameToZip) {
169 vector<string> listOfFiles;
170 vector<string> sourceListOfFiles;
171 listOfFiles.push_back(fileNameToZip);
172 sourceListOfFiles.push_back(sourceFileNameToZip);
173 return addToZip(listOfFiles, sourceListOfFiles);
178 const vector<string> &v_whereToStore) {
180 bool closeAfterwards =
false;
182 closeAfterwards =
true;
189 for (
unsigned int i=0; i<allFileNamesToRead.size(); i++) {
190 if (unzLocateFile(
unZipHandle, allFileNamesToRead[i].c_str(), 2)!=UNZ_OK) {
201 int size_buffer = 1024;
202 char* fileBuffer[size_buffer];
203 pOutFile = fopen(v_whereToStore[i].c_str(),
"wb");
204 if (pOutFile!=NULL) {
206 errorNumberZip = unzReadCurrentFile(
unZipHandle, fileBuffer,
208 if (errorNumberZip<0) {
214 if (errorNumberZip>0)
215 if (fwrite(fileBuffer, 1, errorNumberZip, pOutFile)==0) {
216 errorNumberZip=UNZ_ERRNO;
222 }
while (errorNumberZip>0);
232 if (errorNumberZip==UNZ_OK) {
235 if (errorNumberZip!=UNZ_OK) {
248 const string &whereToStore) {
249 vector<string> allFileNamesToRead;
250 vector<string> v_whereToStore;
251 allFileNamesToRead.push_back(inZipFileName);
252 v_whereToStore.push_back(whereToStore);
253 return getFromZip(allFileNamesToRead, v_whereToStore);
260 switch (errorNumber) {
262 LOG_ERROR(
"unable to open a handle to the zip. maybe the zipfile does not " 263 "exsist or there is no further free memory.");
266 LOG_ERROR(
"unable to close a handle to the zip. maybe the zipfile does not " 267 "exsist or it has been closed befor");
270 LOG_ERROR(
"unable to allocate memory needed for processing the file");
273 LOG_ERROR(
"unable to open the file. maybe the filename is illegal");
276 LOG_ERROR(
"unable to find the spezified file in the zipfile. maybe the file " 277 "does not exsist or the filename is misspelled");
280 LOG_ERROR(
"unable to create the file in the zip. maybe there is now more " 281 "space left on the device");
284 LOG_ERROR(
"there has no date been written to the zipfile. reasons for " 285 "errors are many. but most possible is, something went wrong");
303 LOG_ERROR(
"unable to write file in zip");
306 LOG_ERROR(
"unable to close file in zip");
309 LOG_ERROR(
"unknown error \"%d\" occurred", errorNumber);
315 assert(!
"We should never get here");
323 unsigned long currentFilenameSize=1024;
324 if (whereToStore==
"") {
328 std::cout <<
"unable to open filehandle" <<std::endl;
332 while (err==UNZ_OK) {
334 char currentFilename[currentFilenameSize+1];
335 err=unzGetCurrentFileInfo(
unZipHandle, NULL, currentFilename,
336 size_t(currentFilenameSize)-1, NULL, 0, NULL, 0);
338 filename.assign(currentFilename);
339 filename.insert(0, whereToStore);
349 const std::string &directory) {
353 unzFile zFile = unzOpen(zipFilename.c_str());
357 size_t filenameBufferSize = 1024;
358 char *filenameBuffer =
static_cast<char*
>(malloc(filenameBufferSize));
359 size_t bufferSize = 1024;
360 char *buffer =
static_cast<char*
>(malloc(bufferSize));
361 int err = unzGoToFirstFile(zFile);
362 while(err == UNZ_OK) {
365 unzGetCurrentFileInfo(zFile, &info, NULL, 0, NULL, 0, NULL, 0);
367 if(info.size_filename > filenameBufferSize) {
368 filenameBufferSize = info.size_filename;
369 filenameBuffer =
static_cast<char*
>(realloc(filenameBuffer,
370 filenameBufferSize));
373 unzGetCurrentFileInfo(zFile, NULL, filenameBuffer, filenameBufferSize,
375 FILE *
fd = fopen(filenameBuffer,
"wb");
376 unzOpenCurrentFile(zFile);
377 size_t bytesRead = 0;
378 while(0 < (bytesRead = unzReadCurrentFile(zFile, buffer, bufferSize))) {
379 fwrite(buffer,
sizeof(
char), bytesRead, fd);
382 unzCloseCurrentFile(zFile);
384 unzGoToNextFile(zFile);
386 free(filenameBuffer);
int addToZip(const std::string &fileNameToZip, const std::string &sourceFileNameToZip)
int addToZip(string fileNameToZip, bool zipWithFolders); and int addToZip(string fileNameToZip, bool zipWithFolders, vector<string> listOfFiles); both adding file(s) to a zipfile.
ZipitError unzipAll(const std::string &zipFilename, const std::string &directory)
int openZipHandle()
int openZipHandle(); int closeZipHandle(); int openUnZipHandle(); int closeUnZipHandle(); ...
Zipit(const std::string &zipFileName)
int zipit(string zipFileName); the constructor just initialises some internal values, i.e.
Copyright 2012, DFKI GmbH Robotics Innovation Center.
ZipitError zipDirectory(const std::string &directory, const std::string &zipFile, bool overwrite)
int getFromZip(const std::string &inZipFileName, const std::string &whereToStore)
an important fearture to add: an ability to spezify an folder, where the files should be stored ...
zipFile zipHandle
just a few internal variables.
bool pathExists(const std::string &path)
check whether a file or directory exists.
void zipError(ZipitError errorNumber)
until now, this function does nothing, but in future it should be just to generate uniformed errorMes...
int unpackWholeZipTo(const std::string &whereToStore)