gpt4 book ai didi

c++ - 使用 libarchive 将目录添加到 tarfile

转载 作者:行者123 更新时间:2023-11-28 00:29:56 25 4
gpt4 key购买 nike

我正在尝试将一个目录添加到我正在创建的 tar 存档中。存档已创建,但是其中没有文件。

这是我使用 libarchive 的代码(目录是要压缩的目录路径的 QString。)

struct archive *a;
struct archive_entry *entry;
struct stat st;
char buff[8192];
size_t bytes_read;
int fd;

// the path of the output tarfile
QByteArray outArray = (directory + ".tar").toLocal8Bit();
char *outDirectory = outArray.data();

// the path to the input directory
QByteArray inputArray = directory.toLocal8Bit();
char *inputDirectory = inputArray.data();

QFileInfo inputInfo;
inputInfo.setFile(directory);

// the name of the directory
QByteArray pathArray = inputInfo.fileName().toLocal8Bit();
char *pathDirectory = pathArray.data();

a = archive_write_new();
archive_write_add_filter_gzip(a);
archive_write_set_format_pax_restricted(a);
archive_write_open_filename(a, outDirectory);

entry = archive_entry_new();
stat(inputDirectory, &st);

archive_entry_set_pathname(entry, pathDirectory);
archive_entry_set_filetype(entry, AE_IFDIR);
archive_entry_copy_stat(entry, &st);
archive_write_header(a, entry);

fd = open(inputDirectory, O_RDONLY);
bytes_read = read(fd, buff, sizeof(buff));
while (bytes_read > 0) {
archive_write_data(a, buff, bytes_read);
bytes_read = read(fd, buff, sizeof(buff));
}
close(fd);

archive_write_finish_entry(a);
archive_write_close(a);
archive_write_free(a);

最佳答案

您的代码逻辑不正确。您正在尝试读取目录,而您应该读取目录中的文件......您需要这样的东西来遍历文件:

QDirIterator it("/etc", QDirIterator::Subdirectories);
while (it.hasNext())
qDebug() << it.next();

这是解决您的问题的概念验证代码。

#include <QString>
#include <QByteArray>
#include <QFileInfo>
#include <QDebug>
#include <QDirIterator>

#include <archive.h>
#include <archive_entry.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main()
{
QString directory = "/home/lpapp/tmp/stackoverflow/test";
struct archive *a;
struct archive_entry *entry;
struct stat st;
char buff[8192];
size_t bytes_read;
int fd;

QByteArray outArray = directory.toLocal8Bit() + ".tar";
char *outDirectory = outArray.data();
qDebug() << outDirectory;

QByteArray inputArray = directory.toLocal8Bit();
char *inputDirectory = inputArray.data();
qDebug() << inputDirectory;

QFileInfo inputInfo;
inputInfo.setFile(directory);

// the name of the directory
QByteArray pathArray = inputInfo.fileName().toLocal8Bit();
char *pathDirectory = pathArray.data();
qDebug() << pathDirectory;

a = archive_write_new();
archive_write_add_filter_gzip(a);
archive_write_set_format_pax_restricted(a);
archive_write_open_filename(a, outDirectory);

QDirIterator it(directory, QDirIterator::Subdirectories);
while (it.hasNext()) {
entry = archive_entry_new();
stat(inputDirectory, &st);

archive_entry_set_pathname(entry, it.next().toLocal8Bit().constData());
archive_entry_set_filetype(entry, AE_IFDIR);
archive_entry_copy_stat(entry, &st);
archive_write_header(a, entry);

fd = open(inputDirectory, O_RDONLY);
bytes_read = read(fd, buff, sizeof(buff));
while (bytes_read > 0) {
archive_write_data(a, buff, bytes_read);
bytes_read = read(fd, buff, sizeof(buff));
}
close(fd);
archive_entry_free(entry);

archive_write_finish_entry(a);
archive_write_close(a);
archive_write_free(a);
}

return 0;
}

主程序

TEMPLATE = app
TARGET = main
QT = core
LIBS += -larchive
SOURCES += main.cpp

构建并运行

qmake && make && ./main

关于c++ - 使用 libarchive 将目录添加到 tarfile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23330065/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com