libarchive-ruby icon indicating copy to clipboard operation
libarchive-ruby copied to clipboard

Extract places files where ever ruby was started

Open chris-olszewski opened this issue 10 years ago • 1 comments

Currently if you use #extract, the extracted files go to wherever the ruby process was started. This is an issue if the script is part of a crontab or such. It would be nice if either the method assumed to extract to the same directory the archive is in or if there was an option to set a directory to extract to. Thanks for the gem by the way.

chris-olszewski avatar May 12 '15 03:05 chris-olszewski

An alternative for people using libarchive-ruby

a = Archive.new(@file)
# My archives are a compressed folder
folder = a.each { |e| break File.dirname(e.to_s) }
# Extract to folder of archive
base = File.join(Dir.pwd, File.dirname(@file))
FileUtils.mkdir_p(File.join(base, folder))

a.each do |entry, data| 
  # Go through each entry and data
  path = File.join(base, entry.to_s)
  if path.end_with?('/') 
    # Create Directory if the entry is a directory
    FileUtils.mkdir_p(path)
  else 
    # Or write file to disk if the entry is a file
    File.open(path, 'w+') { |f| f.write(data) }
  end
end

manuelfitz avatar Oct 27 '15 13:10 manuelfitz