From d70ee7b8a581f378d888731269b30a9d2ef43614 Mon Sep 17 00:00:00 2001 From: Stefan Petersen Date: Wed, 12 Aug 2009 23:19:03 +0200 Subject: [PATCH] Added function gerb_filenames_in_zip() to find files in zip. This functions searches a zip file and returns the name of the files in the zip archive in a GSList. --- src/gerb_file.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/gerb_file.h | 8 +++++++ 2 files changed, 76 insertions(+) diff --git a/src/gerb_file.c b/src/gerb_file.c index 0910616..d9f74a1 100644 --- a/src/gerb_file.c +++ b/src/gerb_file.c @@ -45,9 +45,12 @@ #include #include "common.h" +#include "unzip/unzip.h" #include "gerbv.h" #include "gerb_file.h" +#define GERB_FILE_ZFILENAME_LEN_MAX 255 + /* DEBUG printing. #define DEBUG 1 in config.h to use this fcn. */ #define dprintf if(DEBUG) printf @@ -369,3 +372,68 @@ gerb_find_file(char const * filename, char **paths) return complete_path; } /* gerb_find_file */ + + + + +/* + * Extract the file names in a zip archive and return them in a GSList. + */ +int +gerb_filenames_in_zip(char *zipname, GSList **filenames) +{ + unzFile zfd; + int status; + int idx = 0; + gchar *zfilename; + GSList *fn = NULL; + + /* Now try to open the zip file given and determine which files are in it*/ + zfd = unzOpen(zipname); + if (zfd == NULL) { + GERB_FATAL_ERROR("unzOpen failed on %s\n", zipname); + return -1; + } + + if (unzGoToFirstFile(zfd) != UNZ_OK) { + GERB_FATAL_ERROR("Failed to go to first file in %s\n", zipname); + unzClose(zfd); + return -1; + } + + /* Insert all filenames in zip to a GSList */ + zfilename = g_malloc(GERB_FILE_ZFILENAME_LEN_MAX); + while (1) { + memset(zfilename, 0, GERB_FILE_ZFILENAME_LEN_MAX); + unzGetCurrentFileInfo(zfd, + NULL, + zfilename, + 255, + NULL, + 0, + NULL, + 0); + fn = g_slist_append(fn, (gpointer)g_strdup(zfilename)); + idx++; + dprintf("%s(): Found file \"%s\" in the zip file\n", + __FUNCTION__, zfilename); + + status = unzGoToNextFile(zfd); + if (status == UNZ_END_OF_LIST_OF_FILE) { + break; + } + if (status != UNZ_OK) { + GERB_FATAL_ERROR("Failed to go to next file.\n"); + unzClose(zfd); + g_free(zfilename); + return -1; + } + } + g_free(zfilename); + + *filenames = fn; + + unzClose(zfd); + + return idx; +} /* gerb_filenames_in_zip */ diff --git a/src/gerb_file.h b/src/gerb_file.h index bab92ed..d6fb217 100644 --- a/src/gerb_file.h +++ b/src/gerb_file.h @@ -30,6 +30,7 @@ #define GERB_FILE_H #include +#include typedef struct file { FILE *fd; /* File descriptor */ @@ -61,4 +62,11 @@ const char path_separator; */ char *gerb_find_file(char const * filename, char **paths); +/* + * Extract the file names in a zip archive and return them in a GSList. + * Returns how many files was found in the zip archive. Returns -1 on + * error. + */ +int gerb_filenames_in_zip(char *zipname, GSList **filenames); + #endif /* GERB_FILE_H */ -- 2.11.4.GIT