| 1 |
/****************************************************************************** |
| 2 |
This source file is part of the Avogadro project. |
| 3 |
This source code is released under the 3-Clause BSD License, (see "LICENSE"). |
| 4 |
******************************************************************************/ |
| 5 |
|
| 6 |
#include "zipextractor.h" |
| 7 |
|
| 8 |
#include <QtCore/QDebug> |
| 9 |
#include <QtCore/QString> |
| 10 |
|
| 11 |
namespace Avogadro::QtPlugins { |
| 12 |
|
| 13 |
ZipExtractor::ZipExtractor() {} |
| 14 |
|
| 15 |
ZipExtractor::~ZipExtractor() {} |
| 16 |
|
| 17 |
/** |
| 18 |
* Copies data from one archive to another. |
| 19 |
* |
| 20 |
* This is a convenience function to copy data from one archive to another. |
| 21 |
* It will copy the data block by block until it reaches the end of the |
| 22 |
* input archive. If there is an error reading or writing the data, it |
| 23 |
* will return the error code directly. |
| 24 |
*/ |
| 25 |
int ZipExtractor::copyData(struct archive* ar, struct archive* aw) |
| 26 |
{ |
| 27 |
int r; |
| 28 |
const void* buff; |
| 29 |
size_t size; |
| 30 |
la_int64_t offset; |
| 31 |
|
| 32 |
for (;;) { |
| 33 |
r = archive_read_data_block(ar, &buff, &size, &offset); |
| 34 |
if (r == ARCHIVE_EOF) |
| 35 |
return (ARCHIVE_OK); |
| 36 |
if (r < ARCHIVE_OK) |
| 37 |
return (r); |
| 38 |
r = archive_write_data_block(aw, buff, size, offset); |
| 39 |
if (r < ARCHIVE_OK) { |
| 40 |
qWarning() << archive_error_string(aw); |
| 41 |
return (r); |
| 42 |
} |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
QList<QString> ZipExtractor::listFiles(const std::string& absolutepath) |
| 47 |
{ |
| 48 |
struct archive* a; |
| 49 |
struct archive_entry* entry; |
| 50 |
int r; |
| 51 |
|
| 52 |
a = archive_read_new(); |
| 53 |
archive_read_support_format_zip(a); |
| 54 |
archive_read_support_filter_all(a); |
| 55 |
|
| 56 |
QList<QString> toReturn; |
| 57 |
|
| 58 |
if ((r = archive_read_open_filename(a, absolutepath.c_str(), 512))) { |
| 59 |
toReturn.append( |
| 60 |
QString(tr("ERROR: could not open zip file to list contents.\n(%1)", |
| 61 |
"%1 is the error message from libarchive")) |
| 62 |
.arg(archive_error_string(a))); |
| 63 |
archive_read_free(a); |
| 64 |
return toReturn; |
| 65 |
} |
| 66 |
|
| 67 |
for (;;) { |
| 68 |
r = archive_read_next_header(a, &entry); |
| 69 |
if (r < ARCHIVE_OK) |
| 70 |
break; |
| 71 |
|
| 72 |
toReturn.append(archive_entry_pathname(entry)); |
| 73 |
archive_read_data_skip(a); // Note 2 |
| 74 |
} |
| 75 |
archive_read_free(a); |
| 76 |
|
| 77 |
return toReturn; |
| 78 |
} |
| 79 |
|
| 80 |
// Extract method from libarchive docs, changed to return QList of errors |
| 81 |
QList<QString> ZipExtractor::extract(const std::string& extractdir, |
| 82 |
const std::string& absolutepath) |
| 83 |
{ |
| 84 |
struct archive* a; |
| 85 |
struct archive* ext; |
| 86 |
struct archive_entry* entry; |
| 87 |
int flags; |
| 88 |
int r; |
| 89 |
QList<QString> toReturn; |
| 90 |
|
| 91 |
// Ensure extractdir ends with a path separator |
| 92 |
std::string extractPrefix = extractdir; |
| 93 |
if (!extractPrefix.empty() && extractPrefix.back() != '/') |
| 94 |
extractPrefix += '/'; |
| 95 |
|
| 96 |
/* Select which attributes we want to restore. */ |
| 97 |
flags = ARCHIVE_EXTRACT_TIME; |
| 98 |
flags |= ARCHIVE_EXTRACT_PERM; |
| 99 |
flags |= ARCHIVE_EXTRACT_ACL; |
| 100 |
flags |= ARCHIVE_EXTRACT_FFLAGS; |
| 101 |
|
| 102 |
a = archive_read_new(); |
| 103 |
archive_read_support_format_all(a); |
| 104 |
archive_read_support_filter_all(a); |
| 105 |
|
| 106 |
ext = archive_write_disk_new(); |
| 107 |
archive_write_disk_set_options(ext, flags); |
| 108 |
archive_write_disk_set_standard_lookup(ext); |
| 109 |
|
| 110 |
auto cleanup = [&]() { |
| 111 |
archive_read_free(a); |
| 112 |
archive_write_free(ext); |
| 113 |
}; |
| 114 |
|
| 115 |
if ((r = archive_read_open_filename(a, absolutepath.c_str(), 10240))) { |
| 116 |
toReturn.append( |
| 117 |
QString(tr("ERROR: could not open zip file to extract files.\n(%1)", |
| 118 |
"%1 is the error message from libarchive")) |
| 119 |
.arg(archive_error_string(a))); |
| 120 |
cleanup(); |
| 121 |
return toReturn; |
| 122 |
} |
| 123 |
|
| 124 |
for (;;) { |
| 125 |
r = archive_read_next_header(a, &entry); |
| 126 |
|
| 127 |
if (r == ARCHIVE_EOF) { |
| 128 |
break; |
| 129 |
} |
| 130 |
if (r < ARCHIVE_OK) |
| 131 |
qWarning() << archive_error_string(a); |
| 132 |
if (r < ARCHIVE_WARN) { |
| 133 |
toReturn.append( |
| 134 |
QString(tr("Warning: (%1)", "%1 is the message from libarchive")) |
| 135 |
.arg(archive_error_string(a))); |
| 136 |
cleanup(); |
| 137 |
return toReturn; |
| 138 |
} |
| 139 |
|
| 140 |
std::string newFilename = extractPrefix + archive_entry_pathname(entry); |
| 141 |
archive_entry_set_pathname(entry, newFilename.c_str()); |
| 142 |
r = archive_write_header(ext, entry); |
| 143 |
if (r < ARCHIVE_OK) |
| 144 |
qWarning() << archive_error_string(ext); |
| 145 |
else if (archive_entry_size(entry) > 0) { |
| 146 |
r = copyData(a, ext); |
| 147 |
if (r < ARCHIVE_OK) |
| 148 |
qWarning() << archive_error_string(ext); |
| 149 |
if (r < ARCHIVE_WARN) { |
| 150 |
toReturn.append( |
| 151 |
QString(tr("Warning: (%1)", "%1 is the message from libarchive")) |
| 152 |
.arg(archive_error_string(a))); |
| 153 |
cleanup(); |
| 154 |
return toReturn; |
| 155 |
} |
| 156 |
} |
| 157 |
r = archive_write_finish_entry(ext); |
| 158 |
if (r < ARCHIVE_OK) |
| 159 |
qWarning() << archive_error_string(ext); |
| 160 |
if (r < ARCHIVE_WARN) { |
| 161 |
toReturn.append( |
| 162 |
QString(tr("Warning: (%1)", "%1 is the message from libarchive")) |
| 163 |
.arg(archive_error_string(a))); |
| 164 |
cleanup(); |
| 165 |
return toReturn; |
| 166 |
} |
| 167 |
} |
| 168 |
cleanup(); |
| 169 |
return toReturn; |
| 170 |
} |
| 171 |
|
| 172 |
} // namespace Avogadro::QtPlugins |