ArchiveWriter.cc
4.36 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
* ArchiveWriter.cc
*
* Created on: 26 sept. 2013
* Author: CS
*/
#include "ArchiveWriter.hh"
#include "archive.h"
#include <archive.h>
#include <archive_entry.h>
#include <boost/filesystem.hpp>
#include <boost/scope_exit.hpp>
#include <boost/filesystem/path.hpp>
#include <stdexcept>
#include <fstream>
#include <sstream>
#include <sys/stat.h>
namespace postprocessing {
log4cxx::LoggerPtr ArchiveWriter::_logger = log4cxx::Logger::getLogger(
"AMDA-Kernel.ArchiveWriter");
ArchiveWriter::ArchiveWriter(const std::string& pArchiveFileName,
const Formats& pFormat) :
_open(true), _archive(archive_write_new()), _entry(archive_entry_new()), _archiveFileName(
pArchiveFileName), _format(pFormat) {
switch (pFormat) {
case TAR:
checkError(archive_write_set_format_gnutar(_archive), true);
break;
case ZIP:
checkError(archive_write_set_format_zip(_archive), true);
break;
}
checkError(archive_write_open_filename(_archive, pArchiveFileName.c_str()),
true);
}
ArchiveWriter::~ArchiveWriter() {
close();
}
void ArchiveWriter::checkError(const int pErrCode,
const bool pCloseBeforeThrow) {
if (pCloseBeforeThrow && pErrCode == ARCHIVE_FATAL)
close();
if (pErrCode != ARCHIVE_OK && pErrCode != ARCHIVE_WARN)
throw std::runtime_error(archive_error_string(_archive));
}
void ArchiveWriter::addHeader(const std::string& pFilePath,
const std::string& pEntryName) {
struct archive* a = archive_read_disk_new();
BOOST_SCOPE_EXIT(&a) {
if (a != NULL) {
archive_read_close(a);
archive_read_free(a);
}
}
BOOST_SCOPE_EXIT_END
_entry = archive_entry_clear(_entry);
archive_entry_set_pathname(_entry, pEntryName.c_str());
archive_entry_copy_sourcepath(_entry, pFilePath.c_str());
checkError(archive_read_disk_entry_from_file(a, _entry, -1, 0));
checkError(archive_write_header(_archive, _entry));
}
void ArchiveWriter::addFinish() {
archive_write_finish_entry(_archive);
}
void ArchiveWriter::addFile(const std::string& pFilePath,
const std::string& pEntryName) {
if (boost::filesystem::exists(pFilePath)) {
boost::filesystem::file_status file_stat = boost::filesystem::status(
pFilePath);
addHeader(pFilePath, pEntryName.empty() ? pFilePath : pEntryName);
if (file_stat.type() == boost::filesystem::regular_file) {
std::fstream entry_file(pFilePath.c_str(), std::ios::in);
char buff[8192];
while (entry_file.good()) {
entry_file.read(buff, 8192);
archive_write_data(_archive, buff,
static_cast<size_t>(entry_file.gcount()));
}
entry_file.close();
} else
throw std::runtime_error("Entry file type not yet supported.");
addFinish();
} else
throw std::runtime_error("Entry file not found.");
}
void ArchiveWriter::close() {
if (_open) {
if (_archive != NULL) {
archive_write_close(_archive);
archive_write_free(_archive);
LOG4CXX_INFO(_logger, "Written archive " + _archiveFileName);
}
if (_entry != NULL)
archive_entry_free(_entry);
_open = false;
}
}
std::string ArchiveWriter::createArchive(const Formats& pFormat,
const std::string& pExtension, const std::vector<std::string>& pFiles,
const std::string& pPrefix, const std::string& pOutDir) {
if (pFiles.empty()) {
LOG4CXX_WARN(_logger, "No file to archive or compress.");
return std::string();
}
// calculate out file name (timestamp)
std::ostringstream archiveFileName;
if (!pPrefix.empty()) {
archiveFileName << pPrefix << "_";
}
archiveFileName << std::time(0) << pExtension;
std::string archiveCompletePath;
// calculate out file path
if (!pOutDir.empty()) {
// user path
archiveCompletePath = pOutDir + "/" + archiveFileName.str();
} else {
// default path is first file parent path
std::string firstFile = pFiles.front();
if(boost::filesystem::path(firstFile).parent_path().string().empty()){
archiveCompletePath = archiveFileName.str();
}
else {
archiveCompletePath =
boost::filesystem::path(firstFile).parent_path().string()
+ "/" + archiveFileName.str();
}
}
// create archive
ArchiveWriter writer(archiveCompletePath, pFormat);
// add each file to the archive
for (auto file : pFiles) {
// Remove "./" at the begining of the filename
if (file.compare (0, 2, "./") == 0)
file = file.substr(2);
boost::filesystem::path path(file);
writer.addFile(file, path.c_str());
}
// close the archive
writer.close();
return archiveCompletePath;
}
} /* namespace postprocessing */