-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Utilities: Add karchive library #13160
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
HTRamsey
wants to merge
1
commit into
mavlink:master
Choose a base branch
from
HTRamsey:dev-compression-karchive
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+211
−0
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
#include "karchive.h" | ||
#include "QGCLoggingCategory.h" | ||
|
||
#include <QtCore/QDir> | ||
#include <QtCore/QFileInfo> | ||
#include <QtCore/QFile> | ||
#include <QtCore/QDebug> | ||
|
||
#include <memory> | ||
|
||
#include <KCompressionDevice> | ||
#include <ktar.h> | ||
#include <kzip.h> | ||
|
||
#if __has_include(<k7zip.h>) | ||
#include <k7zip.h> | ||
#define KARCHIVE_HAS_K7ZIP 1 | ||
#else | ||
#define KARCHIVE_HAS_K7ZIP 0 | ||
#endif | ||
|
||
QGC_LOGGING_CATEGORY(karchiveLog, "qgc.utilities.compression.karchive") | ||
|
||
namespace | ||
{ | ||
|
||
/// Returns only the file/folder name component of a path ("/a/b/c" -> "c") | ||
QString _archiveRootName(const QString &path) | ||
{ | ||
return QFileInfo(path).fileName(); | ||
} | ||
|
||
/// Factory: choose the correct *writer* class based on the file extension. | ||
std::unique_ptr<KArchive> _makeWriter(const QString &archivePath) | ||
{ | ||
const QString lower = archivePath.toLower(); | ||
|
||
if (lower.endsWith(".zip")) { | ||
return std::make_unique<KZip>(archivePath); | ||
} | ||
|
||
// Tar family (plain, gzip, bzip2, xz) | ||
if (lower.endsWith(".tar") || lower.endsWith(".tar.gz") || lower.endsWith(".tgz") || | ||
lower.endsWith(".tar.bz2")|| lower.endsWith(".tbz2") || lower.endsWith(".tbz") || | ||
lower.endsWith(".tar.xz") || lower.endsWith(".txz")) { | ||
return std::make_unique<KTar>(archivePath); | ||
} | ||
|
||
#if KARCHIVE_HAS_K7ZIP | ||
// NOTE: K7Zip (at least in KF5/KF6) is **read‑only**. We therefore return nullptr | ||
// to tell the caller this archive type cannot be *created*. | ||
if (lower.endsWith(".7z")) { | ||
return nullptr; | ||
} | ||
#endif | ||
|
||
return nullptr; // Unknown format | ||
} | ||
|
||
/// Factory: choose the correct *reader* class based on the file extension. | ||
std::unique_ptr<KArchive> _makeReader(const QString &archivePath) | ||
{ | ||
const QString lower = archivePath.toLower(); | ||
|
||
if (lower.endsWith(".zip")) { | ||
return std::make_unique<KZip>(archivePath); | ||
} | ||
|
||
if (lower.endsWith(".tar") || lower.endsWith(".tar.gz") || lower.endsWith(".tgz") || | ||
lower.endsWith(".tar.bz2")|| lower.endsWith(".tbz2") || lower.endsWith(".tbz") || | ||
lower.endsWith(".tar.xz") || lower.endsWith(".txz")) { | ||
return std::make_unique<KTar>(archivePath); | ||
} | ||
|
||
#if KARCHIVE_HAS_K7ZIP | ||
if (lower.endsWith(".7z")) { | ||
return std::make_unique<K7Zip>(archivePath); | ||
} | ||
#endif | ||
|
||
return nullptr; // Unknown format | ||
} | ||
|
||
} | ||
|
||
namespace karchive | ||
{ | ||
|
||
bool createArchive(const QString &directoryPath, const QString &archivePath) | ||
{ | ||
// Sanity checks -------------------------------------------------- | ||
if (!QDir(directoryPath).exists()) { | ||
qCWarning(karchiveLog) << "Directory does not exist:" << directoryPath; | ||
return false; | ||
} | ||
|
||
const QFileInfo info(archivePath); | ||
if (!QDir().mkpath(info.absolutePath())) { | ||
qCWarning(karchiveLog) << "Unable to create directory for archive:" << info.absolutePath(); | ||
return false; | ||
} | ||
|
||
// Select writer --------------------------------------------------- | ||
auto archive = _makeWriter(archivePath); | ||
if (!archive) { | ||
qCWarning(karchiveLog) << "Unsupported or read‑only archive type for writing:" << archivePath; | ||
return false; | ||
} | ||
|
||
if (!archive->open(QIODevice::WriteOnly)) { | ||
qCWarning(karchiveLog) << "Cannot create archive:" << archivePath << "error:" << archive->errorString(); | ||
return false; | ||
} | ||
|
||
// Add contents ---------------------------------------------------- | ||
if (!archive->addLocalDirectory(directoryPath, _archiveRootName(directoryPath))) { | ||
qCWarning(karchiveLog) << "Failed to add directory" << directoryPath << "to archive"; | ||
archive->close(); | ||
return false; | ||
} | ||
|
||
archive->close(); | ||
return true; | ||
} | ||
|
||
bool extractArchive(const QString &archivePath, const QString &outputDirectoryPath) | ||
{ | ||
// Sanity checks -------------------------------------------------- | ||
if (!QFile::exists(archivePath)) { | ||
qCWarning(karchiveLog) << "Archive does not exist:" << archivePath; | ||
return false; | ||
} | ||
|
||
if (!QDir().mkpath(outputDirectoryPath)) { | ||
qCWarning(karchiveLog) << "Cannot create output directory:" << outputDirectoryPath; | ||
return false; | ||
} | ||
|
||
// Select reader --------------------------------------------------- | ||
auto archive = _makeReader(archivePath); | ||
if (!archive) { | ||
qCWarning(karchiveLog) << "Unsupported archive type:" << archivePath; | ||
return false; | ||
} | ||
|
||
if (!archive->open(QIODevice::ReadOnly)) { | ||
qCWarning(karchiveLog) << "Cannot open" << archivePath << "error:" << archive->errorString(); | ||
return false; | ||
} | ||
|
||
// Extract --------------------------------------------------------- | ||
const KArchiveDirectory *root = archive->directory(); | ||
if (!root) { | ||
qCWarning(karchiveLog) << "Failed to obtain root directory from archive"; | ||
archive->close(); | ||
return false; | ||
} | ||
|
||
root->copyTo(outputDirectoryPath, true); | ||
archive->close(); | ||
return true; | ||
} | ||
|
||
} // namespace karchive |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#pragma once | ||
|
||
#include <QtCore/QLoggingCategory> | ||
|
||
Q_DECLARE_LOGGING_CATEGORY(karchiveLog) | ||
|
||
namespace karchive | ||
{ | ||
/// Method to zip files in a given directory | ||
bool createArchive(const QString &directoryPath, const QString &archivePath); | ||
|
||
/// Method to unzip files to a given directory | ||
bool extractArchive(const QString &archivePath, const QString &outputDirectoryPath); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The return value of
copyTo
is ignored; you should check its result, log a warning on failure, and returnfalse
to accurately report extraction errors.Copilot uses AI. Check for mistakes.