forked from krystalgamer/RealZip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
301 lines (229 loc) · 5.22 KB
/
main.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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "zip.h"
#include <inttypes.h>
#include <string.h>
#include <dirent.h>
#define bool int
#define true 1
#define false 0
// Use this for debugging
//#define DEBUG
#define openZip(name) zipOpen64(name, 0)
#define closeZip(name) zipClose(name, NULL)
#define ull_int unsigned long long int
#define WRITEBUFFERSIZE (16384)
int buffer_size = WRITEBUFFERSIZE; // Buffer Size
void *buffer; //The buffer used to read files
struct stat s;
int compress_level = Z_DEFAULT_COMPRESSION;// If user doesn't choose any compression level the default will be used
FILE *logs;
bool should_continue = true;
uint64_t GetFileSize(FILE* fp)
{
uint64_t tmp;
fseeko64(fp, 0, SEEK_END);
tmp = ftello64(fp);
rewind(fp);
return tmp;
}
void SetCompressLevel(char* level)
{
int tmp;
tmp = atoi(level);
if(tmp>=0 && tmp<= 9)
{
compress_level = tmp;
if(logs != stdout){
fprintf(logs,"Compression level set to: %d\n", compress_level);
}
fprintf(stdout,"Compression level set to: %d\n", compress_level);
}
}
void AllowLog(char* name)
{
char coise[strlen(name)+4];
strcpy(coise, name);
strcat(coise,".txt");
logs = fopen(coise, "a");
}
void ZipTheFile(zipFile* zf, char *filename)
{
uint64_t size_read = 1; // Ammount of bytes read
uint64_t flsize = 0;
int written = 0;
FILE* fp;
fp = fopen64(filename, "rb");
if(fp == NULL)
{
if(logs != stdout){
fprintf(logs,"Couldn't open file %s. %s", filename, (should_continue ? "Continuing..\n" : "Stopping.\n" ));
}
fprintf(stdout,"Couldn't open file %s. %s", filename, (should_continue ? "Continuing..\n" : "Stopping.\n" ));
if(should_continue == 0)
{
return;
}
}
#ifdef DEBUG
#else
if(logs != stdout){
fprintf(logs,"File %s opened successfuly.\n", filename);
}
fprintf(stdout,"File %s opened successfuly.\n", filename);
#endif
flsize = GetFileSize(fp);
zipOpenNewFileInZip(*zf, filename, NULL, NULL, 0, NULL,0, NULL, (compress_level ? Z_DEFLATED : Z_NO_COMPRESSION), compress_level);
while(1)
{
size_read = fread(buffer, 1, buffer_size, fp); // Reading the file, buf get the contents size_read the ammount of bytes read
if(size_read)//If different than 0
{
written+=size_read;
zipWriteInFileInZip(*zf, buffer, size_read);//Writting the file
}
else
{
break;
}
}
if(written == flsize)
{
#ifdef DEBUG
#else
if(logs != stdout){
fprintf(logs,"File %s successfully writen to zip file.\n", filename);}
fprintf(stdout,"File %s successfully writen to zip file.\n", filename);
#endif
}
zipCloseFileInZip(*zf);
fclose(fp);
}
void RealZipDir(zipFile *zp, DIR **dir, char* filen)
{
struct dirent *ent;
DIR* tmp;
while((ent = readdir(*dir)))
{
if(!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
{
}
else
{
char name[256];
strcpy(name, filen);
strcat(name, "/");
strcat(name, ent->d_name);
if( stat(name,&s) == 0 )
{
if( s.st_mode & S_IFDIR )
{
tmp = opendir(name);
if(tmp != NULL)
{
RealZipDir(zp, &tmp, name);//Recursive if finds another dir
}
}
else
{
ZipTheFile(zp, name);//Zip that shit up
}
}
}
}
closedir(*dir);
}
int main (int argc, char *argv[])
{
logs = stdout;
zipFile zf;
DIR *dir;
char path[256];
int last_pos = 0;
if(argc < 3)
{
puts("Usage: ./realzip [flags] file.zip [files to zip]");
puts("Flags:\n \t -k Stop when a file can't be openned \n \t -c [0-9] Compress level\n\t -l To create a log file. \nThe log file name will be [zipfilename].txt. It will append if it already exists");
exit(2);
}
buffer = malloc(buffer_size);
if(buffer == NULL)
{
exit(1);
}
int want_log = 0;
for(int i = 1; i<=5; i++)
{
if(argv[i][0] == '-')
{
switch(argv[i][1])
{
case 'c':
SetCompressLevel(argv[i+1]);
i++;
break;
case 'l':
want_log = 1;
break;
case 'k':
should_continue = false;
break;
}
}
else
{
last_pos = i;
if(want_log)
{AllowLog(argv[i]);}
break;
}
}
zf = openZip(argv[last_pos]);
last_pos++;
for(int i = last_pos; i<argc;i++)
{
if( stat(argv[i],&s) == 0 )
{
if( s.st_mode & S_IFDIR )
{
dir = opendir(argv[i]);
if(dir != NULL)
{
strcpy(path, argv[i]);
RealZipDir(&zf, &dir, path);
}
else
{
#ifdef DEBUG
#else
if(logs != stdout){
fprintf(logs,"Error opening dir %s\n",argv[2]);}
fprintf(stdout,"Error opening dir %s\n",argv[2]);
#endif
}
}
else
{
ZipTheFile(&zf, argv[i]);//Zip that shit up
}
}
else
{
if(logs != stdout)
{
fprintf(logs,"Couldn't open file %s. %s", argv[i], (should_continue ? "Continuing..\n" : "Stopping.\n" ));
}
fprintf(stdout,"Couldn't open file %s. %s", argv[i], (should_continue ? "Continuing..\n" : "Stopping.\n" ));
if(should_continue == 0)
{
exit(1);
}
}
}
closeZip(zf);
free(buffer);
return 0;
}