summaryrefslogtreecommitdiff
path: root/src/base/archive.c
blob: 2c188c334808b894aea026e7104d32e3a0e12f97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#ifdef SUPPORT_LIBPHYSFS
#include <physfs.h>
#endif /* SUPPORT_LIBPHYSFS */

#ifdef SUPPORT_LIBZIP
#include <zip.h>
#endif /* SUPPORT_LIBZIP */

#include "main.h"
#include "archive.h"

#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];
	char buf[ARCHIVE_BUFFER_SIZE];

	struct zip *zipArchive;
	struct zip_file *zipFile;

	FILE *file;
	int len;
	int err;
	char *tmp;

#ifndef __WIN32__
	tmp = getenv("TMPDIR") == NULL ? "/tmp" : getenv("TMPDIR");
#else /* __WIN32__ */
	tmp = getenv("TEMP");
#endif /* __WIN32__ */
	sprintf(name, "%s%stuxanci.%d-%s", tmp, PATH_SEPARATOR, getpid(), filename);

	/*printf("ARCHIVE %s %s %s\n", archive, filename, name);*/

	zipArchive = zip_open(archive, 0, &err);

	if (zipArchive == NULL) {
		error("Unable to open archive [%s]", archive);
		return NULL;
	}

	zipFile = zip_fopen(zipArchive, filename, ZIP_FL_UNCHANGED);

	if (zipFile == NULL) {
		error("Unable to extract the file [%s] from archive [%s]", filename, archive);
		zip_close(zipArchive);
		return NULL;
	}

	if ((file = fopen(name, "wb")) == NULL) {
		error("Unable to create temporary file [%s]", name);
		zip_fclose(zipFile);
		zip_close(zipArchive);
		return NULL;
	}

	do {
		len = zip_fread(zipFile, buf, ARCHIVE_BUFFER_SIZE);

		if (len > 0) {
			fwrite(buf, len, 1, file);
		}
	} while (len > 0);

	fclose(file);
	zip_fclose(zipFile);
	zip_close(zipArchive);

	return strdup(name);
}
#endif /* SUPPORT_LIBZIP */

#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 /* __WIN32__ */
	tmp = getenv("TEMP");
#endif /* __WIN32__ */
	sprintf(name, "%s%stuxanci.%d-%s", tmp, PATH_SEPARATOR, getpid(), filename);

	if (!(PHYSFS_init(get_program_name()))) {
		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 /* SUPPORT_LIBPHYSFS */

void archive_delete_file(char *s)
{
	unlink(s);
	free(s);
}