-
Notifications
You must be signed in to change notification settings - Fork 0
/
compressor.c
145 lines (137 loc) · 2.75 KB
/
compressor.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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "huffman.h"
int zip(char * inputFileName, char * outputFileName)
{
FILE * fileIn, *fileOut;
fileIn = fopen(inputFileName, "r");
if(!fileIn)
{
printf("Error, archivo no encontrado\n");
return -1;
}
fileOut = fopen(outputFileName, "wb");
Node *letters = buildTree(&fileIn);
Table * table = buildTable(letters);
writeTable(&fileOut, table, letters->frecuency);
fileIn = fopen(inputFileName, "r");
code(table, &fileIn, &fileOut);
fclose(fileIn);
fclose(fileOut);
return 0;
}
unsigned long readWord(FILE ** in)
{
unsigned long bits = 0;
fread(&bits, sizeof(unsigned long), 1, *in);
unsigned long maskA = bits&0x000000FFUL;
unsigned long maskB = bits&0x0000FF00UL;
unsigned long maskC = bits&0x00FF0000UL;
unsigned long maskD = bits&0xFF000000UL;
maskA<<=24;
maskB<<=8;
maskC>>=8;
maskD>>=24;
return (maskA)+(maskB)+(maskC)+maskD;
}
int unzip(char * inputFileName, char * outputFileName)
{
FILE * fileIn, *fileOut;
fileIn = fopen(inputFileName, "r");
if(!fileIn)
{
printf("Error, archivo no encontrado\n");
return -1;
}
fileOut = fopen(outputFileName, "wb");
char s;
unsigned long len;
fread(&s, 1, 1, fileIn);
fread(&len, sizeof(unsigned long), 1, fileIn);
if(s>0)
{
char character;
unsigned char size;
unsigned long bits;
Node * letters = NULL;
while(s--)
{
fread(&character, 1, 1, fileIn);
fread(&size, 1, 1, fileIn);
fread(&bits, sizeof(unsigned long), 1, fileIn);
bitTree(&letters, character, size, bits);
}
bits = readWord(&fileIn);
size = 8;
while(len-->0)
{
decode(letters,&size,&bits,&fileIn,&fileOut);
}
}
fclose(fileIn);
fclose(fileOut);
return 0;
}
char * clean(char * in)
{
int l = strlen(in)-1;
char * ext = ".rol";
int ll = strlen(ext)-1;
for(int i = 0;i<4;i++)
{
if(in[l-i]!=ext[ll-i])
{
return NULL;
}
in[l-i]=0;
}
return in;
}
int main(int argc, char const *argv[])
{
if(argc<3)
{
printf("\nPor favor introduzca alguno de los siguientes parametros\n");
printf("\tzip <file>: comprime el archivo\n");
printf("\tunzip <file>: descomprime el archivo\n\n");
}
else
{
char * in = argv[2];
char* out;
if(!strcmp(argv[1],"zip"))
{
printf("Comprimiendo...\n");
out = malloc(strlen(in)+4);
strcpy(out, in);
strcat(out, ".rol");
clean(in);
if(zip(in, out))
{
return -1;
}
}
else if(!strcmp(argv[1],"unzip"))
{
printf("Desomprimiendo...\n");
out = malloc(strlen(in));
strcpy(out, in);
out = clean(out);
if(out)
{
if(unzip(in, out))
{
return -1;
}
}
else
{
printf("Error, formato no reconocido\n");
return -2;
}
}
printf("Listo.\n");
}
return 0;
}