# Custom Archive File Formats

This topic is 5404 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

This is just a start, i'm trying to make a prgram to store files in groups. I intend to expand this system sometime in the future, but first i want to get this part working. Here's the start: Creating the archive:
		int i, j, l;
FILE* file;
if((file = fopen(fname, "wb")) == NULL)
return false;
fwrite(&count, sizeof(int), 1, file);
//first, compile a list of files with lengths, and calculate the offsets by the files position in the archive file list
CArchiveEntry* ents = new CArchiveEntry[count];		//create a list of archive entrys for the files
int offset = (sizeof(CArchiveEntry) * count) + 1;			//create the base offset for the archive
FILE** files = (FILE**)malloc(sizeof(FILE*) * count);
for(i = 0; i < count; i++)
{
//find the last \ in the filename, and get the last bit of the file name
j = FindLastChar(list, '\\');
j++;	//exclude the stupid \ from the output file
if(strlen(list) - j > 32)
{
fclose(file);
for(j = 0; j < i; j++)
fclose(files);
MessageBox(NULL, "File name exceeds allowed length of 32 charachters.", "Error", MB_OK | MB_ICONERROR);
return false;
}
strcpy(ents.fname, (char*)&(list[j]));
files = fopen(list, "rb");
ents.size = _filelength(_fileno(files));
ents.offset = offset;
offset += ents.size + 1;
}
//ok, now that the offset table has been constructed, it's time to begin writing the files.  note that they are all open
fwrite(ents, sizeof(CArchiveEntry), count, file);
char buffer[256];
for(i = 0; i < count; i++)
{
for(j = ents.size; j > 0; j -= l)		//when j = 0, the file should be fully copied
{
l = fread(&buffer, sizeof(char), 256, files);
fwrite(&buffer, sizeof(char), l, file);
}
fclose(files);
}
delete[] ents;
free(files);
return true;

	file = fopen(fname, "rb");		//open the file for reading in binary mode
if(!file)
return false;
//ok then, assume we have a file we can use, first, check for the id code
if(numEnts <= 0)
{
//error, this file is not the correct format
fclose(file);
file = NULL;
return false;
}
//well, we're still not guaranteed to be right, but it's close enough
ents = new CArchiveEntry[numEnts];

	int i;
for(i = 0; i < numEnts; i++)
if(strcmp(ents.fname, fname) == 0)
break;
if(i == numEnts)
return -1;
if(size < ents.size)		//do not load less then the full file
return -1;
if(fseek(file, ents.offset, SEEK_SET))		//go to the files location in the archive
return -1;		//could not find file within archive

does anyone see any kind of problem with this code? for some reason, when i run a program that uses it and compare the file as loaded from the archive to the file as loaded direct from the original, the data in the files are not the same. If anyone has any ideas whats wrong, i'd be glad to hear them, as i can't tell whats wrong. thanks personwholives [Edited by - personwholives on October 5, 2004 11:11:29 PM]

• 13
• 18
• 29
• 11
• 20
×

## Important Information

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!