【发布时间】:2014-06-13 08:22:19
【问题描述】:
我正在尝试将目录添加到我正在创建的 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);
【问题讨论】:
-
为什么不使用 Qt 世界中的归档库,例如 Quazip 或 KDE 中的库?此外,您需要
directory.toLocal8Bit() + ".tar"而不是(directory + ".tar").toLocal8Bit()。另外,我无法想象您为什么不使用 QFile 进行读取。
标签: c++ qt libarchive