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 if (fwrite(output, 1, cSize, pak) != cSize)
159 fprintf(stderr, "Error writing to pakfile: %s\n", strerror(errno));
167 percentage /= totalFiles;
170 printf("\b\b\b\b%3.0f%%", percentage);
181 int main(int argc, char *argv[])
185 printf("Usage : pak <directory names> <outputname>\n");
186 printf("Example : pak data music gfx sound data.pak\n");
190 pak = fopen(argv[argc - 1], "wb");
193 fprintf(stderr, "Error opening %s: %s\n", argv[argc - 1], strerror(errno));
197 for (int i = 1 ; i < (argc - 1) ; i++)
202 printf("Paking...000%%");
210 for (int i = 1 ; i < (argc - 1) ; i++)
212 recurseDirectory(argv[i]);
215 unsigned int pos = ftell(pak);
217 for (int i = 0 ; i < files ; i++)
219 if (fileData[i].fSize == 0)
224 if (fwrite(&fileData[i], sizeof(FileData), 1, pak) != 1)
226 fprintf(stderr, "Error writing to %s: %s\n", argv[argc - 1], strerror(errno));
232 unsigned int numberOfFiles = totalFiles;
234 if (fwrite(&pos, sizeof(unsigned int), 1, pak) != 1)
236 fprintf(stderr, "Error writing to %s: %s\n", argv[argc - 1], strerror(errno));
240 if (fwrite(&numberOfFiles, sizeof(unsigned int), 1, pak) != 1)
242 fprintf(stderr, "Error writing to %s: %s\n", argv[argc - 1], strerror(errno));
249 printf("\nPak: All Done. Added %d files\n", numberOfFiles);