Sha256: 0999a011564147c7cc5d4f56288161f12fbfb684ae77e2e5213ed6b7eec25eb3

Contents?: true

Size: 775 Bytes

Versions: 4

Compression:

Stored size: 775 Bytes

Contents

require "rubygems/package"
require_relative "../reader"

class Metacrunch::File::Reader::TarFileReader
  include Enumerable

  def self.accepts?(filename)
    !!filename[/\.tar\Z|\.tar\.gz\Z|\.tgz\Z/]
  end

  def initialize(filename)
    @filename = filename
  end

  def each
    return enum_for(__method__) unless block_given?

    io =
    if @filename.end_with?("gz") # catches tgz and tar.gz
      Zlib::GzipReader.open(@filename)
    else
      File.open(@filename)
    end

    Gem::Package::TarReader.new(io).each do |_tar_entry|
      unless _tar_entry.directory?
        yield Metacrunch::File.new({
          content: _tar_entry.read,
          entry_name: _tar_entry.full_name,
          file_name: @filename
        })
      end
    end

    io.close
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
metacrunch-2.2.3 lib/metacrunch/file/reader/tar_file_reader.rb
metacrunch-2.2.2 lib/metacrunch/file/reader/tar_file_reader.rb
metacrunch-2.2.1 lib/metacrunch/file/reader/tar_file_reader.rb
metacrunch-2.2.0 lib/metacrunch/file/reader/tar_file_reader.rb