zlib and files in a directory?

Started by
11 comments, last by GameDev.net 18 years, 4 months ago
OK, I have done with zlib. Is working in my project, but I noticed that zlib can't handle more than one file at once(like gzip), and can't handle directories. So if I plan to "zip" a directory with my game files, how can I do that? Something like unix tar? And than compress the tar file? Or have another way to handle this problem?
Advertisement
The easiest way to do that would be to use a library like PhysFS, or my own library if you prefer C++ style.

PhysFS: http://icculus.org/physfs/

VFS: http://www.emilieprema.com/persil/vfs.html

Good luck.
hi

I have written a zip file read, if you would like to use it just pm me

maybe we could develop it further on together, I planned to release it for free somewhen

it have tested it a bit and it worked fine

however its just for reading no writing of zip files supported til now
http://www.8ung.at/basiror/theironcross.html
ok, I'm giving the physfs a try, but the things appear a little trick.
I'm writing this code, but I don't know if it's right. The function PHYSFS_read() takes a parameter buffer, but I must not allocate this first? With PHYSFS_setBuffer()? I'm confused :
	if ( PHYSFS_init( argv[0] ) == 0 ){ //Para inicializar o arquivo PAK		std::cout << "Inicialização do arquivo PAK falhou." << PHYSFS_getLastError() << std::endl;		SDL_Quit( );		return -1;	}	//std::cout << PHYSFS_addToSearchPath(".",1) << std::endl;	PHYSFS_addToSearchPath(".",1);	if ( PHYSFS_exists("pak.zip") == 0 ){		std::cout << PHYSFS_getLastError() << std::endl;		SDL_Quit( );		return -1;	}	PHYSFS_File arquivo_virtual;	arquivo_virtual = *PHYSFS_openRead("pak.zip");	PHYSFS_uint32 pakSize;	pakSize = PHYSFS_fileLength( &arquivo_virtual );	//std::cout <<  << std::endl;	if ( PHYSFS_setBuffer( &arquivo_virtual, pakSize) == 0 ){		std::cout << PHYSFS_getLastError() << std::endl;		SDL_Quit( );		return -1;	}	char* buffer;	PHYSFS_read( &arquivo_virtual, buffer, pakSize, 1 );	...........	if ( PHYSFS_close( &arquivo_virtual ) == 0 ){		std::cout << PHYSFS_getLastError() << std::endl;	}	if ( PHYSFS_removeFromSearchPath(".") == 0 ){		std::cout << PHYSFS_getLastError() << std::endl;	}	if ( PHYSFS_deinit( ) == 0 ){		std::cout << "Finalização do arquivo PAK falhou." << PHYSFS_getLastError() << std::endl;	}

Sorry, I could've helped you, have you decided to use my project, but as for PhysFS, I just pointed it out to you, I have no experience with it.
.zip files are NOT like .gz files
they can handle more than one file and directories
so you can pack your gamefiles in the .zip file, maybe best with apps like winrar, winzip, .. and unpack it with zlib in your program
don't know PhysFS,
but when you look at their site, there is a simple tutorial
http://icculus.org/physfs/physfstut.txt
k, now the things are working. Look at this:
//The function that take the file I desire:void *engine::abreArquivoCompactado( const char *arquivo ){		if ( PHYSFS_exists(arquivo) == 0 ){		std::cout << PHYSFS_getLastError() << std::endl;	}else{		PHYSFS_File arquivo_virtual;		arquivo_virtual = *PHYSFS_openRead(arquivo);		PHYSFS_uint32 pakSize;		pakSize = PHYSFS_fileLength( &arquivo_virtual );		if ( PHYSFS_setBuffer( &arquivo_virtual, pakSize) == 0 ){			std::cout << PHYSFS_getLastError() << std::endl;		}		char *buffer = new char[pakSize];		PHYSFS_read( &arquivo_virtual, buffer, pakSize, 1 );				if ( PHYSFS_close( &arquivo_virtual ) == 0 ){			std::cout << PHYSFS_getLastError() << std::endl;		}		return buffer;	}		return 0;}..................//main:	if ( PHYSFS_init( argv[0] ) == 0 ){ //Para inicializar o arquivo PAK		std::cout << "Inicialização do arquivo PAK falhou." << PHYSFS_getLastError() << std::endl;		SDL_Quit( );		return -1;	}	PHYSFS_addToSearchPath("pak.zip",1);............	layer1 = IMG_Load((char*)abreArquivoCompactado("pak/imagens/layout.png"));


So, since I want to load images with IMG_Load from SDL_Image, and the PHYSFS_read() throw the contents of the file in a buffer, how can I load the image with SDL_Image?
This is the last step to make things working! :)
try the following:

int size;char *buffer = abreArquivoCompactado("pak/imagens/layout.png", &size); // return also the size of the buffer !!!SDL_RWops *rw = SDL_RWFromMem(buffer, size);SDL_Surface *surface = IMG_Load_RW(rw, 0);delete [] buffer;




as a note:

IMG_Load_RW can load all supported image formats, except TGA
so when you need .tga's,
use instead IMG_LoadTyped_RW(SDL_RWops *src, int freesrc, char *type)
ie.
SDL_Surface *surface = IMG_LoadTyped_RW(rw, 0, "TGA");
or you can also use the seperate func's:
ie.
SDL_Surface *IMG_LoadJPG_RW(SDL_RWops *src)
SDL_Surface *IMG_LoadPNG_RW(SDL_RWops *src)
SDL_Surface *IMG_LoadTGA_RW(SDL_RWops *src)
....
Great! I tryed this and works very well :)
But, cannot load TGA? What's the difference? (I really need TGA and a automatic loading thing)

This topic is closed to new replies.

Advertisement