Privacy
An open-source, flexible 3D physical simulation framework
zipit.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2011, 2012, DFKI GmbH Robotics Innovation Center
3  *
4  * This file is part of the MARS simulation framework.
5  *
6  * MARS is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License
8  * as published by the Free Software Foundation, either version 3
9  * of the License, or (at your option) any later version.
10  *
11  * MARS is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with MARS. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 #include "zipit.h"
22 
23 #include <mars/utils/misc.h>
24 #include <mars/interfaces/sim/ControlCenter.h>
25 #include <mars/interfaces/Logging.hpp>
26 
27 #include <cstdlib>
28 #include <cassert>
29 #include <iostream>
30 
31 namespace mars {
32  namespace smurf {
33 
34  using std::vector;
35  using std::string;
36 
37  ZipitError Zipit::zipDirectory(const std::string &directory,
38  const std::string &zipFile,
39  bool overwrite) {
40  return ZIPIT_UNKNOWN_ERROR;
41  this->unZipHandle = NULL;
42  this->zipHandle = NULL;
43  }
44 
45 
46  Zipit::Zipit(const string &zipFileName) {
47  this->zipFileName = zipFileName;
48  this->unZipHandle = NULL;
49  this->zipHandle = NULL;
50  }
54  }
55 
57  zipHandle=zipOpen(zipFileName.c_str(), 0); //handle auf das Zipfile erstellen
58  if (zipHandle==NULL) {
60  return 1;
61  }
62  return 0;
63  }
64 
66  if (zipClose(zipHandle, "")==0) {
67  zipHandle = NULL;
68  return 0;
69  }
70  else {
72  return 1;
73  }
74  }
75 
77  unZipHandle=unzOpen(zipFileName.c_str());
78  if (unZipHandle==NULL) {
80  return 1;
81  }
82  return 0;
83  }
84 
86  if (unzClose(unZipHandle)==0) {
87  unZipHandle = NULL;
88  return 0;
89  }
90  else {
92  return 1;
93  }
94  }
95 
101  int Zipit::addToZip(const vector<string> &listOfFiles,
102  const vector<string> &sourceListOfFiles) {
103  int zipitError;
104 
105  if (openZipHandle()==1) {
106  return 1;
107  }
108 
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);//Z_DEFLATED,Z_DEFAULT_COMPRESSION);
112  if (zipitError!=ZIP_OK) {
113  closeZipHandle();
114  remove(zipFileName.c_str());
116  return 1;
117  }
118  std::ifstream inFile;
119  inFile.open(sourceListOfFiles[i].c_str(),
120  std::ios::out|std::ios::binary);
121  if (!inFile.is_open()) {
122  closeZipHandle();
123  remove(zipFileName.c_str());
125  return 1;
126  }
127  //bestimmen der groese der Datei
128  inFile.seekg(0, std::ios::end);
129  int fileLength = inFile.tellg();
130  inFile.seekg(0, std::ios::beg);
131  //neuen speicher holen
132  char *pBuffer = new char[fileLength];//sollte das speicher holen nicht funktionieren, wird abgebrochen
133  if (pBuffer==NULL) {
135  zipCloseFileInZip(zipHandle);
136  delete[] pBuffer;
137  closeZipHandle();
138  remove(zipFileName.c_str());
139  return 1;
140  }
141  //wenn es funktioniert haben sollte, wird die datei gelesen
142  //hier noch die jeweiligen fehler abfangen
143  inFile.read(pBuffer, fileLength); //die gesammte datei wird in den speicher gelesen.
144  zipitError=zipWriteInFileInZip(zipHandle, pBuffer, fileLength);//der inhalt des Speichers wird in die Datei im Zip geschrieben
145  if (zipitError!=ZIP_OK) {
147  zipCloseFileInZip(zipHandle);
148  delete[] pBuffer;
149  inFile.close();
150  closeZipHandle();
151  remove(zipFileName.c_str());
152  return 1;
153  }
154  zipCloseFileInZip(zipHandle);
155  delete[] pBuffer;
156  inFile.close();
157  }
158  closeZipHandle();
159  return 0;
160  }
161 
167  int Zipit::addToZip(const string &fileNameToZip,
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);
174  }
175 
176 
177  int Zipit::getFromZip(const vector<string> &allFileNamesToRead,
178  const vector<string> &v_whereToStore) {
179  int errorNumberZip;
180  bool closeAfterwards = false;
181  if(unZipHandle == NULL) {
182  closeAfterwards = true;
183  if (openUnZipHandle()==1) {
184  LOG_ERROR("unable to open filehandle");
185  return ZIPIT_NO_OPEN_HANDLE;
186  }
187  }
188 
189  for (unsigned int i=0; i<allFileNamesToRead.size(); i++) {
190  if (unzLocateFile(unZipHandle, allFileNamesToRead[i].c_str(), 2)!=UNZ_OK) {
194  } else {
195  if (unzOpenCurrentFile(unZipHandle)!=UNZ_OK) {
199  }
200  FILE * pOutFile;
201  int size_buffer = 1024;
202  char* fileBuffer[size_buffer]; //a buffer for buffering
203  pOutFile = fopen(v_whereToStore[i].c_str(), "wb");
204  if (pOutFile!=NULL) { //everythings fine, proceding
205  do {
206  errorNumberZip = unzReadCurrentFile(unZipHandle, fileBuffer,
207  size_buffer);
208  if (errorNumberZip<0) {
210  fclose(pOutFile);
213  }
214  if (errorNumberZip>0)
215  if (fwrite(fileBuffer, 1, errorNumberZip, pOutFile)==0) {
216  errorNumberZip=UNZ_ERRNO;
217  fclose(pOutFile);
221  }
222  } while (errorNumberZip>0);
223  if (pOutFile) {
224  fclose(pOutFile);
225  }
226  } else {
227  fclose(pOutFile);
230  return ZIPIT_NO_BUFFER;
231  }
232  if (errorNumberZip==UNZ_OK) {
233  //closing actual opened file in the archive
234  errorNumberZip=unzCloseCurrentFile(unZipHandle);
235  if (errorNumberZip!=UNZ_OK) {
239  }
240  }
241  }
242  }
243  if(closeAfterwards) closeUnZipHandle();
244  return ZIPIT_SUCCESS;
245  }
246 
247  int Zipit::getFromZip(const string &inZipFileName,
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);
254  }
255 
256  void Zipit::zipError(ZipitError errorNumber) {
257  if(errorNumber == ZIPIT_SUCCESS)
258  return;
259  LOG_ERROR("ZIPIT: an error occured");
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.");
264  break;
266  LOG_ERROR("unable to close a handle to the zip. maybe the zipfile does not "
267  "exsist or it has been closed befor");
268  break;
269  case ZIPIT_NO_BUFFER:
270  LOG_ERROR("unable to allocate memory needed for processing the file");
271  break;
273  LOG_ERROR("unable to open the file. maybe the filename is illegal");
274  break;
276  LOG_ERROR("unable to find the spezified file in the zipfile. maybe the file "
277  "does not exsist or the filename is misspelled");
278  break;
280  LOG_ERROR("unable to create the file in the zip. maybe there is now more "
281  "space left on the device");
282  break;
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");
286  break;
288  LOG_ERROR("unable to open file in zip");
289  break;
291  LOG_ERROR("unable to read file in zip");
292  break;
294  LOG_ERROR("no handle for file");
295  break;
297  LOG_ERROR("unable to close zipfile");
298  break;
300  LOG_ERROR("unable to write in file");
301  break;
303  LOG_ERROR("unable to write file in zip");
304  break;
306  LOG_ERROR("unable to close file in zip");
307  break;
308  case ZIPIT_UNKNOWN_ERROR:
309  LOG_ERROR("unknown error \"%d\" occurred", errorNumber);
310  break;
311  case ZIPIT_SUCCESS:
312  // We should never get here because we should bail out
313  // early on success. This is here to silence compiler
314  // warnings about unhandled switch-cases.
315  assert(!"We should never get here");
316  }
317  }
318 
319  int Zipit::unpackWholeZipTo(const string &whereToStore) {
320  string filename="";
321  int err = 0;
322 
323  unsigned long currentFilenameSize=1024;
324  if (whereToStore=="") {
325  return 0;
326  }
327  if (openUnZipHandle()==1) {
328  std::cout <<"unable to open filehandle" <<std::endl;
329  return 1;
330  }
331  err=unzGoToFirstFile(unZipHandle);
332  while (err==UNZ_OK) {
333  //std::cout<<"in der schleife"<<std::endl;
334  char currentFilename[currentFilenameSize+1];
335  err=unzGetCurrentFileInfo(unZipHandle, NULL, currentFilename,
336  size_t(currentFilenameSize)-1, NULL, 0, NULL, 0);
337  if (err==UNZ_OK) {
338  filename.assign(currentFilename);
339  filename.insert(0, whereToStore);
340  getFromZip(currentFilename, filename);
341  err=unzGoToNextFile(unZipHandle);
342  }
343  }
345  return 1;
346  }
347 
348  ZipitError Zipit::unzipAll(const std::string &zipFilename,
349  const std::string &directory) {
350  if(!mars::utils::pathExists(zipFilename)) {
351  return ZIPIT_FILE_NOT_FOUND;
352  }
353  unzFile zFile = unzOpen(zipFilename.c_str());
354  if(!zFile) {
355  return ZIPIT_NO_OPEN_HANDLE;
356  }
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) {
363  // get info about the current file
364  unz_file_info info;
365  unzGetCurrentFileInfo(zFile, &info, NULL, 0, NULL, 0, NULL, 0);
366  // make sure we have enough space for the filename
367  if(info.size_filename > filenameBufferSize) {
368  filenameBufferSize = info.size_filename;
369  filenameBuffer = static_cast<char*>(realloc(filenameBuffer,
370  filenameBufferSize));
371  }
372  // get the filename
373  unzGetCurrentFileInfo(zFile, NULL, filenameBuffer, filenameBufferSize,
374  NULL, 0, NULL, 0);
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);
380  // do nothing and continue reading
381  }
382  unzCloseCurrentFile(zFile);
383  fclose(fd);
384  unzGoToNextFile(zFile);
385  }
386  free(filenameBuffer);
387  free(buffer);
388  unzClose(zFile);
389  return ZIPIT_SUCCESS;
390  }
391 
392  } // end of namespace smurf
393 } // end of namespace mars
unzFile unZipHandle
Definition: zipit.h:136
int closeZipHandle()
Definition: zipit.cpp:65
int closeUnZipHandle()
Definition: zipit.cpp:85
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)
Definition: zipit.cpp:348
int openZipHandle()
int openZipHandle(); int closeZipHandle(); int openUnZipHandle(); int closeUnZipHandle(); ...
Definition: zipit.cpp:56
Zipit(const std::string &zipFileName)
int zipit(string zipFileName); the constructor just initialises some internal values, i.e.
std::string zipFileName
Definition: zipit.h:137
ZipitError
Definition: zipit.h:47
Copyright 2012, DFKI GmbH Robotics Innovation Center.
int openUnZipHandle()
Definition: zipit.cpp:76
ZipitError zipDirectory(const std::string &directory, const std::string &zipFile, bool overwrite)
Definition: zipit.cpp:37
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.
Definition: zipit.h:135
bool pathExists(const std::string &path)
check whether a file or directory exists.
Definition: misc.h:112
void zipError(ZipitError errorNumber)
until now, this function does nothing, but in future it should be just to generate uniformed errorMes...
Definition: zipit.cpp:256
#define LOG_ERROR(...)
Definition: Logging.hpp:21
int unpackWholeZipTo(const std::string &whereToStore)