Ffi-libarchive: how to extract and overwrite existing files?

I am using the library chef/ffi-libarchive in RoR. I want to extract a compressed file, and if there is already a file with the same name, I want it to be overwritten. I've noticed that there is a flag EXTRACT_NO_OVERWRITE, so I guess overwritting is the default behavior. But in my experiment, when I only use the flag EXTRACT_PERM, it keeped the old file and discard the new file. So what is the proper way to extract and overwrite? Thanks in advance.

Rather than referencing the library directly, have you tried the archive_file resource that extracts archived files (I believe using ffi-libarchive under the hood).

Here's the link to the documentation:
https://docs.chef.io/resources/archive_file

But you would basically create a code block like this:

archive_file 'myfile.zip' do
  path '/tmp/myfile.zip'
  destination '/srv/myfiles'
  overwrite true
end

You could also use overwrite auto to have the resource check the destination vs. the files in the archive to see if the files in the archive are newer or not and only overwrite when the files in the archive are newer than those in the destination.

Unfortunately I'm not familiar with working with the library directly, but if possible I recommend using the built-in resources if they'll do the job.

Thanks,
~Davin

From its Quick Start, it seems that Chef Infra is a huge library. But I only want its archive_file feature, how can I isolate the feature and use it in a ruby program? Thanks.

I think you're interested by the code here which define the chef resource as inspiration to mimic it:

chef/archive_file.rb at master · chef/chef · GitHub

This code handle the permissions/ownership separately than the extraction itself.