| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | #include "ZipArchive.h" |
|---|
| 15 | |
|---|
| 16 | #include <osgDB/FileUtils> |
|---|
| 17 | #include <osgDB/FileNameUtils> |
|---|
| 18 | #include <osgDB/ReadFile> |
|---|
| 19 | #include <osgDB/Registry> |
|---|
| 20 | |
|---|
| 21 | #include <sys/types.h> |
|---|
| 22 | #include <sys/stat.h> |
|---|
| 23 | |
|---|
| 24 | #include <sstream> |
|---|
| 25 | #include <cstdio> |
|---|
| 26 | #include "unzip.h" |
|---|
| 27 | |
|---|
| 28 | #if !defined(S_ISDIR) |
|---|
| 29 | # if defined( _S_IFDIR) && !defined( __S_IFDIR) |
|---|
| 30 | # define __S_IFDIR _S_IFDIR |
|---|
| 31 | # endif |
|---|
| 32 | # define S_ISDIR(mode) (mode&__S_IFDIR) |
|---|
| 33 | #endif |
|---|
| 34 | |
|---|
| 35 | #ifndef S_ISREG |
|---|
| 36 | #define S_ISREG(x) (((x) & S_IFMT) == S_IFREG) |
|---|
| 37 | #endif |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | ZipArchive::ZipArchive() |
|---|
| 41 | : mZipLoaded(false) |
|---|
| 42 | , mZipRecord(NULL) |
|---|
| 43 | { |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | ZipArchive::~ZipArchive() |
|---|
| 47 | { |
|---|
| 48 | |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | void ZipArchive::close() |
|---|
| 53 | { |
|---|
| 54 | if(mZipLoaded) |
|---|
| 55 | { |
|---|
| 56 | CloseZip(mZipRecord); |
|---|
| 57 | |
|---|
| 58 | mZipRecord = NULL; |
|---|
| 59 | mZipIndex.clear(); |
|---|
| 60 | } |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | bool ZipArchive::fileExists(const std::string& filename) const |
|---|
| 65 | { |
|---|
| 66 | return GetZipEntry(filename) != NULL; |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | std::string ZipArchive::getMasterFileName() const |
|---|
| 71 | { |
|---|
| 72 | return std::string(); |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | std::string ZipArchive::getArchiveFileName() const |
|---|
| 76 | { |
|---|
| 77 | std::string result; |
|---|
| 78 | if(mZipLoaded) |
|---|
| 79 | { |
|---|
| 80 | result = mMainRecord.name; |
|---|
| 81 | } |
|---|
| 82 | return result; |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | bool ZipArchive::getFileNames(osgDB::Archive::FileNameList& fileNameList) const |
|---|
| 87 | { |
|---|
| 88 | if(mZipLoaded) |
|---|
| 89 | { |
|---|
| 90 | ZipEntryMap::const_iterator iter = mZipIndex.begin(); |
|---|
| 91 | |
|---|
| 92 | for(;iter != mZipIndex.end(); ++iter) |
|---|
| 93 | { |
|---|
| 94 | fileNameList.push_back((*iter).first); |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | return true; |
|---|
| 98 | } |
|---|
| 99 | else |
|---|
| 100 | { |
|---|
| 101 | return false; |
|---|
| 102 | } |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | bool ZipArchive::open(const std::string& file, ArchiveStatus status, const osgDB::ReaderWriter::Options* options) |
|---|
| 108 | { |
|---|
| 109 | std::string ext = osgDB::getLowerCaseFileExtension(file); |
|---|
| 110 | if (!acceptsExtension(ext)) return osgDB::ReaderWriter::ReadResult::FILE_NOT_HANDLED; |
|---|
| 111 | |
|---|
| 112 | std::string fileName = osgDB::findDataFile( file, options ); |
|---|
| 113 | if (fileName.empty()) return osgDB::ReaderWriter::ReadResult::FILE_NOT_FOUND; |
|---|
| 114 | |
|---|
| 115 | std::string password = ReadPassword(options); |
|---|
| 116 | |
|---|
| 117 | HZIP hz = OpenZip(fileName.c_str(), password.c_str()); |
|---|
| 118 | |
|---|
| 119 | if(hz != NULL) |
|---|
| 120 | { |
|---|
| 121 | IndexZipFiles(hz); |
|---|
| 122 | return true; |
|---|
| 123 | } |
|---|
| 124 | else |
|---|
| 125 | { |
|---|
| 126 | return false; |
|---|
| 127 | } |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | bool ZipArchive::open(std::istream& fin, const osgDB::ReaderWriter::Options* options) |
|---|
| 131 | { |
|---|
| 132 | osgDB::ReaderWriter::ReadResult result = osgDB::ReaderWriter::ReadResult(osgDB::ReaderWriter::ReadResult::FILE_NOT_HANDLED); |
|---|
| 133 | |
|---|
| 134 | if (fin.fail()) return false; |
|---|
| 135 | |
|---|
| 136 | fin.seekg(0,std::ios_base::end); |
|---|
| 137 | unsigned int ulzipFileLength = fin.tellg(); |
|---|
| 138 | fin.seekg(0,std::ios_base::beg); |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | char * pMemBuffer = new char [ulzipFileLength]; |
|---|
| 145 | if (!pMemBuffer) return false; |
|---|
| 146 | |
|---|
| 147 | std::string password = ReadPassword(options); |
|---|
| 148 | |
|---|
| 149 | HZIP hz = NULL; |
|---|
| 150 | fin.read(pMemBuffer, ulzipFileLength); |
|---|
| 151 | |
|---|
| 152 | if ((unsigned int)fin.gcount() == ulzipFileLength) |
|---|
| 153 | { |
|---|
| 154 | hz = OpenZip(pMemBuffer, ulzipFileLength, password.c_str()); |
|---|
| 155 | } |
|---|
| 156 | delete [] pMemBuffer; |
|---|
| 157 | |
|---|
| 158 | if(hz != NULL) |
|---|
| 159 | { |
|---|
| 160 | IndexZipFiles(hz); |
|---|
| 161 | return true; |
|---|
| 162 | } |
|---|
| 163 | else |
|---|
| 164 | { |
|---|
| 165 | return false; |
|---|
| 166 | } |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | osgDB::ReaderWriter::ReadResult ZipArchive::readObject(const std::string& file, const osgDB::ReaderWriter::Options* options) const |
|---|
| 170 | { |
|---|
| 171 | osgDB::ReaderWriter::ReadResult rresult = osgDB::ReaderWriter::ReadResult::FILE_NOT_HANDLED; |
|---|
| 172 | |
|---|
| 173 | std::string ext = osgDB::getLowerCaseFileExtension(file); |
|---|
| 174 | if (!mZipLoaded || !acceptsExtension(ext)) return osgDB::ReaderWriter::ReadResult::FILE_NOT_HANDLED; |
|---|
| 175 | |
|---|
| 176 | const ZIPENTRY* ze = GetZipEntry(file); |
|---|
| 177 | if(ze != NULL) |
|---|
| 178 | { |
|---|
| 179 | std::stringstream buffer; |
|---|
| 180 | |
|---|
| 181 | osgDB::ReaderWriter* rw = ReadFromZipEntry(ze, options, buffer); |
|---|
| 182 | if (rw != NULL) |
|---|
| 183 | { |
|---|
| 184 | |
|---|
| 185 | osg::ref_ptr<osgDB::ReaderWriter::Options> local_opt = options ? |
|---|
| 186 | static_cast<osgDB::ReaderWriter::Options*>(options->clone(osg::CopyOp::SHALLOW_COPY)) : |
|---|
| 187 | new osgDB::ReaderWriter::Options; |
|---|
| 188 | |
|---|
| 189 | local_opt->setPluginStringData("STREAM_FILENAME", osgDB::getSimpleFileName(ze->name)); |
|---|
| 190 | |
|---|
| 191 | osgDB::ReaderWriter::ReadResult readResult = rw->readObject(buffer,local_opt.get()); |
|---|
| 192 | if (readResult.success()) |
|---|
| 193 | { |
|---|
| 194 | return readResult; |
|---|
| 195 | } |
|---|
| 196 | } |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | return rresult; |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | osgDB::ReaderWriter::ReadResult ZipArchive::readImage(const std::string& file,const osgDB::ReaderWriter::Options* options) const |
|---|
| 203 | { |
|---|
| 204 | osgDB::ReaderWriter::ReadResult rresult = osgDB::ReaderWriter::ReadResult::FILE_NOT_HANDLED; |
|---|
| 205 | |
|---|
| 206 | std::string ext = osgDB::getLowerCaseFileExtension(file); |
|---|
| 207 | if (!mZipLoaded || !acceptsExtension(ext)) return osgDB::ReaderWriter::ReadResult::FILE_NOT_HANDLED; |
|---|
| 208 | |
|---|
| 209 | const ZIPENTRY* ze = GetZipEntry(file); |
|---|
| 210 | if(ze != NULL) |
|---|
| 211 | { |
|---|
| 212 | std::stringstream buffer; |
|---|
| 213 | |
|---|
| 214 | osgDB::ReaderWriter* rw = ReadFromZipEntry(ze, options, buffer); |
|---|
| 215 | if (rw != NULL) |
|---|
| 216 | { |
|---|
| 217 | |
|---|
| 218 | osg::ref_ptr<osgDB::ReaderWriter::Options> local_opt = options ? |
|---|
| 219 | static_cast<osgDB::ReaderWriter::Options*>(options->clone(osg::CopyOp::SHALLOW_COPY)) : |
|---|
| 220 | new osgDB::ReaderWriter::Options; |
|---|
| 221 | |
|---|
| 222 | local_opt->setPluginStringData("STREAM_FILENAME", osgDB::getSimpleFileName(ze->name)); |
|---|
| 223 | |
|---|
| 224 | osgDB::ReaderWriter::ReadResult readResult = rw->readImage(buffer,local_opt.get()); |
|---|
| 225 | if (readResult.success()) |
|---|
| 226 | { |
|---|
| 227 | return readResult; |
|---|
| 228 | } |
|---|
| 229 | } |
|---|
| 230 | } |
|---|
| 231 | |
|---|
| 232 | return rresult; |
|---|
| 233 | } |
|---|
| 234 | |
|---|
| 235 | osgDB::ReaderWriter::ReadResult ZipArchive::readHeightField(const std::string& file,const osgDB::ReaderWriter::Options* options) const |
|---|
| 236 | { |
|---|
| 237 | osgDB::ReaderWriter::ReadResult rresult = osgDB::ReaderWriter::ReadResult::FILE_NOT_HANDLED; |
|---|
| 238 | |
|---|
| 239 | std::string ext = osgDB::getLowerCaseFileExtension(file); |
|---|
| 240 | if (!mZipLoaded || !acceptsExtension(ext)) return osgDB::ReaderWriter::ReadResult::FILE_NOT_HANDLED; |
|---|
| 241 | |
|---|
| 242 | const ZIPENTRY* ze = GetZipEntry(file); |
|---|
| 243 | if(ze != NULL) |
|---|
| 244 | { |
|---|
| 245 | std::stringstream buffer; |
|---|
| 246 | |
|---|
| 247 | osgDB::ReaderWriter* rw = ReadFromZipEntry(ze, options, buffer); |
|---|
| 248 | if (rw != NULL) |
|---|
| 249 | { |
|---|
| 250 | |
|---|
| 251 | osg::ref_ptr<osgDB::ReaderWriter::Options> local_opt = options ? |
|---|
| 252 | options->cloneOptions() : |
|---|
| 253 | new osgDB::ReaderWriter::Options; |
|---|
| 254 | |
|---|
| 255 | local_opt->setPluginStringData("STREAM_FILENAME", osgDB::getSimpleFileName(ze->name)); |
|---|
| 256 | |
|---|
| 257 | osgDB::ReaderWriter::ReadResult readResult = rw->readObject(buffer,local_opt.get()); |
|---|
| 258 | if (readResult.success()) |
|---|
| 259 | { |
|---|
| 260 | return readResult; |
|---|
| 261 | } |
|---|
| 262 | } |
|---|
| 263 | } |
|---|
| 264 | |
|---|
| 265 | return rresult; |
|---|
| 266 | } |
|---|
| 267 | |
|---|
| 268 | osgDB::ReaderWriter::ReadResult ZipArchive::readNode(const std::string& file,const osgDB::ReaderWriter::Options* options) const |
|---|
| 269 | { |
|---|
| 270 | osgDB::ReaderWriter::ReadResult rresult = osgDB::ReaderWriter::ReadResult::FILE_NOT_HANDLED; |
|---|
| 271 | |
|---|
| 272 | std::string ext = osgDB::getLowerCaseFileExtension(file); |
|---|
| 273 | if (!mZipLoaded || !acceptsExtension(ext)) return osgDB::ReaderWriter::ReadResult::FILE_NOT_HANDLED; |
|---|
| 274 | |
|---|
| 275 | const ZIPENTRY* ze = GetZipEntry(file); |
|---|
| 276 | if(ze != NULL) |
|---|
| 277 | { |
|---|
| 278 | std::stringstream buffer; |
|---|
| 279 | |
|---|
| 280 | osgDB::ReaderWriter* rw = ReadFromZipEntry(ze, options, buffer); |
|---|
| 281 | if (rw != NULL) |
|---|
| 282 | { |
|---|
| 283 | |
|---|
| 284 | osg::ref_ptr<osgDB::ReaderWriter::Options> local_opt = options ? |
|---|
| 285 | options->cloneOptions() : |
|---|
| 286 | new osgDB::ReaderWriter::Options; |
|---|
| 287 | |
|---|
| 288 | local_opt->setPluginStringData("STREAM_FILENAME", osgDB::getSimpleFileName(ze->name)); |
|---|
| 289 | |
|---|
| 290 | osgDB::ReaderWriter::ReadResult readResult = rw->readNode(buffer,local_opt.get()); |
|---|
| 291 | if (readResult.success()) |
|---|
| 292 | { |
|---|
| 293 | return readResult; |
|---|
| 294 | } |
|---|
| 295 | } |
|---|
| 296 | } |
|---|
| 297 | |
|---|
| 298 | return rresult; |
|---|
| 299 | } |
|---|
| 300 | |
|---|
| 301 | osgDB::ReaderWriter::WriteResult ZipArchive::writeObject(const osg::Object& ,const std::string& ,const osgDB::ReaderWriter::Options*) const |
|---|
| 302 | { |
|---|
| 303 | return osgDB::ReaderWriter::WriteResult(osgDB::ReaderWriter::WriteResult::FILE_NOT_HANDLED); |
|---|
| 304 | } |
|---|
| 305 | |
|---|
| 306 | osgDB::ReaderWriter::WriteResult ZipArchive::writeImage(const osg::Image& ,const std::string& ,const osgDB::ReaderWriter::Options*) const |
|---|
| 307 | { |
|---|
| 308 | return osgDB::ReaderWriter::WriteResult(osgDB::ReaderWriter::WriteResult::FILE_NOT_HANDLED); |
|---|
| 309 | } |
|---|
| 310 | |
|---|
| 311 | osgDB::ReaderWriter::WriteResult ZipArchive::writeHeightField(const osg::HeightField& ,const std::string& ,const osgDB::ReaderWriter::Options*) const |
|---|
| 312 | { |
|---|
| 313 | return osgDB::ReaderWriter::WriteResult(osgDB::ReaderWriter::WriteResult::FILE_NOT_HANDLED); |
|---|
| 314 | } |
|---|
| 315 | |
|---|
| 316 | osgDB::ReaderWriter::WriteResult ZipArchive::writeNode(const osg::Node& ,const std::string& ,const osgDB::ReaderWriter::Options*) const |
|---|
| 317 | { |
|---|
| 318 | return osgDB::ReaderWriter::WriteResult(osgDB::ReaderWriter::WriteResult::FILE_NOT_HANDLED); |
|---|
| 319 | } |
|---|
| 320 | |
|---|
| 321 | |
|---|
| 322 | osgDB::ReaderWriter* ZipArchive::ReadFromZipEntry(const ZIPENTRY* ze, const osgDB::ReaderWriter::Options* options, std::stringstream& buffer) const |
|---|
| 323 | { |
|---|
| 324 | if (ze != 0) |
|---|
| 325 | { |
|---|
| 326 | char* ibuf = new (std::nothrow) char[ze->unc_size]; |
|---|
| 327 | if (ibuf) |
|---|
| 328 | { |
|---|
| 329 | ZRESULT result = UnzipItem(mZipRecord, ze->index, ibuf, ze->unc_size); |
|---|
| 330 | bool unzipSuccesful = CheckZipErrorCode(result); |
|---|
| 331 | if(unzipSuccesful) |
|---|
| 332 | { |
|---|
| 333 | buffer.write(ibuf,ze->unc_size); |
|---|
| 334 | } |
|---|
| 335 | |
|---|
| 336 | delete[] ibuf; |
|---|
| 337 | |
|---|
| 338 | std::string file_ext = osgDB::getFileExtension(ze->name); |
|---|
| 339 | |
|---|
| 340 | osgDB::ReaderWriter* rw = osgDB::Registry::instance()->getReaderWriterForExtension(file_ext); |
|---|
| 341 | if (rw != NULL) |
|---|
| 342 | { |
|---|
| 343 | return rw; |
|---|
| 344 | } |
|---|
| 345 | } |
|---|
| 346 | else |
|---|
| 347 | { |
|---|
| 348 | |
|---|
| 349 | } |
|---|
| 350 | } |
|---|
| 351 | |
|---|
| 352 | return NULL; |
|---|
| 353 | } |
|---|
| 354 | |
|---|
| 355 | void CleanupFileString(std::string& strFileOrDir) |
|---|
| 356 | { |
|---|
| 357 | if (strFileOrDir.empty()) |
|---|
| 358 | { |
|---|
| 359 | return; |
|---|
| 360 | } |
|---|
| 361 | |
|---|
| 362 | |
|---|
| 363 | for (unsigned int i = 0; i < strFileOrDir.length(); ++i) |
|---|
| 364 | { |
|---|
| 365 | if (strFileOrDir[i] == '\\') |
|---|
| 366 | { |
|---|
| 367 | strFileOrDir[i] = '/'; |
|---|
| 368 | } |
|---|
| 369 | } |
|---|
| 370 | |
|---|
| 371 | |
|---|
| 372 | if (strFileOrDir[strFileOrDir.length()-1] == '/') |
|---|
| 373 | { |
|---|
| 374 | strFileOrDir = strFileOrDir.substr(0, strFileOrDir.length()-1); |
|---|
| 375 | } |
|---|
| 376 | |
|---|
| 377 | |
|---|
| 378 | if(strFileOrDir[0] != '/') |
|---|
| 379 | { |
|---|
| 380 | strFileOrDir.insert(0, "/"); |
|---|
| 381 | } |
|---|
| 382 | } |
|---|
| 383 | |
|---|
| 384 | void ZipArchive::IndexZipFiles(HZIP hz) |
|---|
| 385 | { |
|---|
| 386 | if(hz != NULL && !mZipLoaded) |
|---|
| 387 | { |
|---|
| 388 | mZipRecord = hz; |
|---|
| 389 | |
|---|
| 390 | GetZipItem(hz,-1,&mMainRecord); |
|---|
| 391 | int numitems = mMainRecord.index; |
|---|
| 392 | |
|---|
| 393 | |
|---|
| 394 | for (int i = 0; i < numitems; i++) |
|---|
| 395 | { |
|---|
| 396 | ZIPENTRY* ze = new ZIPENTRY(); |
|---|
| 397 | |
|---|
| 398 | GetZipItem(hz, i, ze); |
|---|
| 399 | std::string name = ze->name; |
|---|
| 400 | |
|---|
| 401 | CleanupFileString(name); |
|---|
| 402 | |
|---|
| 403 | if(!name.empty()) |
|---|
| 404 | { |
|---|
| 405 | mZipIndex.insert(ZipEntryMapping(name, ze)); |
|---|
| 406 | } |
|---|
| 407 | |
|---|
| 408 | } |
|---|
| 409 | |
|---|
| 410 | mZipLoaded = true; |
|---|
| 411 | } |
|---|
| 412 | } |
|---|
| 413 | |
|---|
| 414 | ZIPENTRY* ZipArchive::GetZipEntry(const std::string& filename) |
|---|
| 415 | { |
|---|
| 416 | ZIPENTRY* ze = NULL; |
|---|
| 417 | std::string fileToLoad = filename; |
|---|
| 418 | CleanupFileString(fileToLoad); |
|---|
| 419 | |
|---|
| 420 | ZipEntryMap::iterator iter = mZipIndex.find(fileToLoad); |
|---|
| 421 | if(iter != mZipIndex.end()) |
|---|
| 422 | { |
|---|
| 423 | ze = (*iter).second; |
|---|
| 424 | } |
|---|
| 425 | |
|---|
| 426 | return ze; |
|---|
| 427 | } |
|---|
| 428 | |
|---|
| 429 | const ZIPENTRY* ZipArchive::GetZipEntry(const std::string& filename) const |
|---|
| 430 | { |
|---|
| 431 | ZIPENTRY* ze = NULL; |
|---|
| 432 | std::string fileToLoad = filename; |
|---|
| 433 | CleanupFileString(fileToLoad); |
|---|
| 434 | |
|---|
| 435 | ZipEntryMap::const_iterator iter = mZipIndex.find(fileToLoad); |
|---|
| 436 | if(iter != mZipIndex.end()) |
|---|
| 437 | { |
|---|
| 438 | ze = (*iter).second; |
|---|
| 439 | } |
|---|
| 440 | |
|---|
| 441 | return ze; |
|---|
| 442 | } |
|---|
| 443 | |
|---|
| 444 | osgDB::FileType ZipArchive::getFileType(const std::string& filename) const |
|---|
| 445 | { |
|---|
| 446 | const ZIPENTRY* ze = GetZipEntry(filename); |
|---|
| 447 | if(ze != NULL) |
|---|
| 448 | { |
|---|
| 449 | #ifdef ZIP_STD |
|---|
| 450 | if (ze->attr & S_IFDIR) |
|---|
| 451 | #else |
|---|
| 452 | if (ze->attr & FILE_ATTRIBUTE_DIRECTORY) |
|---|
| 453 | #endif |
|---|
| 454 | { |
|---|
| 455 | return osgDB::DIRECTORY; |
|---|
| 456 | } |
|---|
| 457 | else |
|---|
| 458 | { |
|---|
| 459 | return osgDB::REGULAR_FILE; |
|---|
| 460 | } |
|---|
| 461 | } |
|---|
| 462 | |
|---|
| 463 | return osgDB::FILE_NOT_FOUND; |
|---|
| 464 | } |
|---|
| 465 | |
|---|
| 466 | osgDB::DirectoryContents ZipArchive::getDirectoryContents(const std::string& dirName) const |
|---|
| 467 | { |
|---|
| 468 | osgDB::DirectoryContents dirContents; |
|---|
| 469 | |
|---|
| 470 | ZipEntryMap::const_iterator iter = mZipIndex.begin(); |
|---|
| 471 | ZipEntryMap::const_iterator iterEnd = mZipIndex.end(); |
|---|
| 472 | |
|---|
| 473 | for(; iter != iterEnd; ++iter) |
|---|
| 474 | { |
|---|
| 475 | std::string searchPath = dirName; |
|---|
| 476 | CleanupFileString(searchPath); |
|---|
| 477 | |
|---|
| 478 | if(iter->first.size() > searchPath.size()) |
|---|
| 479 | { |
|---|
| 480 | size_t endSubElement = iter->first.find(searchPath); |
|---|
| 481 | |
|---|
| 482 | |
|---|
| 483 | if(endSubElement == 0) |
|---|
| 484 | { |
|---|
| 485 | std::string remainingFile = iter->first.substr(searchPath.size() + 1, std::string::npos); |
|---|
| 486 | size_t endFileToken = remainingFile.find_first_of('/'); |
|---|
| 487 | |
|---|
| 488 | if(endFileToken == std::string::npos) |
|---|
| 489 | { |
|---|
| 490 | dirContents.push_back(remainingFile); |
|---|
| 491 | } |
|---|
| 492 | } |
|---|
| 493 | } |
|---|
| 494 | } |
|---|
| 495 | |
|---|
| 496 | return dirContents; |
|---|
| 497 | } |
|---|
| 498 | |
|---|
| 499 | std::string ZipArchive::ReadPassword(const osgDB::ReaderWriter::Options* options) const |
|---|
| 500 | { |
|---|
| 501 | |
|---|
| 502 | std::string password = ""; |
|---|
| 503 | if(options != NULL) |
|---|
| 504 | { |
|---|
| 505 | const osgDB::AuthenticationMap* credentials = options->getAuthenticationMap(); |
|---|
| 506 | if(credentials != NULL) |
|---|
| 507 | { |
|---|
| 508 | const osgDB::AuthenticationDetails* details = credentials->getAuthenticationDetails("ZipPlugin"); |
|---|
| 509 | if(details != NULL) |
|---|
| 510 | { |
|---|
| 511 | password = details->password; |
|---|
| 512 | } |
|---|
| 513 | } |
|---|
| 514 | } |
|---|
| 515 | |
|---|
| 516 | |
|---|
| 517 | if(password.empty()) |
|---|
| 518 | { |
|---|
| 519 | osgDB::Registry* reg = osgDB::Registry::instance(); |
|---|
| 520 | if(reg != NULL) |
|---|
| 521 | { |
|---|
| 522 | const osgDB::AuthenticationMap* credentials = reg->getAuthenticationMap(); |
|---|
| 523 | if(credentials != NULL) |
|---|
| 524 | { |
|---|
| 525 | const osgDB::AuthenticationDetails* details = credentials->getAuthenticationDetails("ZipPlugin"); |
|---|
| 526 | if(details != NULL) |
|---|
| 527 | { |
|---|
| 528 | password = details->password; |
|---|
| 529 | } |
|---|
| 530 | } |
|---|
| 531 | } |
|---|
| 532 | } |
|---|
| 533 | |
|---|
| 534 | return password; |
|---|
| 535 | } |
|---|
| 536 | |
|---|
| 537 | bool ZipArchive::CheckZipErrorCode(ZRESULT result) const |
|---|
| 538 | { |
|---|
| 539 | if(result == ZR_OK) |
|---|
| 540 | { |
|---|
| 541 | return true; |
|---|
| 542 | } |
|---|
| 543 | else |
|---|
| 544 | { |
|---|
| 545 | unsigned buf_size = 1025; |
|---|
| 546 | char* buf = new (std::nothrow) char[buf_size]; |
|---|
| 547 | buf[buf_size - 1] = 0; |
|---|
| 548 | |
|---|
| 549 | if (buf) |
|---|
| 550 | { |
|---|
| 551 | FormatZipMessage(result, buf, buf_size - 1); |
|---|
| 552 | |
|---|
| 553 | |
|---|
| 554 | |
|---|
| 555 | OSG_WARN << "Error loading zip file: " << getArchiveFileName() << ", Zip loader returned error: " << buf << "\n"; |
|---|
| 556 | delete [] buf; |
|---|
| 557 | } |
|---|
| 558 | |
|---|
| 559 | return false; |
|---|
| 560 | } |
|---|
| 561 | } |
|---|