forked from MAYANK25402/Hactober-2023-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzip file reader.c
90 lines (71 loc) · 2.15 KB
/
zip file reader.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// C program to read and print
// all files in a zip file
// uses library libzip
#include <stdlib.h>
#include <zip.h>
// this is run from the command line with the zip file
// passed in example usage: ./program zipfile.zip
int main(int argc, char* argv[])
{
// if more or less than 2
// command line arguments,
// program ends
if (argc > 2 || argc < 2)
return -1;
// if the file provided can't
// be opened/read, program
// ends
if (!fopen(argv[1], "r"))
return -2;
// stores error codes for libzip functions
int errorp = 0;
// initializes a pointer to a zip archive
zip_t* arch = NULL;
// sets that pointer to the
// zip file from argv[1]
arch = zip_open(argv[1], 0, &errorp);
// the zip_stat structure
// contains information such as
// file name, size, comp size
struct zip_stat* finfo = NULL;
// must be allocated enough space
// (not exact space here)
finfo = calloc(256, sizeof(int));
// "initializes" the structure
// according to documentation
zip_stat_init(finfo);
// initialize file descriptor for
// zip files inside archive
zip_file_t* fd = NULL;
// initialize string pointer for
// reading from fd
char* txt = NULL;
// count = index of file archive 0 =
// first file
int count = 0;
// we open the file at the count'th index inside the
// archive we loop and print every file and its
// contents, stopping when zip_stat_index did not return
// 0, which means the count index is more than # of
// files
while ((zip_stat_index(arch, count, 0, finfo)) == 0) {
// allocate room for the entire file contents
txt = calloc(finfo->size + 1, sizeof(char));
fd = zip_fopen_index(
arch, count, 0); // opens file at count index
// reads from fd finfo->size
// bytes into txt buffer
zip_fread(fd, txt, finfo->size);
printf("file #%i: %s\n\n", count + 1,
finfo->name); // prints filename
printf("%s\n\n",
txt); // prints entire file contents
// frees allocated buffer, will
// reallocate on next iteration of loop
free(txt);
// increase index by 1 and the loop will
// stop when files are not found
count++;
}
return 0;
}