std::unordered_map<ResourceID, unz_file_pos> CZipLoader::listFiles(const std::string & mountPoint, const std::string & archive) { std::unordered_map<ResourceID, unz_file_pos> ret; unzFile file = unzOpen(archive.c_str()); if (unzGoToFirstFile(file) == UNZ_OK) { do { unz_file_info info; std::vector<char> filename; // Fill unz_file_info structure with current file info unzGetCurrentFileInfo (file, &info, nullptr, 0, nullptr, 0, nullptr, 0); filename.resize(info.size_filename); // Get name of current file. Contrary to docs "info" parameter can't be null unzGetCurrentFileInfo (file, &info, filename.data(), filename.size(), nullptr, 0, nullptr, 0); std::string filenameString(filename.data(), filename.size()); unzGetFilePos(file, &ret[ResourceID(mountPoint + filenameString)]); } while (unzGoToNextFile(file) == UNZ_OK); } unzClose(file); return ret; }
bool ZipArchive::try_open_pack(const String& p_name) { //printf("opening zip pack %ls, %i, %i\n", p_name.c_str(), p_name.extension().nocasecmp_to("zip"), p_name.extension().nocasecmp_to("pcz")); if (p_name.get_extension().nocasecmp_to("zip") != 0 && p_name.get_extension().nocasecmp_to("pcz") != 0) return false; zlib_filefunc_def io; FileAccess* f = FileAccess::open(p_name, FileAccess::READ); if (!f) return false; io.opaque = f; io.zopen_file = godot_open; io.zread_file = godot_read; io.zwrite_file = godot_write; io.ztell_file = godot_tell; io.zseek_file = godot_seek; io.zclose_file = godot_close; io.zerror_file = godot_testerror; unzFile zfile = unzOpen2(p_name.utf8().get_data(), &io); ERR_FAIL_COND_V(!zfile, false); unz_global_info64 gi; int err = unzGetGlobalInfo64(zfile, &gi); ERR_FAIL_COND_V(err!=UNZ_OK, false); Package pkg; pkg.filename = p_name; pkg.zfile = zfile; packages.push_back(pkg); int pkg_num = packages.size()-1; for (unsigned int i=0;i<gi.number_entry;i++) { char filename_inzip[256]; unz_file_info64 file_info; err = unzGetCurrentFileInfo64(zfile,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0); ERR_CONTINUE(err != UNZ_OK); File f; f.package = pkg_num; unzGetFilePos(zfile, &f.file_pos); String fname = String("res://") + filename_inzip; files[fname] = f; uint8_t md5[16]={0,0,0,0,0,0,0,0 , 0,0,0,0,0,0,0,0}; PackedData::get_singleton()->add_path(p_name, fname, 1, 0, md5, this); //printf("packed data add path %ls, %ls\n", p_name.c_str(), fname.c_str()); if ((i+1)<gi.number_entry) { unzGoToNextFile(zfile); }; }; return true; };
bool Zip::open(const char* file) { _zipFile = unzOpen(file); if (!_zipFile) return false; char szCurrentFileName[PATH_MAX]; unz_file_info64 fileInfo; int err = unzGoToNextFile2(_zipFile, &fileInfo, szCurrentFileName, sizeof(szCurrentFileName) - 1, nullptr, 0, nullptr, 0); while (err == UNZ_OK) { unz_file_pos posInfo; if (unzGetFilePos(_zipFile, &posInfo) == UNZ_OK) { std::string currentFileName = szCurrentFileName; ZipEntryInfo entry; entry.pos = posInfo; entry.uncompressed_size = (uLong)fileInfo.uncompressed_size; _files[currentFileName] = entry; } err = unzGoToNextFile2(_zipFile, &fileInfo, szCurrentFileName, sizeof(szCurrentFileName) - 1, nullptr, 0, nullptr, 0); } return true; }
bool ZipFile::setFilter(const std::string& filterStr) { bool ret = false; do { CC_BREAK_IF(!m_data); CC_BREAK_IF(!m_data->zipFile); std::string filter(filterStr); //str_tolower(filter); // clear existing file list m_data->fileList.clear(); m_data->folderList.clear(); // UNZ_MAXFILENAMEINZIP + 1 - it is done so in unzLocateFile char szCurrentFileName[UNZ_MAXFILENAMEINZIP + 1]; unz_file_info64 fileInfo; // go through all files and store position information about the required files int err = unzGoToFirstFile64(m_data->zipFile, &fileInfo, szCurrentFileName, sizeof(szCurrentFileName) - 1); while (err == UNZ_OK) { unz_file_pos posInfo; int posErr = unzGetFilePos(m_data->zipFile, &posInfo); if (posErr == UNZ_OK) { std::string currentFileName = szCurrentFileName; //str_tolower(currentFileName); // cache info about filtered files only (like 'assets/') if (filter.empty() || currentFileName.substr(0, filter.length()) == filter) { ZipEntryInfo entry; entry.pos = posInfo; entry.uncompressed_size = (uLong)fileInfo.uncompressed_size; m_data->fileList[currentFileName] = entry; size_t pos = currentFileName.rfind('/'); while (pos != std::string::npos) { currentFileName = currentFileName.substr(0, pos); m_data->folderList.insert(currentFileName); pos = currentFileName.rfind('/'); } } } // next file - also get the information about it err = unzGoToNextFile64(m_data->zipFile, &fileInfo, szCurrentFileName, sizeof(szCurrentFileName) - 1); } ret = true; } while(false); return ret; }
bool nglZipFS::BuildIndex() { int res; unzFile Zip = mpPrivate->mZip; unz_global_info global_info; if (UNZ_OK != unzGetGlobalInfo(Zip, &global_info)) return false; if (UNZ_OK != unzGoToFirstFile(Zip)) return false; do { unz_file_info file_info; unz_file_pos file_pos; char filename[4096]; if (UNZ_OK != unzGetCurrentFileInfo(Zip, &file_info, filename, 4096, NULL, 0, NULL, 0)) return false; if (UNZ_OK != unzGetFilePos(Zip, &file_pos)) return false; uint len = strlen(filename); bool IsDir = (filename[len-1] == '\\') || (filename[len-1] == '/'); std::list<nglPath> List; nglZipPath::Decompose(filename, List); std::list<nglPath>::iterator it; std::list<nglPath>::iterator end = List.end(); Node* pPath = &mRoot; int i = List.size(); for (it = List.begin(); it != end; ++it) { nglString name = (*it).GetPathName(); Node* pChild = pPath->Find(name); if (!pChild) { //printf("zipfile: %s\n", name.GetChars()); pChild = new Node(name, file_info.uncompressed_size, file_pos.pos_in_zip_directory, file_pos.num_of_file, i == 1 && !IsDir); pPath->AddChild(pChild); } pPath = pChild; i--; } } while (UNZ_OK == (res = unzGoToNextFile(Zip))); if (res == UNZ_END_OF_LIST_OF_FILE) return true; return false; }
std::vector<std::pair<std::string, File*>> ZipArchive::GetFiles() { std::vector<std::pair<std::string, File*>> result; unz_global_info globalInfo; if (unzGetGlobalInfo(archive, &globalInfo) != UNZ_OK) { return result; } for (uLong i = 0; i < globalInfo.number_entry; ++i) { // Get the filename size. unz_file_info fileInfo; if (unzGetCurrentFileInfo(archive, &fileInfo, NULL, 0, NULL, 0, NULL, 0) != UNZ_OK) { std::cerr << "Failed to read file info. Entry " << i << " in " << archivePath << std::endl; unzGoToNextFile(archive); continue; } // Get the filename now that we have the filename size. std::string filename; filename.resize(fileInfo.size_filename); if (unzGetCurrentFileInfo(archive, &fileInfo, &filename[0], filename.size(), NULL, 0, NULL, 0) != UNZ_OK) { std::cerr << "Failed to read file info. Entry " << i << " in " << archivePath << std::endl; unzGoToNextFile(archive); continue; } // Do not add directories. if (filename[filename.size() - 1] == '/') { unzGoToNextFile(archive); continue; } // Get the position of the file and create a file. unz_file_pos position; if (unzGetFilePos(archive, &position) != UNZ_OK) { std::cerr << "Failed to read file info. Entry " << i << " in " << archivePath << std::endl; continue; } File* file = new ZipArchiveFile(archive, fileInfo, position, &mutex); result.push_back(std::pair<std::string, File*>(filename, file)); // Move to the next file in the archive. unzGoToNextFile(archive); } return result; }
bool zip::ZipArchiveInput::Index() { static const int UNZ_MAXFILENAMEINZIP = 256; int err = unzGoToFirstFile(uf); if( err != UNZ_OK ) { m_errorMessage << "Can't go to first file" << std::endl; return false; } while (err == UNZ_OK) { char szCurrentFileName[UNZ_MAXFILENAMEINZIP+1]; err = unzGetCurrentFileInfo64(uf, NULL, szCurrentFileName, sizeof(szCurrentFileName)-1,NULL,0,NULL,0); if(err == UNZ_OK) { #ifdef SCARAB_WCHAR_MODE String_t fileNameKey = utf_convert::as_wide( szCurrentFileName ); #else String_t fileNameKey = szCurrentFileName; #endif if( !m_caseSensitive ) for (size_t i = 0; i < fileNameKey.size(); ++i ) fileNameKey[i] = _ttolower( fileNameKey[i] ); unz_file_pos pos; err = unzGetFilePos( uf, &pos ); if( err != UNZ_OK ) { m_errorMessage << "Can't get file position for " << fileNameKey << std::endl; return false; } ZipEntry zipEntry; zipEntry.pos_in_zip_directory = pos.pos_in_zip_directory; zipEntry.num_of_file = pos.num_of_file; m_nameToEntry.insert( std::make_pair( fileNameKey, zipEntry ) ); err = unzGoToNextFile(uf); if( err != UNZ_OK && err != UNZ_END_OF_LIST_OF_FILE ) { m_errorMessage << "Can't go to next file" << std::endl; return false; } } else { m_errorMessage << "Can't get file info" << std::endl; return false; } } return err == UNZ_END_OF_LIST_OF_FILE; }
void FileSystemZip::CacheIndex() { assert(m_cache.empty() && "Why would you want to call this twice?"); uLong i; unz_global_info gi; int err; err = unzGetGlobalInfo (m_uf,&gi); if (err!=UNZ_OK) { LogError("error %d with zipfile in unzGetGlobalInfo \n",err); return; } unzGoToFirstFile(m_uf); ZipCacheEntry entry; for (i=0;i<gi.number_entry;i++) { char filename_inzip[512]; unz_file_info file_info; err = unzGetCurrentFileInfo(m_uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0); if (err!=UNZ_OK) { LogError("error %d with zipfile in unzGetCurrentFileInfo\n",err); break; } err = unzGetFilePos(m_uf, &entry.m_filepos); if (err!=UNZ_OK) { LogError("error %d with zipfile in unzGetFilePos\n",err); break; } m_cache[filename_inzip] = entry; if ((i+1)<gi.number_entry) { err = unzGoToNextFile(m_uf); if (err!=UNZ_OK) { LogError("error %d with zipfile in unzGoToNextFile\n",err); break; } } } LogMsg("Cache has %d files.", m_cache.size()); }
KDbool ZipFile::setFilter ( const std::string& sFilter ) { KDbool bRet = KD_FALSE; do { CC_BREAK_IF ( !m_pData ); CC_BREAK_IF ( !m_pData->zipFile ); // clear existing file list m_pData->fileList.clear ( ); // UNZ_MAXFILENAMEINZIP + 1 - it is done so in unzLocateFile KDchar szCurrentFileName [ UNZ_MAXFILENAMEINZIP + 1 ]; unz_file_info64 tFileInfo; // go through all files and store position information about the required files KDint nErr = unzGoToFirstFile64 ( m_pData->zipFile, &tFileInfo, szCurrentFileName, sizeof ( szCurrentFileName ) - 1 ); while ( nErr == UNZ_OK ) { unz_file_pos tPosInfo; KDint nPosErr = unzGetFilePos ( m_pData->zipFile, &tPosInfo ); if ( nPosErr == UNZ_OK ) { std::string sCurrentFileName = szCurrentFileName; // cache info about filtered files only (like 'assets/') if ( sFilter.empty ( ) || sCurrentFileName.substr ( 0, sFilter.length ( ) ) == sFilter ) { ZipEntryInfo tEntry; tEntry.pos = tPosInfo; tEntry.uncompressed_size = (uLong) tFileInfo.uncompressed_size; m_pData->fileList [ szCurrentFileName ] = tEntry; } } // next file - also get the information about it nErr = unzGoToNextFile64 ( m_pData->zipFile, &tFileInfo, szCurrentFileName, sizeof ( szCurrentFileName ) - 1 ); } bRet = KD_TRUE; } while ( 0 ); return bRet; }
void Zips::read(unzFile zp) { do { unz_file_pos pos; unzGetFilePos(zp, &pos); file_entry entry; unzGetCurrentFileInfo(zp, 0, entry.name, sizeof(entry.name) - 1, 0, 0, 0, 0); entry.pos = pos; entry.zp = zp; //strcpy(entry.name, entry.name); _files.push_back(entry); } while (unzGoToNextFile(zp) != UNZ_END_OF_LIST_OF_FILE); _sort = true; }
bool CCZipFile::genFileList() { bool ret = false; do { CC_BREAK_IF(!m_zipFile); // clear existing file list m_fileList.clear(); // UNZ_MAXFILENAMEINZIP + 1 - it is done so in unzLocateFile char szCurrentFileName[UNZ_MAXFILENAMEINZIP + 1]; unz_file_info64 fileInfo; // go through all files and store position information about the required files int err = unzGoToFirstFile64(m_zipFile, &fileInfo, szCurrentFileName, sizeof(szCurrentFileName) - 1); while (err == UNZ_OK) { unz_file_pos posInfo; int posErr = unzGetFilePos(m_zipFile, &posInfo); if (posErr == UNZ_OK) { std::string currentFileName = szCurrentFileName; { __ZipEntryInfo entry; entry.pos = posInfo; entry.uncompressed_size = (uLong)fileInfo.uncompressed_size; m_fileList[currentFileName] = entry; } } // next file - also get the information about it err = unzGoToNextFile64(m_zipFile, &fileInfo, szCurrentFileName, sizeof(szCurrentFileName) - 1); } ret = true; } while(false); m_hasGenFlist = true; return ret; }
void Unzip::retrieveAllFileInfos(void) { do{ unz_file_pos pos; if(UNZ_OK != unzGetFilePos(zipfile_handle, &pos)){ continue; } unz_file_info info; char currentFileName[CPPZIP_UNZIP_CHAR_ARRAY_BUFFER_SIZE]; char currentExtraField[CPPZIP_UNZIP_CHAR_ARRAY_BUFFER_SIZE]; char currentComment[CPPZIP_UNZIP_CHAR_ARRAY_BUFFER_SIZE]; unzGetCurrentFileInfo(zipfile_handle, &info, currentFileName, CPPZIP_UNZIP_CHAR_ARRAY_BUFFER_SIZE, currentExtraField, CPPZIP_UNZIP_CHAR_ARRAY_BUFFER_SIZE, currentComment, CPPZIP_UNZIP_CHAR_ARRAY_BUFFER_SIZE); std::shared_ptr<InnerZipFileInfo> innerFileInfo(new InnerZipFileInfo()); innerFileInfo->fileName = currentFileName; innerFileInfo->extraField = currentExtraField; innerFileInfo->comment = currentComment; innerFileInfo->time_sec = info.tmu_date.tm_sec; innerFileInfo->time_min = info.tmu_date.tm_min; innerFileInfo->time_hour = info.tmu_date.tm_hour; innerFileInfo->time_day_of_month = info.tmu_date.tm_mday; innerFileInfo->time_month = info.tmu_date.tm_mon; innerFileInfo->time_year = info.tmu_date.tm_year; innerFileInfo->dosDate = info.dosDate; innerFileInfo->crc = info.crc; innerFileInfo->compressed_size = info.compressed_size; innerFileInfo->uncompressed_size = info.uncompressed_size; innerFileInfo->internal_fileAttributes = info.internal_fa; innerFileInfo->external_fileAttributes = info.external_fa; fileInfos.insert(std::make_pair(innerFileInfo->fileName, innerFileInfo)); } while(UNZ_OK == unzGoToNextFile(zipfile_handle)); }
CArchiveZip::CArchiveZip(const std::string& name): CArchiveBuffered(name), curSearchHandle(1) { #ifdef USEWIN32IOAPI zlib_filefunc_def ffunc; fill_win32_filefunc(&ffunc); zip = unzOpen2(name.c_str(),&ffunc); #else zip = unzOpen(name.c_str()); #endif if (!zip) { LogObject() << "Error opening " << name; return; } // We need to map file positions to speed up opening later for (int ret = unzGoToFirstFile(zip); ret == UNZ_OK; ret = unzGoToNextFile(zip)) { unz_file_info info; char fname[512]; unzGetCurrentFileInfo(zip, &info, fname, 512, NULL, 0, NULL, 0); const std::string name = StringToLower(fname); if (name.empty()) { continue; } const char last = name[name.length() - 1]; if ((last == '/') || (last == '\\')) { continue; // exclude directory names } FileData fd; unzGetFilePos(zip, &fd.fp); fd.size = info.uncompressed_size; fd.origName = fname; fd.crc = info.crc; fileData[name] = fd; } }
void UnzipBase::buildIndices() { if(!_handle) return; _indices.clear(); _indices_ci.clear(); int ret; for(ret=unzGoToFirstFile(_handle);ret==UNZ_OK;ret=unzGoToNextFile(_handle)) { char filename[4096]; if(unzGetCurrentFileInfo(_handle,0,filename,(unsigned long)sizeof(filename),0,0,0,0)!=UNZ_OK) continue; filename[4095]=0; __int64 index=0; unzGetFilePos(_handle,(unz_file_pos*)&index); _indices[filename]=index; _strlwr_s(filename); _indices_ci[filename]=index; } }
CZipArchive::CZipArchive(const std::string& archiveName) : IArchive(archiveName) { zip = unzOpen(archiveName.c_str()); if (!zip) { LOG_ERROR("Error opening %s", archiveName.c_str()); return; } // We need to map file positions to speed up opening later for (int ret = unzGoToFirstFile(zip); ret == UNZ_OK; ret = unzGoToNextFile(zip)) { unz_file_info info; char fName[512]; unzGetCurrentFileInfo(zip, &info, fName, 512, NULL, 0, NULL, 0); const std::string fLowerName = fName; if (fLowerName.empty()) { continue; } const char last = fLowerName[fLowerName.length() - 1]; if ((last == '/') || (last == '\\')) { continue; // exclude directory names } FileData fd; unzGetFilePos(zip, &fd.fp); fd.size = info.uncompressed_size; fd.origName = fName; fd.crc = info.crc; fd.mode = 0755; if (info.external_fa>0) fd.mode = info.external_fa & 0755; fileData.push_back(fd); // lcNameIndex[fLowerName] = fileData.size() - 1; } }
bool ZipReader::Open( StringRef path) { Close(); mZipFile = unzOpen(path.c_str()); if (mZipFile!=nullptr) { StackString<Path::MaxPathLength> outFileName; //unz_file_info64 outFileInfo; int err = unzGoToFirstFile(mZipFile); //int err = unzGoToFirstFile(mZipFile, &outFileInfo, outFileName.c_str(), static_cast<uLong>((outFileName.Size() - 1)*sizeof(char))); outFileName.ForceUpdateLength(); while (err == UNZ_OK) { unz_file_pos posInfo; int posErr = unzGetFilePos(mZipFile, &posInfo); if (posErr == UNZ_OK) { //TODO: invoke unzGetCurrentFileInfo ZipFileInfo entry; entry.Pos = posInfo; //entry.UncompressedSize = (uLong)outFileInfo.uncompressed_size; mFileDict.Add(outFileName.ToString(),entry); } err = unzGoToNextFile(mZipFile); //err = unzGoToNextFile(mZipFile, &outFileInfo, outFileName.c_str(), static_cast<uLong>((outFileName.Size() - 1)*sizeof(char))); outFileName.ForceUpdateLength(); } return true; } return false; }
CArchiveZip::CArchiveZip(const string& name) : CArchiveBuffered(name), curSearchHandle(1) { #ifdef USEWIN32IOAPI zlib_filefunc_def ffunc; fill_win32_filefunc(&ffunc); zip = unzOpen2(name.c_str(),&ffunc); #else zip = unzOpen(name.c_str()); #endif if (!zip) return; // We need to map file positions to speed up opening later for (int ret = unzGoToFirstFile(zip); ret == UNZ_OK; ret = unzGoToNextFile(zip)) { unz_file_info info; char fname[512]; string name; unzGetCurrentFileInfo(zip, &info, fname, 512, NULL, 0, NULL, 0); if (info.uncompressed_size > 0) { name = StringToLower(fname); // SetSlashesForwardToBack(name); FileData fd; unzGetFilePos(zip, &fd.fp); fd.size = info.uncompressed_size; fd.origName = fname; fd.crc = info.crc; // SetSlashesForwardToBack(fd.origName); fileData[name] = fd; } } }
CZipArchive::CZipArchive(FileReader &file) : ArchiveBase(file) //------------------------------------------------------------ { zlib_filefunc_def functions = { ZipFileAbstraction::fopen_mem, ZipFileAbstraction::fread_mem, ZipFileAbstraction::fwrite_mem, ZipFileAbstraction::ftell_mem, ZipFileAbstraction::fseek_mem, ZipFileAbstraction::fclose_mem, ZipFileAbstraction::ferror_mem, &inFile }; zipFile = unzOpen2(nullptr, &functions); if(zipFile == nullptr) { return; } // read comment { unz_global_info info; if(unzGetGlobalInfo(zipFile, &info) == UNZ_OK) { if(info.size_comment > 0) { if(info.size_comment < Util::MaxValueOfType(info.size_comment)) { info.size_comment++; } std::vector<char> commentData(info.size_comment); if(unzGetGlobalComment(zipFile, &commentData[0], info.size_comment) >= 0) { commentData[info.size_comment - 1] = '\0'; comment = mpt::ToWide(mpt::CharsetCP437, &commentData[0]); } } } } // read contents unz_file_pos curFile; int status = unzGoToFirstFile(zipFile); unzGetFilePos(zipFile, &curFile); while(status == UNZ_OK) { ArchiveFileInfo fileinfo; fileinfo.type = ArchiveFileNormal; unz_file_info info; char name[256]; unzGetCurrentFileInfo(zipFile, &info, name, sizeof(name), nullptr, 0, nullptr, 0); fileinfo.name = mpt::PathString::FromWide(mpt::ToWide(mpt::CharsetCP437, std::string(name))); fileinfo.size = info.uncompressed_size; unzGetFilePos(zipFile, &curFile); fileinfo.cookie1 = curFile.pos_in_zip_directory; fileinfo.cookie2 = curFile.num_of_file; contents.push_back(fileinfo); status = unzGoToNextFile(zipFile); } }
/*! Open/Extract a file from disk and load it in memory. \param[in] filename The file to load in memory. \param[in] relative_path Determine if the filename is an absolute or relative path. \return Return a MEMORY structure pointer if the file is found and loaded, instead will return NULL. */ MEMORY *mopen( char *filename, unsigned char relative_path ) { #ifdef __IPHONE_4_0 FILE *f; char fname[ MAX_PATH ] = {""}; if( relative_path ) { get_file_path( getenv( "FILESYSTEM" ), fname ); strcat( fname, filename ); } else strcpy( fname, filename ); f = fopen( fname, "rb" ); if( !f ) return NULL; MEMORY *memory = ( MEMORY * ) calloc( 1, sizeof( MEMORY ) ); strcpy( memory->filename, fname ); fseek( f, 0, SEEK_END ); memory->size = ftell( f ); fseek( f, 0, SEEK_SET ); memory->buffer = ( unsigned char * ) calloc( 1, memory->size + 1 ); fread( memory->buffer, memory->size, 1, f ); memory->buffer[ memory->size ] = 0; fclose( f ); return memory; #else char fpath[ MAX_PATH ] = {""}, fname[ MAX_PATH ] = {""}; unzFile uf; unz_file_info fi; unz_file_pos fp; strcpy( fpath, getenv( "FILESYSTEM" ) ); uf = unzOpen( fpath ); if( !uf ) return NULL; if( relative_path ) sprintf( fname, "assets/%s", filename ); else strcpy( fname, filename ); unzGoToFirstFile( uf ); MEMORY *memory = ( MEMORY * ) calloc( 1, sizeof( MEMORY ) ); unzGetFilePos( uf, &fp ); if( unzLocateFile( uf, fname, 1 ) == UNZ_OK ) { unzGetCurrentFileInfo( uf, &fi, memory->filename, MAX_PATH, NULL, 0, NULL, 0 ); if( unzOpenCurrentFilePassword( uf, NULL ) == UNZ_OK ) { memory->position = 0; memory->size = fi.uncompressed_size; memory->buffer = ( unsigned char * ) realloc( memory->buffer, fi.uncompressed_size + 1 ); memory->buffer[ fi.uncompressed_size ] = 0; while( unzReadCurrentFile( uf, memory->buffer, fi.uncompressed_size ) > 0 ){} unzCloseCurrentFile( uf ); unzClose( uf ); return memory; } } unzClose( uf ); return NULL; #endif }
int load_game_scripts(lua_State* L, const char* zipfile) { size_t size = 0; unsigned char* filedata = s_read(zipfile, &size, 0); if (!filedata) { fprintf(stderr, "unable to read zipfile = %s\n", zipfile); return 1; } unzFile unzfile = unzOpenBuffer(filedata, size); if(!unzfile) { fprintf(stderr, "open zip from buffer failed.\n"); return 1; } lua_getglobal(L, "package"); lua_getfield(L, -1, "preload"); char filename[1024] = ""; int err = unzGoToFirstFile(unzfile); if (err) { fprintf(stderr, "go to first file failed"); return 1; } bool succeed = true; while (true) { unz_file_info info; err = unzGetCurrentFileInfo(unzfile, &info, filename, 1024, NULL, 0, NULL, 0); if (err) { fprintf(stderr, "get current file info failed, filename = %s\n", filename); succeed = false; break; } unz_file_pos file_pos; err = unzGetFilePos(unzfile, &file_pos); err = unzGoToFilePos(unzfile, &file_pos); err = unzOpenCurrentFile(unzfile); unsigned long size = info.uncompressed_size; unsigned char* buffer = s_malloc(size); unsigned int readed = unzReadCurrentFile(unzfile, buffer, (unsigned int)size); if(readed != size) { succeed = false; fprintf(stderr, "read zip file failed? error size, required = %ld, readed = %d, filename = %s\n", size, readed, filename); goto error; } err = luaL_loadbuffer(L, (const char*)buffer, size, filename); if(err) { fprintf(stderr, "error loadL_loadbuffer, filename = %s\n", filename); goto error; } lua_setfield(L, -2, filename); if(unzGoToNextFile(unzfile) == UNZ_END_OF_LIST_OF_FILE) { succeed = true; break; } unzCloseCurrentFile(unzfile); s_free(buffer); continue; error: succeed = false; unzCloseCurrentFile(unzfile); s_free(buffer); break; } lua_pop(L, -1); return succeed ? 1 : 0; }
void ZipInput::ReadInfos(void *masterFile) { // Read infos m_fileInfos.reserve(m_numberOfFiles); if (unzGoToFirstFile(static_cast<unzFile>(masterFile)) != UNZ_OK) { LogPedantic("Failed to go to first file"); ThrowMsg(Exception::SeekFileFailed, "Failed to seek first file"); } for (size_t i = 0; i < m_numberOfFiles; ++i) { unz_file_pos_s filePos; if (unzGetFilePos(static_cast<unzFile>(masterFile), &filePos) != UNZ_OK) { LogPedantic("Failed to get file pos"); ThrowMsg(Exception::FileInfoFailed, "Failed to get zip file info"); } unz_file_info64 fileInfo; if (unzGetCurrentFileInfo64(static_cast<unzFile>(masterFile), &fileInfo, NULL, 0, NULL, 0, NULL, 0) != UNZ_OK) { LogPedantic("Failed to get file pos"); ThrowMsg(Exception::FileInfoFailed, "Failed to get zip file info"); } ScopedArray<char> fileName(new char[fileInfo.size_filename + 1]); ScopedArray<char> fileComment(new char[fileInfo.size_file_comment + 1]); if (unzGetCurrentFileInfo64(static_cast<unzFile>(masterFile), &fileInfo, fileName.Get(), fileInfo.size_filename + 1, NULL, 0, fileComment.Get(), fileInfo.size_file_comment + 1) != UNZ_OK) { LogPedantic("Failed to get file pos"); ThrowMsg(Exception::FileInfoFailed, "Failed to get zip file info"); } m_fileInfos.push_back( FileInfo( FileHandle( static_cast<size_t>(filePos.pos_in_zip_directory), static_cast<size_t>(filePos.num_of_file) ), std::string(fileName.Get()), std::string(fileComment.Get()), static_cast<off64_t>(fileInfo.compressed_size), static_cast<off64_t>(fileInfo.uncompressed_size) ) ); // If this is not the last file, go to next one if (i != m_numberOfFiles - 1) { if (unzGoToNextFile( static_cast<unzFile>(masterFile))!= UNZ_OK) { LogPedantic("Failed to go to next file"); ThrowMsg(Exception::FileInfoFailed, "Failed to go to next file"); } } } }
void DataBuffer::read(char *filename, unsigned char relativePath) { if (this->filename != NULL) this->filename[0] = '\0'; this->size = 0; this->position = 0; if (this->buffer != NULL) free(this->buffer); #ifdef __IPHONE_4_0 FILE *f; char fname[MAX_PATH] = {""}; if(relativePath) { get_file_path(getenv("FILESYSTEM"), fname); strcat(fname, filename); } else strcpy(fname, filename); f = fopen(fname, "rb"); if(!f) return NULL; // DataBuffer *dataBuffer = (DataBuffer *) calloc(1, sizeof(DataBuffer)); strcpy(this->filename, fname); fseek(f, 0, SEEK_END); this->size = ftell(f); fseek(f, 0, SEEK_SET); this->buffer = (unsigned char *) calloc(1, this->size + 1); fread(this->buffer, this->size, 1, f); this->buffer[this->size] = 0; fclose(f); return ; #else char fpath[MAX_PATH] = {""}, fname[MAX_PATH] = {""}; unzFile uf; unz_file_info fi; unz_file_pos fp; strcpy(fpath, getenv("FILESYSTEM")); uf = unzOpen(fpath); if(!uf) return ; if(relativePath) sprintf(fname, "assets/%s", filename); else strcpy(fname, filename); unzGoToFirstFile(uf); // DataBuffer *dataBuffer = (DataBuffer *) calloc(1, sizeof(DataBuffer)); unzGetFilePos(uf, &fp); if(unzLocateFile(uf, fname, 1) == UNZ_OK) { unzGetCurrentFileInfo( uf, &fi, this->filename, MAX_PATH, NULL, 0, NULL, 0); if(unzOpenCurrentFilePassword(uf, NULL) == UNZ_OK) { this->position = 0; this->size = fi.uncompressed_size; this->buffer = (unsigned char *) realloc(this->buffer, fi.uncompressed_size + 1); this->buffer[fi.uncompressed_size] = 0; while(unzReadCurrentFile(uf, this->buffer, fi.uncompressed_size) > 0) {} unzCloseCurrentFile(uf); unzClose(uf); return; } } unzClose(uf); return ; #endif }
void initMap(const char **classPath, int cpSize) { int i; struct stat s; initZipMap(&classNameToJar, 500, 0.7, stringHash, stringEq); for (i = 0; i < cpSize; i++) { if (stat(classPath[i], &s) >= 0) { if (S_ISREG(s.st_mode) && (endsWith(classPath[i], ".jar") || endsWith(classPath[i], ".zip"))) { /* Add all of the class files from this jar file * to the growing hash map */ /* FIXME: cleaner method can be used ... */ unzFile uf; unz_global_info gi; uLong j; char *s; uf = unzOpen(classPath[i]); if (uf == NULL) { fprintf(stderr, "Unable to open \"%s\"\n", classPath[i]); continue; } if (unzGetGlobalInfo (uf,&gi) != UNZ_OK) { fprintf(stderr, "Failed to get Global Info\n"); unzClose(uf); continue; } for (j = 0; j < gi.number_entry; j++) { char filename_inzip[256]; unz_file_info file_info; /* int unused; */ if (unzGetCurrentFileInfo(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0) != UNZ_OK) { fprintf(stderr, "Failed to get file info\n"); unzClose(uf); continue; } if (endsWith(filename_inzip, ".class")) { size_t nameLen; JarEntry_t *jarEntry; jarEntry = NEW(JarEntry_t); if (jarEntry == NULL) { fprintf(stderr, "Failed to create new jar entry\n"); unzCloseCurrentFile(uf); unzClose(uf); continue; } nameLen = strlen(filename_inzip); s = (char *) calloc(nameLen - 5, sizeof(char)); strncpy(s, filename_inzip, nameLen - 6); replace(s, '/', '.'); jarEntry->path = (char *) classPath[i]; if (unzGetFilePos(uf, &(jarEntry->pos)) == UNZ_OK) { if (zipMapPut(&classNameToJar, s, jarEntry) != ZIP_MAP_OK) { fprintf(stderr, "Failed to store the relationship \"%s\" --> \"%s\"\n", s, classPath[i]); } } else { fprintf(stderr, "Failed to store the relationship (2) \"%s\" --> \"%s\"\n", s, classPath[i]); } } unzCloseCurrentFile(uf); if ((j != gi.number_entry - 1) && (unzGoToNextFile(uf) != UNZ_OK)) { fprintf(stderr, "Could not fetch next file\n"); unzCloseCurrentFile(uf); unzClose(uf); continue; } } unzCloseCurrentFile(uf); unzClose(uf); } } } }