首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在目录路径而不是文件列表上创建LibArchive存档?

如何在目录路径而不是文件列表上创建LibArchive存档?
EN

Stack Overflow用户
提问于 2020-12-02 09:02:45
回答 1查看 246关注 0票数 4

我以前从未使用过LibArchive,正在尝试使用它来创建目录的.tar归档,因为它安装在我的远程计算实例上。

LibArchive有一个如何从文件的平面列表创建存档的示例(如下所示)。但是我找不到任何关于如何从目录路径实际创建归档的示例。在从同一网站“提取”example的过程中,似乎所有内容都包含在"archive_entry“结构中,但似乎没有对应的Create。

代码语言:javascript
运行
复制
void
write_archive(const char *outname, const char **filename)
{
  struct archive *a;
  struct archive_entry *entry;
  struct stat st;
  char buff[8192];
  int len;
  int fd;

  a = archive_write_new();
  archive_write_add_filter_gzip(a);
  archive_write_set_format_pax_restricted(a); // Note 1
  archive_write_open_filename(a, outname);
  while (*filename) {
    stat(*filename, &st);
    entry = archive_entry_new(); // Note 2
    archive_entry_set_pathname(entry, *filename);
    archive_entry_set_size(entry, st.st_size); // Note 3
    archive_entry_set_filetype(entry, AE_IFREG);
    archive_entry_set_perm(entry, 0644);
    archive_write_header(a, entry);
    fd = open(*filename, O_RDONLY);
    len = read(fd, buff, sizeof(buff));
    while ( len > 0 ) {
        archive_write_data(a, buff, len);
        len = read(fd, buff, sizeof(buff));
    }
    close(fd);
    archive_entry_free(entry);
    filename++;
  }
  archive_write_close(a); // Note 4
  archive_write_free(a); // Note 5
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-12 04:50:49

libtar api中的archive_read_disk_descend函数允许您遍历目录。应该在archive_write_header(a,entry)之前调用它;要在启动for循环之前使用此函数,您需要调用struct archive *disk = archive_read_disk_new(),以便创建一个表示磁盘的归档文件。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65100774

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档