-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArchivator.cpp
130 lines (109 loc) · 3.68 KB
/
Archivator.cpp
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
#include "Archivator.h"
void Archivator::getInfo() {
basic_std::string<char> s_info = "";
remove((this->path+"info.temp").c_str());
FILE *info = fopen((this->path+"info.temp").c_str(),"a+");
int bytes_size = 0;
for (std::vector<std::string>::iterator itr = this->files.begin(); itr != this->files.end(); ++itr) {
FILE *f = fopen((*itr).c_str(),"rb");
if(!f)
break;
fseek(f, 0, SEEK_END);
int size = ftell(f);
std::string name = Archivator::get_file_name(*itr);
char *m_size = new char[digs(size)];
itoa(size, m_size, 10);
fclose(f);
s_info.append(m_size);
s_info.append("||");
s_info.append(name);
s_info.append("||");
delete [] m_size;
}
bytes_size = s_info.size() + 2;
char *b_buff = new char[digs(bytes_size)];
itoa(bytes_size, b_buff, 10);
if(digs(bytes_size) < 5)
fputs(std::string(5 - digs(bytes_size),'0').c_str(), info);
fputs(b_buff, info);
fputs("||", info);
fputs(s_info.c_str(), info);
delete[] b_buff;
fclose(info);
}
void Archivator::InCompress(std::string alg) {
char byte[1];
getInfo();
FILE *f, *main = fopen((this->real_bin_file).c_str(), "wb");
FILE *info = fopen((this->path + "info.temp").c_str(), "rb");
while(!feof(info)){
if(fread(byte, 1, 1, info) == 1)
fwrite(byte, 1, 1, main);
}
fclose(info);
remove((this->path + "info.temp").c_str());
for(std::vector<std::string>::iterator itr = this->files.begin(); itr != this->files.end(); ++itr){
f = fopen((*itr).c_str(), "rb");
if (!f){
cout<<*itr<<" not found!"<<endl;
break;
}
while (!feof(f)){
if (fread(byte, 1, 1, f) == 1)
fwrite(byte, 1, 1, main);
}
cout<<*itr<<" packed to '"<<this->real_bin_file<<"'."<<endl;
fclose(f);
}
fclose(main);
if (alg == "hfm")
hfm_encode(this->real_bin_file, this->path);
else if (alg == "lzw")
lzw_encode(this->real_bin_file, this->path);
// remove((this->path + "binary.Archivator").c_str());
}
void Archivator::OutCompress(std::string binary) {
FILE *encoded = fopen(binary.c_str(), "rb");
char alg[5];
fread(alg, 1, 5, encoded);
fclose(encoded);
if (strcmp(alg, "hfm||") == 0)
hfm_decode(binary, this->path);
else if (strcmp(alg, "lzw||") == 0)
lzw_decode(binary, this->path);
FILE *bin = fopen((this->path + "decoded_binary.Archivator").c_str(),"rb");
char info_block_size[5];
fread(info_block_size, 1, 5, bin);
int _sz = atoi(info_block_size);
char *info_block = new char[_sz];
fread(info_block, 1, _sz, bin);
std::vector<std::string> tokens;
char *tok = strtok(info_block, "||");
int toks = 0;
while(tok){
if (strlen(tok) == 0)
break;
tokens.push_back(tok);
tok = strtok(NULL, "||");
toks++;
};
int files{1};
if (toks != 1)
files = toks/2;
char byte[1];
for(int i = 0; i < files; ++i){
char full_path[255];
strcpy(full_path, this->path.c_str());
strcat(full_path, tokens[i*2 + 1].c_str());
int _sz = atoi(tokens[i*2].c_str());
FILE *curr = fopen(full_path, "wb");
for(int r {1}; r <= _sz; ++r)
if(fread(byte, 1, 1, bin) == 1)
fwrite(byte, 1, 1, curr);
cout<<"-- '"<<tokens[i*2 + 1].c_str()<<"' unpacked to '"<<this->path<<"' ."<<endl;
fclose(curr);
};
fclose(bin);
delete[] info_block;
// remove((this->path + "decoded_binary.Archivator").c_str());
}