Skip to content

Extract places files where ever ruby was started #12

Open

Description

@chris-olszewski

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.

Activity

  1. manuelfitz commented on Oct 27, 2015

    @manuelfitz

    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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions