Well, I say finished, I mean I basically finished it, you can't delete any files, but you can pack and extract them.
#include <cstdlib>
#include <iostream>
#include <windows.h>
using namespace std;
void MoveResourceBlock(FILE* resourcefile, long int initialoffset, long int newoffset, long int blocksize)
{
fseek(resourcefile,initialoffset,SEEK_SET);
char* buffer = (char*)malloc(blocksize);
fread(buffer,1,blocksize,resourcefile);
fseek(resourcefile,newoffset,SEEK_SET);
fwrite(buffer,1,blocksize,resourcefile);
free(buffer);
}
FILE* CreateResourceFile(const char* resourcename)
{
long int temp = -1;
FILE* file = fopen(resourcename,"w+b");
fwrite(&temp,4,1,file);
return file;
}
FILE* OpenResourceFile(const char* resourcename)
{
return fopen(resourcename,"r+b");
}
long int GetFileCount(FILE* resourcefile)
{
long int temp;
long int points = ftell(resourcefile);
fseek(resourcefile,0,SEEK_SET);
fread(&temp,4,1,resourcefile);
fseek(resourcefile,points,SEEK_SET);
return temp;
}
long int GetItemOffset(FILE* resourcefile, int idnumber)
{
long int temp;
long int points = ftell(resourcefile);
fseek(resourcefile,4+(4*idnumber),SEEK_SET);
fread(&temp,4,1,resourcefile);
fseek(resourcefile,points,SEEK_SET);
return temp;
}
void SetItemOffset(FILE* resourcefile, int idnumber, int newoffset)
{
long int temp = newoffset;
long int points = ftell(resourcefile);
fseek(resourcefile,4+(4*idnumber),SEEK_SET);
fwrite(&temp,4,1,resourcefile);
fseek(resourcefile,points,SEEK_SET);
}
long int GetFileSize(FILE* resourcefile)
{
long int temp;
long int points = ftell(resourcefile);
fseek(resourcefile,0,SEEK_END);
temp = ftell(resourcefile);
fseek(resourcefile,points,SEEK_SET);
return temp;
}
long int GetContentsOffset(FILE* resourcefile, int idnumber)
{
long filenamesize;
long int points = ftell(resourcefile);
fseek(resourcefile,GetItemOffset(resourcefile,idnumber)+4,SEEK_SET);
fread(&filenamesize,4,1,resourcefile);
fseek(resourcefile,filenamesize,SEEK_CUR);
filenamesize = ftell(resourcefile);
fseek(resourcefile,points,SEEK_SET);
return filenamesize;
}
long int GetContentsSize(FILE* resourcefile, int idnumber)
{
long contentssize;
long int points = ftell(resourcefile);
fseek(resourcefile,GetItemOffset(resourcefile,idnumber),SEEK_SET);
fread(&contentssize,4,1,resourcefile);
fseek(resourcefile,points,SEEK_SET);
return contentssize;
}
void GetContents(FILE* resourcefile, int idnumber, char* buffer)
{
long int points = ftell(resourcefile);
fseek(resourcefile,GetContentsOffset(resourcefile,idnumber),SEEK_SET);
fread(buffer,1,GetContentsSize(resourcefile,idnumber),resourcefile);
fseek(resourcefile,points,SEEK_SET);
}
long int GetFilenameSize(FILE* resourcefile, int idnumber)
{
long int points = ftell(resourcefile);
long int filenamesize;
fseek(resourcefile,GetItemOffset(resourcefile,idnumber)+4,SEEK_SET);
fread(&filenamesize,4,1,resourcefile);
fseek(resourcefile,points,SEEK_SET);
return filenamesize;
}
void GetFileName(FILE* resourcefile, int idnumber, char* buffer)
{
long int points = ftell(resourcefile);
fseek(resourcefile,GetItemOffset(resourcefile,idnumber)+8,SEEK_SET);
fread(buffer,1,GetFilenameSize(resourcefile,idnumber),resourcefile);
fseek(resourcefile,points,SEEK_SET);
}
bool SetFileEnd(const char* filename, long position)
{
HANDLE hFile = CreateFile(filename,FILE_ALL_ACCESS,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if (hFile == NULL)
return false;
SetFilePointer(hFile,position,NULL,FILE_BEGIN);
SetEndOfFile(hFile);
CloseHandle(hFile);
return true;
}
void PackFile(FILE* resourcefile, const char* filename)
{
int count;
FILE* file = fopen(filename,"rb");
long int filenamesize = strlen(filename);
long int resourcesize = GetFileSize(resourcefile);
long int filesize = GetFileSize(file);
long int filecount = GetFileCount(resourcefile)+1;
char* buffer = (char*)malloc(filesize);
fseek(file,0,SEEK_SET);
fread(buffer,1,filesize,file);
// Begin Packing
fseek(resourcefile,0,SEEK_SET);
fwrite(&filecount,4,1,resourcefile);
MoveResourceBlock(resourcefile,4+(filecount*4),8+(filecount*4),(resourcesize-(4+(filecount*4))));
resourcesize += 4;
fseek(resourcefile,4+(filecount*4),SEEK_SET);
fwrite(&resourcesize,4,1,resourcefile);
for (count = 0; count < filecount; count++){
long temp = GetItemOffset(resourcefile,count)+4;
fseek(resourcefile,4+(4*count),SEEK_SET);
fwrite(&temp,4,1,resourcefile);
}
fseek(resourcefile,resourcesize,SEEK_SET);
fwrite(&filesize,4,1,resourcefile);
fseek(resourcefile,resourcesize+4,SEEK_SET);
fwrite(&filenamesize,4,1,resourcefile);
fseek(resourcefile,resourcesize+8,SEEK_SET);
fwrite(filename,1,filenamesize,resourcefile);
fseek(resourcefile,resourcesize+8+filenamesize,SEEK_SET);
fwrite(buffer,1,filesize,resourcefile);
// End Packing
free(buffer);
}
void UnpackFile(FILE* resourcefile, const char* filename, int idnumber)
{
char* buffer;
int count;
buffer = (char*)malloc(GetContentsSize(resourcefile,idnumber));
GetContents(resourcefile,idnumber,buffer);
FILE* tempfile = fopen(filename,"w+b");
fwrite(buffer,1,GetContentsSize(resourcefile,idnumber),tempfile);
free(buffer);
fclose(tempfile);
}
int main(int argc, char *argv[])
{
char line[MAX_PATH];
if (argc > 1){
if (strcmp(argv[1],"-c") == 0){
if (argc == 3){
FILE* file = CreateResourceFile(argv[2]);
cout << "Resource File " << argv[2] << " created." << endl;
fclose(file);
}
else
cout << "Usage: " << argv[0] << " -c <filename>" << endl;
}
else if (strcmp(argv[1],"-a") == 0){
if (argc == 4){
cout << "Packing File " << argv[3] << " into " << argv[2] << "... ";
FILE* file = OpenResourceFile(argv[2]);
PackFile(file,argv[3]);
cout << "Done!" << endl;
fclose(file);
}
else
cout << "Usage: " << argv[0] << " -a <resource> <filename>" << endl;
}
else if (strcmp(argv[1],"-u") == 0){
if (argc == 5){
FILE* file = OpenResourceFile(argv[2]);
UnpackFile(file,argv[4],atoi(argv[3]));
fclose(file);
}
else
cout << "Usage: " << argv[0] << " -u <resource> <idnumber> <filename>" << endl << endl;
}
else if (strcmp(argv[1],"-h") == 0){
cout << "Command List for Packing Utility - By Eriond" << endl << endl;
cout << "-c <resource> -- Creates a Resource with the Specified Name" << endl;
cout << "-a <resource> <filename> -- Adds the specified file to the resource." << endl;
cout << "-u <resource> <idnumber> <filename> -- Extracts the file at the ID number specified to the filename specified." << endl;
}
}
else
cout << "More Arguments Needed. For help, type " << argv[0] << " -h" << endl;
return EXIT_SUCCESS;
}