summaryrefslogtreecommitdiff
path: root/src/base/archive.c
diff options
context:
space:
mode:
authorDušan Ďurech <dusan.d@centrum.cz>2010-01-25 19:26:08 +0100
committerDušan Ďurech <dusan.d@centrum.cz>2010-01-25 19:26:08 +0100
commit0a880fe38a9060cb01b05de21b97c71fab5b14b0 (patch)
tree26f5e31d5e5c7133eaf8cdfcbc62c6b6c1572e00 /src/base/archive.c
parentcdc100ff4a0ceecae2ee2cecaf9df6b82b8636e4 (diff)
support lib physfs for extract data from archive files
Diffstat (limited to 'src/base/archive.c')
-rw-r--r--src/base/archive.c72
1 files changed, 72 insertions, 0 deletions
diff --git a/src/base/archive.c b/src/base/archive.c
index cdd2ae1..a364df6 100644
--- a/src/base/archive.c
+++ b/src/base/archive.c
@@ -2,7 +2,17 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
+
+#define SUPPORT_LIBZIP
+/* #define SUPPORT_LIBPHYSFS */
+
+#ifdef SUPPORT_LIBPHYSFS
+#include <physfs.h>
+#endif
+
+#ifdef SUPPORT_LIBZIP
#include <zip.h>
+#endif
#include "main.h"
#include "archive.h"
@@ -10,6 +20,7 @@
#define ARCHIVE_BUFFER_SIZE 4096
#define STR_TMP_FILE_NAME 4096
+#ifdef SUPPORT_LIBZIP
char *archive_extract_file(char *archive, char *filename)
{
char name[STR_TMP_FILE_NAME];
@@ -69,6 +80,67 @@ char *archive_extract_file(char *archive, char *filename)
return strdup(name);
}
+#endif
+
+#ifdef SUPPORT_LIBPHYSFS
+char *archive_extract_file(char *archive, char *filename)
+{
+ char name[STR_TMP_FILE_NAME];
+ char buffer[ARCHIVE_BUFFER_SIZE];
+
+ PHYSFS_file *file_archive;
+ FILE *file;
+ int count;
+
+ char *tmp;
+
+#ifndef __WIN32__
+ tmp = getenv("TMPDIR") == NULL ? "/tmp" : getenv("TMPDIR");
+#else
+ tmp = getenv("TEMP");
+#endif
+ sprintf(name, "%s%stuxanci.%d-%s", tmp, PATH_SEPARATOR ,getpid(), filename);
+
+ if( ! PHYSFS_init("hahaha") )
+ {
+ error("Unable to init physfs");
+ return NULL;
+ }
+
+ if (! PHYSFS_addToSearchPath(archive, 1)) {
+ error("Unable to open archive [%s]", archive);
+ PHYSFS_deinit();
+ return NULL;
+ }
+
+ file_archive = PHYSFS_openRead(filename);
+
+ if (file_archive == NULL) {
+ error("Unable to extract the file [%s] from archive [%s]", filename, archive);
+ PHYSFS_deinit();
+ return NULL;
+ }
+
+ if ((file = fopen(name, "wb")) == NULL) {
+ error("Unable to create temporary file [%s]", name);
+ PHYSFS_close(file_archive);
+ PHYSFS_deinit();
+ return NULL;
+ }
+
+ while ((count = PHYSFS_read(file_archive, buffer, 1, ARCHIVE_BUFFER_SIZE)) > 0)
+ {
+ fwrite(buffer, count, 1, file);
+ }
+
+ PHYSFS_close(file_archive);
+ fclose(file);
+
+ PHYSFS_deinit();
+
+ return strdup(name);
+}
+#endif
void archive_delete_file(char *s)
{