Gandi Posted February 22, 2010 Share Posted February 22, 2010 I want to store game specific data in a .pak file which I load with the C++ ifstream. However, if I do something like: ifstream file; file.open(AbstractPath("abstract::myfile.txt")); it obviously doesn't work, because only the engine's LoadXY() - functions can read from zip-files. However, I'm pretty sure that the engine can already extract files from .pak's because of the cache:: - protocol, so something like ifstream file; //This returns the path of the cached file str path = CacheFile("abstract::myfile.txt"); file.open(path); Would be great^^ Quote Link to comment Share on other sites More sharing options...
Canardia Posted February 22, 2010 Share Posted February 22, 2010 You can use ZLib (includes MiniZip): http://www.winimage.com/zLibDll/minizip.html And then you can read a text file into a C++ string: /* unzips testfile.txt from C:\temp\test.zip and puts it in a string */ #include <cstdio> #include <string> #include <iostream> #include "unzip.h" // MiniZip library #define WRITEBUFFERSIZE (5242880) // 5Mb buffer using namespace std; string readZipFile(string zipFile, string fileInZip) { int err = UNZ_OK; // error status uInt size_buf = WRITEBUFFERSIZE; // byte size of buffer to store raw csv data void* buf; // the buffer string sout; // output strings char filename_inzip[256]; // for unzGetCurrentFileInfo unz_file_info file_info; // for unzGetCurrentFileInfo unzFile uf = unzOpen(zipFile.c_str()); // open zipfile stream if (uf==NULL) { cerr << "Cannot open " << zipFile << endl; return sout; } // file is open if ( unzLocateFile(uf,fileInZip.c_str(),1) ) { // try to locate file inside zip // second argument of unzLocateFile: 1 = case sensitive, 0 = case-insensitive cerr << "File " << fileInZip << " not found in " << zipFile << endl; return sout; } // file inside zip found if (unzGetCurrentFileInfo(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0)) { cerr << "Error " << err << " with zipfile " << zipFile << " in unzGetCurrentFileInfo." << endl; return sout; } // obtained the necessary details about file inside zip buf = (void*)malloc(size_buf); // setup buffer if (buf==NULL) { cerr << "Error allocating memory for read buffer" << endl; return sout; } // buffer ready err = unzOpenCurrentFilePassword(uf,NULL); // Open the file inside the zip (password = NULL) if (err!=UNZ_OK) { cerr << "Error " << err << " with zipfile " << zipFile << " in unzOpenCurrentFilePassword." << endl; return sout; } // file inside the zip is open // Copy contents of the file inside the zip to the buffer cout << "Extracting: " << filename_inzip << " from " << zipFile << endl; do { err = unzReadCurrentFile(uf,buf,size_buf); if (err<0) { cerr << "Error " << err << " with zipfile " << zipFile << " in unzReadCurrentFile" << endl; sout = ""; // empty output string break; } // copy the buffer to a string if (err>0) for (int i = 0; i < (int) err; i++) sout.push_back( *(((char*)buf)+i) ); } while (err>0); err = unzCloseCurrentFile (uf); // close the zipfile if (err!=UNZ_OK) { cerr << "Error " << err << " with zipfile " << zipFile << " in unzCloseCurrentFile" << endl; sout = ""; // empty output string } free(buf); // free up buffer memory return sout; } int main(int argc, char *argv[]) { string string_buffer = readZipFile("C:/temp/test.zip", "testfile.txt"); cout << string_buffer << endl; return 0; } Quote ■ Ryzen 9 ■ RX 6800M ■ 16GB ■ XF8 ■ Windows 11 ■ ■ Ultra ■ LE 2.5 ■ 3DWS 5.6 ■ Reaper ■ C/C++ ■ C# ■ Fortran 2008 ■ Story ■ ■ Homepage: https://canardia.com ■ Link to comment Share on other sites More sharing options...
Gandi Posted February 22, 2010 Author Share Posted February 22, 2010 Yeah I know how to unzip files, but I thought this was an easy-to-implement and though useful function, so I made a request. Quote Link to comment Share on other sites More sharing options...
Masterxilo Posted February 22, 2010 Share Posted February 22, 2010 True, a way to load any file from a pak would be really neat. Quote Hurricane-Eye Entertainment - Site, blog. Link to comment Share on other sites More sharing options...
omid3098 Posted February 23, 2010 Share Posted February 23, 2010 True, a way to load any file from a pak would be really neat. + Quote Omid Saadat OD Arts Blog AMD Phenom II X4 940 - Geforce 8800GTS - 4GB RAM - XP x86 AMD 6000+ - Geforce 9800 GT - 2GB RAM - XP x86 (Home pc) Intel Core i7 - Geforce 310M - 4GB Ram - Win7 x64 (Laptop) Link to comment Share on other sites More sharing options...
tec.imp Posted April 9, 2010 Share Posted April 9, 2010 I second that! Would be quite a useful feature. Quote Link to comment Share on other sites More sharing options...
NivorbiaN Posted June 4, 2010 Share Posted June 4, 2010 +10 i have a (mapname).set file that holds the fog values for that particular map, it would be neat to hide it in the .pak(password protected), so it's harder to cheat by turning them off in the .set file. Quote "Hmm, don't have time to play with myself." ~Duke Nuke'm Link to comment Share on other sites More sharing options...
ZioRed Posted June 4, 2010 Share Posted June 4, 2010 +1 here Since the engine is capable of loading a file directly from the .pak as it does with LoadModel, LoadMesh etc. could be very useful to give us a command LoadFile("abstract::mydata.dat") which returns the content of file as string (and we can parse it splitting, deserializing etc). I would need it for the Gamelib porting too since atm I'm writing a file system managament by myself for the AbstractPath problem Quote ?? FRANCESCO CROCETTI ?? http://skaredcreations.com Link to comment Share on other sites More sharing options...
TylerH Posted June 18, 2010 Share Posted June 18, 2010 I agree with ZR on the LoadFile type command, but instead of a string, if we could simply get a pointer to the file's memory, and then from there we can get it as binary, XML, text, or whatever, in any encoding. Quote nVidia 530M Intel Core i7 - 2.3Ghz 8GB DDR3 RAM Windows 7 Ultimate (64x)----- Visual Studio 2010 Ultimate Google Chrome Creative Suite 5 FL Studio 10 Office 15 ----- Expert Professional Expert BMX Programmer ----- Link to comment Share on other sites More sharing options...
paramecij Posted June 19, 2010 Share Posted June 19, 2010 I agree with ZR on the LoadFile type command, but instead of a string, if we could simply get a pointer to the file's memory, and then from there we can get it as binary, XML, text, or whatever, in any encoding. agree Quote Link to comment Share on other sites More sharing options...
L B Posted July 3, 2010 Share Posted July 3, 2010 How come so many +s and no official reply? This seems not so hard to implement and useful (+1 by the way) Quote Link to comment Share on other sites More sharing options...
Masterxilo Posted July 4, 2010 Share Posted July 4, 2010 Looks like there are more important things to do... Quote Hurricane-Eye Entertainment - Site, blog. Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.