forked from planetbeing/partial-zip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.c
75 lines (55 loc) · 1.3 KB
/
test.c
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
#include <stdio.h>
#include "common.h"
#include "partial/partial.h"
void callback(ZipInfo* info, CDFile* file, size_t progress) {
int percentDone = progress * 100/file->compressedSize;
printf("Getting: %d%%\n", percentDone);
}
int main(int argc, char* argv[]) {
if (3 > argc) {
printf("partialzip <zipfile> <extract> [<outfile>]\r\n");
return -1;
}
int len = strlen(argv[0]);
char* extract = argv[2], fname[len+7];
char* outfile = argv[2];
if (argc >= 4)
{
outfile = argv[3];
}
if (strstr(argv[1], "http://") == NULL || strstr(argv[1], "file://") == NULL)
{
strcpy(fname, "file://");
}
strcpy(fname, argv[1]);
ZipInfo* info = PartialZipInit(fname);
if(!info)
{
printf("Cannot find /tmp/test.zip\n");
return 0;
}
PartialZipSetProgressCallback(info, callback);
CDFile* file = PartialZipFindFile(info, extract);
if(!file)
{
printf("Cannot find %s in %s\n", extract, fname);
return 0;
}
unsigned char* data = PartialZipGetFile(info, file);
int dataLen = file->size;
PartialZipRelease(info);
data = realloc(data, dataLen + 1);
data[dataLen] = '\0';
FILE* out;
out = fopen(outfile, "w");
if (out == NULL)
{
printf("Failed to open file");
exit(-1);
}
int done = 0;
done = fwrite(data, sizeof(char), dataLen, out);
fclose(out);
free(data);
return 0;
}