title: Adding directory to tarfile with libarchive
body: <p>I am trying to add a directory to a tar archive that I am creating. The archive is created, however, it has no files in it.</p>
  
  <p>Here's the code I have using libarchive (directory is a QString of the path to the directory to be tarred.)</p>
  
  <pre><code>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, &amp;st);
  
  archive_entry_set_pathname(entry, pathDirectory);
  archive_entry_set_filetype(entry, AE_IFDIR);
  archive_entry_copy_stat(entry, &amp;st);
  archive_write_header(a, entry);
  
  fd = open(inputDirectory, O_RDONLY);
  bytes_read = read(fd, buff, sizeof(buff));
  while (bytes_read &gt; 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);
  </code></pre>