-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_compression.c
117 lines (101 loc) · 2.76 KB
/
main_compression.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "arbre.h"
#include "huffman.h"
#define T_MAX 10000
void compress(char *file_in, char *file_out)
{
arbre a;
dico d;
int n , len, i;
int occ[256] = {0};
entree table[256] = {0};
unsigned char *serial =malloc (sizeof (char)*256) ;
unsigned char c ;
entree e;
Bin_file *in = (Bin_file *)malloc(sizeof(Bin_file));
open_file_read(in, file_in);
// N=nb de char diff
n = countCharInFile(occ, in->file);
a = stringToHuffman(occ);
//print_arbre(a);
len = ser(a, serial, 0);
d = get_table(a , n, table);
/* i = 0;
for (i = 0; i < n; i++) {
printEntree(d[i]);
}*/
Bin_file *out = (Bin_file *)malloc(sizeof(Bin_file));
open_file_write(out, file_out);
fprintf(out->file, "%d\n", a->poids);
fprintf(out->file, "%d\n", len);
fprintf(out->file, "%s\n", serial);
open_file_read(in, file_in);
fscanf(in->file, "%c", &c) ;
while (!feof(in->file)) {
e = code(c, d) ;
for (i = 0; i < e.len; i++) {
ecrire_2(out, e.seqBits[i]) ;
}
fscanf(in->file, "%c", &c) ;
}
while (out->i_octet > 0) {
ecrire_2(out, 0) ;
}
close_file(in);
close_file(out);
printf("Compression effectuee\n");
}
void extract(char *file_in, char *file_out){
arbre a;
int n , i , j , len;
unsigned char s[T_MAX];
unsigned char c;
Bin_file *in = (Bin_file *)malloc(sizeof(Bin_file));
Bin_file *out = (Bin_file *)malloc(sizeof(Bin_file));
in->i_record = 0;
in->record_length = 0;
open_file_read(in,file_in);
open_file_write(out,file_out);
fscanf(in->file , "%d\n" , &n);
fscanf(in->file, "%d\n", &len);
j=0;
while (j < len){
fscanf(in->file, "%c", &c);
s[j++] = c;
}
s[j] = 0;
fscanf(in->file, "%c", &c);
a = deserialisation_sup(s);
for (i=0 ; i<n ; i++ ){
c = findChar(a, in);
fprintf(out->file , "%c", c);
}
close_file(in);
close_file(out);
printf("Extraction effectuee\n");
}
int main(int argc, char *argv[])
{
char c;
char source[100], sortie[100] ;
printf("Voulez-vous compresser ou décompresser (c ou d) ?\n");
scanf("%c", &c) ;
if (c == 'c') {
printf("Entrer un fichier source à compresser :\n");
scanf("%s", source) ;
printf("Entrer un fichier de sortie :\n");
scanf("%s", sortie) ;
compress(source, sortie);
} else if (c == 'd') {
printf("Entrer un fichier source à décompresser:\n");
scanf("%s", source) ;
printf("Entrer un fichier de sortie :\n");
scanf("%s", sortie) ;
extract(source , sortie);
} else {
printf ("Erreur\n") ;
}
return 0;
}