-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompressor.c
More file actions
83 lines (60 loc) · 1.78 KB
/
compressor.c
File metadata and controls
83 lines (60 loc) · 1.78 KB
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
#include <raylib.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char *suffix = ".compressed";
int main(int argc, char *argv[]) {
if(argc != 2 && argc != 3){
printf("Usage: %s -[d] <file>\n", argv[0]);
return 1;
}
char *input_file;
bool decompress = false;
if(argc == 2){
input_file = argv[1];
} else {
if(strcmp(argv[1], "-d") != 0){
printf("Invalid option: %s\n", argv[1]);
printf("Usage: %s -[d] <file>\n", argv[0]);
return 1;
}
input_file = argv[2];
decompress = true;
}
FILE *input = fopen(input_file, "r");
if(!input){
printf("Could not open file: %s\n", input_file);
return 1;
}
suffix = decompress ? ".decompressed" : ".compressed";
int filename_len = strlen(input_file)+ strlen(suffix);
char *output_file = (char *)malloc(filename_len);
sprintf(output_file, "%s%s", input_file, suffix);
printf("Compressing %s to %s\n", input_file, output_file);
FILE *output = fopen(output_file, "w");
if(!output){
printf("Could not create file: %s\n", output_file);
return 1;
}
fseek(input, 0L, SEEK_END);
long input_size = ftell(input);
rewind(input);
unsigned char *input_data = (unsigned char *)malloc(input_size);
int c;
int index = 0;
while((c = fgetc(input)) != EOF){
input_data[index++] = c;
}
int output_size = 0;
unsigned char *final_data;
if(decompress){
final_data = DecompressData(input_data, input_size, &output_size);
}
else{
final_data = CompressData(input_data, input_size, &output_size);
}
for(int i = 0; i < output_size; i++){
fputc(final_data[i], output);
}
return 0;
}