79 unsigned int cmpsize = 0;
80 unsigned int cursize = 0;
81 unsigned int curseek = 0;
87 memset(resource, 0,
sizeof(*resource));
90 resource->
data = NULL;
92 sh_new_strdup(resource->
entries);
98 f = fopen(path,
"rb");
102 gf_resource_destroy(resource);
106 fseek(f, 0, SEEK_END);
108 fseek(f, 0, SEEK_SET);
110 stream.zalloc = Z_NULL;
111 stream.zfree = Z_NULL;
112 stream.opaque = Z_NULL;
114 stream.next_in = Z_NULL;
116 if(inflateInit(&stream) != Z_OK) {
118 gf_resource_destroy(resource);
125 unsigned char in[CHUNK];
126 unsigned char out[CHUNK];
127 stream.avail_in = fread(in, 1,
sizeof(in), f);
128 if(stream.avail_in == 0)
break;
133 stream.avail_out =
sizeof(out);
134 stream.next_out = out;
135 ret = inflate(&stream, Z_NO_FLUSH);
136 if(ret == Z_NEED_DICT || ret == Z_DATA_ERROR || ret == Z_MEM_ERROR) {
139 gf_resource_destroy(resource);
142 have =
sizeof(out) - stream.avail_out;
143 memcpy(gf_resource_allocate(resource, have), out, have);
145 }
while(stream.avail_out == 0);
146 }
while(ret != Z_STREAM_END);
151 gf_log_function(engine,
"Compression rate: %.2f%%", (
double)cursize / cmpsize * 100);
157 if(th->filename[0] == 0)
break;
161 if(memcmp(th->ustar,
"ustar", 6) != 0) {
163 gf_resource_destroy(resource);
168 sz = tar_getsize(&th->size[0]);
172 entry.
key = th->filename;
175 shputs(resource->
entries, entry);
177 if(sz != 0) curseek += ((sz / 512) + 1) * 512;
242void gf_resource_write(
gf_resource_t* resource,
const char* path,
int progress) {
243 unsigned char out[CHUNK];
247 stream.zalloc = Z_NULL;
248 stream.zfree = Z_NULL;
249 stream.opaque = Z_NULL;
251 stream.next_in = Z_NULL;
253 if(deflateInit(&stream, Z_DEFAULT_COMPRESSION) != Z_OK) {
257 f = fopen(path,
"wb");
259 unsigned int totalog = 0;
260 unsigned int totalcmp = 0;
262 for(i = 0; i < shlen(resource->
entries); i++) {
264 in = malloc(512 + sz);
266 stream.avail_in = 512 + sz;
278 stream.avail_out = CHUNK;
279 stream.next_out = out;
280 deflate(&stream, Z_NO_FLUSH);
281 have = CHUNK - stream.avail_out;
282 fwrite(out, have, 1, f);
288 }
while(stream.avail_out == 0);
299 stream.avail_in = 512;
305 stream.avail_out = CHUNK;
306 stream.next_out = out;
307 deflate(&stream, Z_FINISH);
308 have = CHUNK - stream.avail_out;
309 fwrite(out, have, 1, f);
311 }
while(stream.avail_out == 0);
317 printf(
"Compression rate: %.2f%%\n", (
double)totalog / totalcmp * 100);