-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathArchiver.cpp
84 lines (66 loc) · 2.13 KB
/
Archiver.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
//
// Created by perturabo on 15.11.2019.
//
#include "Archiver.hpp"
#include "ArchiveFileReader.hpp"
#include <utility>
std::string Archiver::Pack (const std::map<std::string, std::string>& compressed_data,std::string path,
const std::string& name){
path.append("/");
path.append(name);
path.append(".tprk");
path_to_archive = path;
ArchiveFileWriter archive (path_to_archive);
content_start = archive.CreateEntrySystem(compressed_data);
std::vector<Entry> EntrySystem = archive.ViewArchive ();
for(auto& entry : EntrySystem){
archive.WriteFile (entry.bin_name);
}
archive.CloseOutput ();
return path_to_archive;
}
std::vector<Entry> Archiver::Read (){
ArchiveFileReader archive(path_to_archive);
std::vector<Entry> EntrySystem;
Entry entry_iter;
unsigned long int entry_system_length = ArchiveFileWriter::GetPointer (archive.GetLine ());
for(unsigned long int i = 0; i < entry_system_length; i++){
archive.GetEntry(entry_iter);
EntrySystem.push_back(entry_iter);
}
content_start = ArchiveFileWriter::GetPointer (archive.GetLine ());
archive.CloseInput();
return EntrySystem;
}
std::map<std::string, std::string> // name, bin_name
Archiver::Unpack (const std::vector<Entry>& EntrySystem){
std::map<std::string, std::string> out;
unsigned long int i = 0;
for(auto entry: EntrySystem){
CutABinary(entry, i);
std::pair<std::string, std::string> p(entry.name, entry.bin_name);
out.insert(p);
i++;
}
return out;
}
std::pair<std::string, std::string> // name, bin_name
Archiver::UnpackSingle (Entry entry, std::string name){
CutABinary(entry, 0);
std::pair<std::string, std::string> p(entry.name, entry.bin_name);
return p;
}
std::string Archiver::CutABinary(Entry& entry, unsigned long int name_binary){
ArchiveFileReader archive (path_to_archive);
archive.SkipTo(content_start + entry.start - name_binary);
char buf[100];
std::snprintf(buf, sizeof(buf), "%lu.bin", name_binary);
Output out(buf);
size_t length = entry.end - entry.start;
byte buff;
for(unsigned long int i = 0; i < length; i++){
archive.Read(buff);
out.Write(buff);
}
return buf;
}