2 Copyright (C) 2005 Parallel Realities
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 int dirs = 0, files = 0;
29 FileData *fileData = NULL;
37 void countFiles(const char *dirName)
41 dirp = opendir(dirName);
44 while ((dfile = readdir(dirp)))
46 if (dfile->d_name[0] == '.')
51 snprintf(filename, sizeof filename, "%s/%s", dirName, dfile->d_name);
53 if (strlen(filename) > PAK_MAX_FILENAME - 1)
55 printf("\nERROR - '%s' exceeds maximum defined file length of %d\n", filename, PAK_MAX_FILENAME);
59 dirp2 = opendir(filename);
74 fileData = new FileData[totalFiles];
77 void recurseDirectory(const char *dirName)
88 dirp = opendir(dirName);
92 printf("%s: Directory does not exist or is not accessable\n", dirName);
99 while ((dfile = readdir(dirp)))
101 if (dfile->d_name[0] == '.')
106 snprintf(filename, sizeof filename, "%s/%s", dirName, dfile->d_name);
108 dirp2 = opendir(filename);
113 recurseDirectory(filename);
117 infile = fopen(filename, "rb");
120 printf("Couldn't open %s for reading!\n", filename);
126 fseek(infile, SEEK_SET, SEEK_END);
128 filesize = ftell(infile);
133 buffer = new unsigned char[filesize];
136 output = new unsigned char[(int)(filesize * 1.01) + 12];
138 fp = gzopen(filename, "rb");
142 printf("Couldn't open %s for reading!\n", filename);
149 fSize = gzread(fp, buffer, filesize);
152 cSize = (uLongf)((fSize * 1.01) + 12);
153 compress2(output, &cSize, buffer, fSize, 9);
155 fileData[files].set(filename, fSize, cSize, ftell(pak));
157 fwrite(output, 1, cSize, pak);
162 percentage /= totalFiles;
165 printf("\b\b\b\b%3.0f%%", percentage);
176 int main(int argc, char *argv[])
180 printf("Usage : pak <directory names> <outputname>\n");
181 printf("Example : pak data music gfx sound data.pak\n");
185 pak = fopen(argv[argc - 1], "wb");
187 for (int i = 1 ; i < (argc - 1) ; i++)
192 printf("Paking...000%%");
200 for (int i = 1 ; i < (argc - 1) ; i++)
202 recurseDirectory(argv[i]);
205 unsigned int pos = ftell(pak);
207 for (int i = 0 ; i < files ; i++)
209 if (fileData[i].fSize == 0)
214 fwrite(&fileData[i], sizeof(FileData), 1, pak);
217 unsigned int numberOfFiles = totalFiles;
219 fwrite(&pos, sizeof(unsigned int), 1, pak);
220 fwrite(&numberOfFiles, sizeof(unsigned int), 1, pak);
224 printf("\nPak: All Done. Added %d files\n", numberOfFiles);