ZipFileSystem.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. #include <string.h>
  2. #include <map>
  3. #include "../minizip/ioapi_mem.h"
  4. #include "core/ZipFileSystem.h"
  5. #include "core/Object.h"
  6. #include "utils/stringUtils.h"
  7. #include <algorithm>
  8. //#include "utils/utils.h"
  9. #ifdef __S3E__
  10. #include "s3eFile.h"
  11. #endif
  12. namespace oxygine
  13. {
  14. namespace file
  15. {
  16. bool sortFiles(const file_entry& ob1, const file_entry& ob2)
  17. {
  18. return strcmp_cns(ob1.name, ob2.name) < 0;
  19. }
  20. bool readEntry(const file_entry* entry, file::buffer& bf)
  21. {
  22. bf.data.clear();
  23. int r = unzGoToFilePos(entry->zp, const_cast<unz_file_pos*>(&entry->pos));
  24. OX_ASSERT(r == UNZ_OK);
  25. unz_file_info file_info;
  26. r = unzGetCurrentFileInfo(entry->zp, &file_info, 0, 0, 0, 0, 0, 0);
  27. OX_ASSERT(r == UNZ_OK);
  28. unzOpenCurrentFile(entry->zp);
  29. bf.data.resize(file_info.uncompressed_size);
  30. r = unzReadCurrentFile(entry->zp, &bf.data.front(), (int)bf.data.size());
  31. OX_ASSERT(r == (int)file_info.uncompressed_size);
  32. unzCloseCurrentFile(entry->zp);
  33. return true;
  34. }
  35. Zips::Zips(): _sort(false)
  36. {
  37. }
  38. Zips::~Zips()
  39. {
  40. reset();
  41. }
  42. void Zips::read(unzFile zp)
  43. {
  44. do
  45. {
  46. unz_file_pos pos;
  47. unzGetFilePos(zp, &pos);
  48. file_entry entry;
  49. unzGetCurrentFileInfo(zp, 0, entry.name, sizeof(entry.name) - 1, 0, 0, 0, 0);
  50. entry.pos = pos;
  51. entry.zp = zp;
  52. _files.push_back(entry);
  53. }
  54. while (unzGoToNextFile(zp) != UNZ_END_OF_LIST_OF_FILE);
  55. _sort = true;
  56. }
  57. void Zips::add(const unsigned char* data, unsigned int size)
  58. {
  59. zlib_filefunc_def ff;
  60. fill_memory_filefunc(&ff);
  61. zmemdata dta;
  62. dta.data = (char*)data;
  63. dta.size = size;
  64. unzFile zp = unzOpen2((const char*)&dta, &ff);
  65. OX_ASSERT(zp);
  66. if (!zp)
  67. return;
  68. zpitem item;
  69. item.handle = zp;
  70. _zps.push_back(item);
  71. read(zp);
  72. }
  73. void Zips::add(std::vector<char>& data)
  74. {
  75. zlib_filefunc_def ff;
  76. fill_memory_filefunc(&ff);
  77. zmemdata dta;
  78. dta.data = (char*)&data.front();
  79. dta.size = data.size();
  80. unzFile zp = unzOpen2((const char*)&dta, &ff);
  81. OX_ASSERT(zp);
  82. if (!zp)
  83. return;
  84. _zps.push_back(zpitem());
  85. zpitem& item = _zps.back();
  86. item.handle = zp;
  87. std::swap(item.data, data);
  88. read(zp);
  89. }
  90. voidpf ZCALLBACK ox_fopen(voidpf opaque, const char* filename, int mode)
  91. {
  92. return file::open(filename, "rb");
  93. }
  94. uLong ZCALLBACK ox_fread(voidpf opaque, voidpf stream, void* buf, uLong size)
  95. {
  96. return file::read((handle)stream, buf, (unsigned int)size);
  97. }
  98. /*
  99. uLong ZCALLBACK ox_fwriteOF(voidpf opaque, voidpf stream, const void* buf, uLong size)
  100. {
  101. }
  102. */
  103. long ZCALLBACK ox_ftell(voidpf opaque, voidpf stream)
  104. {
  105. return file::tell((handle)stream);
  106. }
  107. long ZCALLBACK ox_fseek(voidpf opaque, voidpf stream, uLong offset, int origin)
  108. {
  109. file::seek((handle)stream, (unsigned int)offset, origin);
  110. return 0;
  111. }
  112. int ZCALLBACK ox_fclose(voidpf opaque, voidpf stream)
  113. {
  114. file::close((handle)stream);
  115. return 0;
  116. }
  117. int ZCALLBACK ox_ferror(voidpf opaque, voidpf stream)
  118. {
  119. return 0;
  120. }
  121. void Zips::add(const char* name)
  122. {
  123. zlib_filefunc_def zpfs;
  124. memset(&zpfs, 0, sizeof(zpfs));
  125. zpfs.zopen_file = ox_fopen;
  126. zpfs.zread_file = ox_fread;
  127. zpfs.zwrite_file = 0;
  128. zpfs.ztell_file = ox_ftell;
  129. zpfs.zseek_file = ox_fseek;
  130. zpfs.zclose_file = ox_fclose;
  131. zpfs.zerror_file = ox_ferror;
  132. unzFile zp = unzOpen2(name, &zpfs);//todo, optimize search in zip
  133. OX_ASSERT(zp);
  134. if (!zp)
  135. return;
  136. zpitem item;
  137. item.handle = zp;
  138. strcpy(item.name, name);
  139. _zps.push_back(item);
  140. read(zp);
  141. }
  142. void Zips::update()
  143. {
  144. std::sort(_files.begin(), _files.end(), sortFiles);
  145. #ifdef OX_DEBUG
  146. for (size_t i = 0; i < _files.size() - 1; ++i)
  147. {
  148. OX_ASSERT(strcmp(_files[i].name, _files[i + 1].name) != 0);
  149. }
  150. #endif
  151. log::messageln("ZipFS, total files: %d", _files.size());
  152. }
  153. bool Zips::read(const char* name, file::buffer& bf)
  154. {
  155. const file_entry* entry = getEntryByName(name);
  156. if (!entry)
  157. return false;
  158. return readEntry(entry, bf);
  159. }
  160. bool Zips::read(const file_entry* entry, file::buffer& bf)
  161. {
  162. return readEntry(entry, bf);
  163. }
  164. void Zips::reset()
  165. {
  166. for (zips::iterator i = _zps.begin(); i != _zps.end(); ++i)
  167. {
  168. zpitem& f = *i;
  169. unzClose(f.handle);
  170. }
  171. _zps.clear();
  172. _files.clear();
  173. }
  174. bool findEntry(const file_entry& g, const char* name)
  175. {
  176. return strcmp_cns(g.name, name) < 0;
  177. }
  178. const file_entry* Zips::getEntryByName(const char* name)
  179. {
  180. if (_sort)
  181. {
  182. update();
  183. _sort = false;
  184. }
  185. files::const_iterator it = std::lower_bound(_files.begin(), _files.end(), name, findEntry);
  186. if (it != _files.end())
  187. {
  188. const file_entry& g = *it;
  189. if (!strcmp_cns(g.name, name))
  190. return &g;
  191. }
  192. return 0;
  193. }
  194. const file_entry* Zips::getEntry(int index)
  195. {
  196. if (_sort)
  197. {
  198. update();
  199. _sort = false;
  200. }
  201. return &_files[index];
  202. }
  203. size_t Zips::getNumEntries() const
  204. {
  205. return _files.size();
  206. }
  207. class fileHandleZip: public fileHandle
  208. {
  209. public:
  210. fileHandleZip(const file_entry* entry): _entry(entry)
  211. {
  212. int r = 0;
  213. r = unzGoToFilePos(entry->zp, const_cast<unz_file_pos*>(&entry->pos));
  214. OX_ASSERT(r == UNZ_OK);
  215. r = unzOpenCurrentFile(entry->zp);
  216. OX_ASSERT(r == UNZ_OK);
  217. }
  218. void release()
  219. {
  220. int r = unzCloseCurrentFile(_entry->zp);
  221. OX_ASSERT(r == UNZ_OK);
  222. delete this;
  223. }
  224. int seek(unsigned int offset, int whence)
  225. {
  226. OX_ASSERT(!"not implemented");
  227. return 0;
  228. }
  229. unsigned int tell() const
  230. {
  231. OX_ASSERT(!"not implemented");
  232. return 0;
  233. }
  234. unsigned int read(void* dest, unsigned int size)
  235. {
  236. return unzReadCurrentFile(_entry->zp, dest, size);
  237. }
  238. unsigned int write(const void* src, unsigned int size)
  239. {
  240. return 0;
  241. }
  242. unsigned int getSize() const
  243. {
  244. unz_file_info file_info;
  245. unzGetCurrentFileInfo(_entry->zp, &file_info, 0, 0, 0, 0, 0, 0);
  246. return (unsigned int) file_info.uncompressed_size;
  247. }
  248. const file_entry* _entry;
  249. };
  250. void ZipFileSystem::add(const char* zip)
  251. {
  252. log::messageln("ZipFileSystem::add %s", zip);
  253. _zips.add(zip);
  254. }
  255. void ZipFileSystem::add(const unsigned char* data, unsigned int size)
  256. {
  257. _zips.add(data, size);
  258. }
  259. void ZipFileSystem::add(std::vector<char>& data)
  260. {
  261. _zips.add(data);
  262. }
  263. void ZipFileSystem::reset()
  264. {
  265. _zips.reset();
  266. }
  267. FileSystem::status ZipFileSystem::_open(const char* file, const char* mode, error_policy ep, file::fileHandle*& fh)
  268. {
  269. const file_entry* entry = _zips.getEntryByName(file);
  270. if (entry)
  271. {
  272. fh = new fileHandleZip(entry);
  273. return status_ok;
  274. }
  275. handleErrorPolicy(ep, "can't find zip file entry: %s", file);
  276. return status_error;
  277. }
  278. FileSystem::status ZipFileSystem::_deleteFile(const char* file)
  279. {
  280. return status_error;
  281. }
  282. FileSystem::status ZipFileSystem::_makeDirectory(const char* file)
  283. {
  284. return status_error;
  285. }
  286. FileSystem::status ZipFileSystem::_deleteDirectory(const char* file)
  287. {
  288. return status_error;
  289. }
  290. FileSystem::status ZipFileSystem::_renameFile(const char* src, const char* dest)
  291. {
  292. return status_error;
  293. }
  294. }
  295. }