forked from GotoHack/partial-zip
/
partialzip.c
65 lines (48 loc) · 1.23 KB
/
partialzip.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
#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("\rDownloading: %d%%", percentDone);
}
int main(int argc, char* argv[]) {
if (argc < 4 || argc > 5)
{
printf("usage: %s <ZipURL> <RemotePath> <LocalPath> [numBytes]\n", argv[0]);
return 1;
}
char* url = argv[1];
char* remotefile = argv[2];
char* localpath = argv[3];
char* sizeToDownload = argv[4];
printf("Initializing download for %s...\n", remotefile);
ZipInfo* info = PartialZipInit(url);
if(!info)
{
printf("Cannot find %s\n", url);
return 0;
}
PartialZipSetProgressCallback(info, callback);
CDFile* file = PartialZipFindFile(info, remotefile);
if(!file)
{
printf("Cannot find %s in %s\n", remotefile, url);
return 0;
}
unsigned char* data = PartialZipGetFile(info, file, sizeToDownload);
int dataLen = file->size;
PartialZipRelease(info);
printf("\n");
data = realloc(data, dataLen + 1);
data[dataLen] = '\0';
FILE* out = fopen(localpath, "wb");
if (out == NULL)
{
printf("Failed to open %s", localpath);
return 1;
}
fwrite(data, sizeof(char), dataLen, out);
fclose(out);
free(data);
return 0;
}