-
Notifications
You must be signed in to change notification settings - Fork 0
/
Archive.cpp
40 lines (36 loc) · 1.01 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
// SPDX-License-Identifier: AGPL-3.0-or-later
// (C) 2023 Bernhard Rosenkränzer <[email protected]>
#include "Archive.h"
extern "C" {
#include <archive_entry.h>
}
Archive::Archive(String const &filename, int format):_isOpen(true) {
_archive = archive_write_new();
archive_write_set_format(_archive, format);
archive_write_open_filename(_archive, filename);
}
Archive::~Archive() {
close();
}
void Archive::close() {
if(!_isOpen)
return;
_isOpen = false;
archive_write_close(_archive);
archive_write_free(_archive);
}
bool Archive::addFile(String const &filename, QByteArray const &contents) const {
archive_entry *e = archive_entry_new();
if(!e)
return false;
bool ret = true;
archive_entry_set_pathname(e, filename);
archive_entry_set_size(e, contents.size());
archive_entry_set_filetype(e, AE_IFREG);
archive_entry_set_perm(e, 0644);
archive_write_header(_archive, e);
if(archive_write_data(_archive, contents.constData(), contents.length()) != ARCHIVE_OK)
ret = false;
archive_entry_free(e);
return ret;
}