zip format support

This commit is contained in:
p.kosyh 2010-06-18 07:13:06 +00:00
parent 0ceaaf4b3d
commit 16b7ad199c

View file

@ -121,7 +121,7 @@ char *newdir;
buffer = (char *)malloc(len + 1); buffer = (char *)malloc(len + 1);
if (buffer == NULL) { if (buffer == NULL) {
printf("Error allocating memory\n"); fprintf(stderr, "Error allocating memory\n");
return UNZ_INTERNALERROR; return UNZ_INTERNALERROR;
} }
strcpy(buffer, newdir); strcpy(buffer, newdir);
@ -143,7 +143,7 @@ char *newdir;
hold = *p; hold = *p;
*p = 0; *p = 0;
if ((mymkdir(buffer) == -1) && (errno == ENOENT)) { if ((mymkdir(buffer) == -1) && (errno == ENOENT)) {
printf("couldn't create directory %s\n", buffer); fprintf(stderr, "couldn't create directory %s\n", buffer);
free(buffer); free(buffer);
return 0; return 0;
} }
@ -173,14 +173,14 @@ const char *password;
sizeof(filename_inzip), NULL, 0, NULL, 0); sizeof(filename_inzip), NULL, 0, NULL, 0);
if (err != UNZ_OK) { if (err != UNZ_OK) {
printf("error %d with zipfile in unzGetCurrentFileInfo\n", err); fprintf(stderr, "error %d with zipfile in unzGetCurrentFileInfo\n", err);
return err; return err;
} }
size_buf = WRITEBUFFERSIZE; size_buf = WRITEBUFFERSIZE;
buf = (void *)malloc(size_buf); buf = (void *)malloc(size_buf);
if (buf == NULL) { if (buf == NULL) {
printf("Error allocating memory\n"); fprintf(stderr, "Error allocating memory\n");
return UNZ_INTERNALERROR; return UNZ_INTERNALERROR;
} }
@ -198,28 +198,24 @@ const char *password;
fprintf(stderr, "Too many dirs in zip...\n"); fprintf(stderr, "Too many dirs in zip...\n");
goto out; goto out;
} }
printf("creating directory: %s\n", filename_inzip); fprintf(stderr, "creating directory: %s\n", filename_inzip);
mymkdir(filename_inzip); mymkdir(filename_inzip);
if (!*zip_game_dirname) if (!*zip_game_dirname)
strcpy(zip_game_dirname, filename_inzip); strcpy(zip_game_dirname, filename_inzip);
} else { } else {
const char *write_filename; const char *write_filename;
int skip = 0; int skip = 0;
if (!*zip_game_dirname) {
err = -1;
fprintf(stderr, "No dir in zip...\n");
goto out;
}
write_filename = filename_inzip; write_filename = filename_inzip;
err = unzOpenCurrentFilePassword(uf, password); err = unzOpenCurrentFilePassword(uf, password);
if (err != UNZ_OK) { if (err != UNZ_OK) {
printf fprintf
("error %d with zipfile in unzOpenCurrentFilePassword\n", (stderr, "error %d with zipfile in unzOpenCurrentFilePassword\n",
err); err);
goto out;
} }
if ((skip == 0) && (err == UNZ_OK)) { if (skip == 0) {
fout = fopen64(write_filename, "wb"); fout = fopen64(write_filename, "wb");
/* some zipfile don't contain directory alone before file */ /* some zipfile don't contain directory alone before file */
@ -228,30 +224,41 @@ const char *password;
char c = *(filename_withoutpath - 1); char c = *(filename_withoutpath - 1);
*(filename_withoutpath - 1) = '\0'; *(filename_withoutpath - 1) = '\0';
makedir(write_filename); makedir(write_filename);
if (!*zip_game_dirname)
strcpy(zip_game_dirname, filename_inzip);
*(filename_withoutpath - 1) = c; *(filename_withoutpath - 1) = c;
fout = fopen64(write_filename, "wb"); fout = fopen64(write_filename, "wb");
} }
if (fout == NULL) { if (fout == NULL) {
printf("error opening %s\n", write_filename); fprintf(stderr, "error opening %s\n", write_filename);
} }
} }
if (!*zip_game_dirname) {
err = -1;
fprintf(stderr, "No dir in zip...\n");
if (fout)
fclose(fout);
unzCloseCurrentFile(uf);
goto out;
}
if (fout != NULL) { if (fout != NULL) {
printf(" extracting: %s\n", write_filename); fprintf(stderr, " extracting: %s\n", write_filename);
do { do {
err = unzReadCurrentFile(uf, buf, size_buf); err = unzReadCurrentFile(uf, buf, size_buf);
if (err < 0) { if (err < 0) {
printf fprintf
("error %d with zipfile in unzReadCurrentFile\n", (stderr, "error %d with zipfile in unzReadCurrentFile\n",
err); err);
break; break;
} }
if (err > 0) if (err > 0)
if (fwrite(buf, err, 1, fout) != 1) { if (fwrite(buf, err, 1, fout) != 1) {
printf fprintf
("error in writing extracted file\n"); (stderr, "error in writing extracted file\n");
err = UNZ_ERRNO; err = UNZ_ERRNO;
break; break;
} }
@ -269,8 +276,8 @@ const char *password;
if (err == UNZ_OK) { if (err == UNZ_OK) {
err = unzCloseCurrentFile(uf); err = unzCloseCurrentFile(uf);
if (err != UNZ_OK) { if (err != UNZ_OK) {
printf fprintf
("error %d with zipfile in unzCloseCurrentFile\n", (stderr, "error %d with zipfile in unzCloseCurrentFile\n",
err); err);
} }
} else } else
@ -298,7 +305,7 @@ const char *password;
int err; int err;
err = unzGetGlobalInfo64(uf, &gi); err = unzGetGlobalInfo64(uf, &gi);
if (err != UNZ_OK) if (err != UNZ_OK)
printf("error %d with zipfile in unzGetGlobalInfo \n", err); fprintf(stderr, "error %d with zipfile in unzGetGlobalInfo \n", err);
for (i = 0; i < gi.number_entry; i++) { for (i = 0; i < gi.number_entry; i++) {
if (do_extract_currentfile(uf, password) != UNZ_OK) if (do_extract_currentfile(uf, password) != UNZ_OK)
@ -307,8 +314,8 @@ const char *password;
if ((i + 1) < gi.number_entry) { if ((i + 1) < gi.number_entry) {
err = unzGoToNextFile(uf); err = unzGoToNextFile(uf);
if (err != UNZ_OK) { if (err != UNZ_OK) {
printf fprintf
("error %d with zipfile in unzGoToNextFile\n", (stderr, "error %d with zipfile in unzGoToNextFile\n",
err); err);
return -1; return -1;
} }
@ -364,7 +371,7 @@ int unpack(const char *zipfilename, const char *dirname)
#endif #endif
{ {
ret_value = -1; ret_value = -1;
printf("Error changing dir to %s, aborting\n", dirname); fprintf(stderr, "Error changing dir to %s, aborting\n", dirname);
goto out; goto out;
} }
ret_value = do_extract(uf, NULL); ret_value = do_extract(uf, NULL);