-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathArchive.cpp
74 lines (59 loc) · 1.96 KB
/
Archive.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
//
// Created by Алексей on 2019-11-17.
//
#include "Archive.h"
ModelResponse<> Archive::handle(Request request) {
ModelResponse<> response;
response.module = ModelResponse<>::archive;
try {
std::map <std::string, std::string> files_to_archive;
for (auto it : request.filenames) {
Selection selection(TypeIdentifier::SignatureDetect(it));
selection.Compress(it, new_name(it));
files_to_archive.insert(std::make_pair(it, new_name(it)));
}
Archiver archiver(request.archive_path);
archiver.Pack(files_to_archive, request.archive_path, request.archive_name);
for (auto it : request.filenames) {
Input input(new_name(it));
input.RemoveFile();
}
}
catch (std::invalid_argument& exept) {
response.state = ModelResponse<>::error;
response.info = exept.what();
return response;
}
catch(...) {
response.state = ModelResponse<>::error;
response.info = "Can't archive files";
return response;
}
response.state = ModelResponse<>::ok;
response.info = "File(s) successfully archived";
return response;
}
bool Archive::can_handle(Request request) {
return request.type == Request::archive;
}
std::string Archive::new_name(std::string &CompressedFileName) {
std::string TypeFile;
std::string NameFileWithoutExtension;
bool flag = false;
for(auto it = CompressedFileName.end(); it >= CompressedFileName.begin(); it--){
if(*it == '.' and !flag){
flag = true;
continue;
}
if(!flag){
TypeFile.append(1, *it);
}
if(flag){
NameFileWithoutExtension.append(1, *it);
}
}
std::reverse(TypeFile.begin(), TypeFile.end());
std::reverse(NameFileWithoutExtension.begin(), NameFileWithoutExtension.end());
std::string out = NameFileWithoutExtension + ".bin";
return out;
}