libarchive 在某些条目上返回错误,而 7z 可以正常提取
libarchive returns error on some entries while 7z can extract normally
我在使用libarchive
版本3.3.2时遇到问题。我编写了一个程序来读取 7z 存档中的选定条目,如下所示:
file.7z
|__ file.xml
|__ file.fog
|__ file_1.fog
但是,该程序无法读取我大多数档案的file_1.fog
,并且无法读取某些档案的file.fog
。我试图使用archive_error_string()
来查看会发生什么,错误要么corrupted archive
,要么truncated RAR archive
,要么Decompressing internal error
。
这是故障代码:
void list_archive(string name) {
struct archive *a;
struct archive_entry *entry;
// create new archive struct for the file
a = archive_read_new();
archive_read_support_filter_all(a);
archive_read_support_format_all(a);
// open 7z file
int r = archive_read_open_filename(a, name.c_str(), 1024);
if (r != ARCHIVE_OK) {
cout << "cannot read file: " << name << endl;
cout << "read error: " << archive_error_string(a) << endl;
}
// looping through entries
for (;;) {
int status = archive_read_next_header(a, &entry);
// if there's no more header
if (status != ARCHIVE_OK) break;
// print some status messages to stdout
string pathname(archive_entry_pathname(entry));
cout << "working on: " << pathname << endl;
size_t entry_size = archive_entry_size(entry);
// load the entry's content
char * content;
content = (char*)malloc(entry_size);
r = archive_read_data(a, content, entry_size);
// check if archive_read_data was successful
if (r > 0) {
cout << "read " << r << " of " << entry_size << " bytes successfullyn";
// we are interested in .fog file only
if (pathname.back() == 'g') {
// do something with the .fog file
}
}
else // usually the error happens here
if (archive_errno(a) != ARCHIVE_OK) cout << "read error: " << archive_error_string(a) << endl;
// free the content and clear the entry
archive_read_data_skip(a);
free(content);
archive_entry_clear(entry);
cout << "-----" << endl;
}
// we are done with the current archive, free it
r = archive_read_free(a);
if (r != ARCHIVE_OK) {
cout << "Failed to free archive object. Error: " << archive_error_string(a) << endl;
exit(1);
}
}
我找到了麻烦制造者,如果未来的用户遇到同样的问题,我会在这里回答。
int r = archive_read_open_filename(a, name.c_str(), 1024);
显然1024
对于缓冲区大小来说太小了。我将其增加到102400
,并能够读取/提取我的所有档案。
请注意,从技术上讲,缓冲区大小不应破坏功能,降低速度是可以的,但中断操作是不可接受的,因此我认为它处理存档的方式不是那么可靠。
相关文章:
- 从包含m行的文件中提取n行,必要时(惰性地)重复该文件
- 如何从 std::atomic 中提取指针 T<T>?
- 为什么istream不支持右值提取
- 如何设置一个范围来提取我想要获得的信息
- 视觉工作室项目.提取源文件夹名称
- 为什么常量词在重载运算符中不与 ostream 对象一起使用<<?
- C++17 - 使用自定义分配器的节点提取/重新插入 - 适用于 clang++/libc++,但不适用于 libstd
- C 和 C++ 中开关语句的案例标签的常量值,但显示不同的行为
- 从字符串中提取整数并形成一个数组
- C ++中的StringStream有助于使用向量从字符串中提取逗号分隔的整数,而不是空格分隔的整数,为什么?
- asn1c 不会从 asn.1 模块中提取八位字节字符串的默认值
- 从 std::vector<无符号字符>切片中提取 int?
- 在C++中使用重载提取运算符时出现问题
- 为什么我们将单个或多维数组的大小声明为常量值?
- 在 Qt(C++) 中使用 QProcess 解压缩 - 提取目录问题
- C++(.cpp文件和.h文件)拆分代码并添加一个函数,提取 - 这很容易吗?
- 无法从 std::string 中提取C++ Unicode 符号
- 如何从也包含C++字母的文本文件中提取某些数字?
- libarchive 在某些条目上返回错误,而 7z 可以正常提取
- 通过boost python从python中提取常量值