class Webgen::Source::TarArchive
This class is used to read source paths from a (gzipped) tar archive. The archive can be remote (http(s) or ftp) or local.
For example, the following are all valid URIs:
http://example.com/directory/file.tgz /home/test/my.tar.gz ftp://ftp.example.com/archives/archive.tar
Attributes
glob[R]
The glob (see File.fnmatch for details) that is used to specify which paths in the archive should be returned by paths
.
uri[R]
The URI of the tar archive.
Public Class Methods
new(website, uri, glob = '**/*')
click to toggle source
Create a new tar archive source for the URI string uri
.
# File lib/webgen/source/tar_archive.rb 31 def initialize(website, uri, glob = '**/*') 32 @uri = uri 33 @glob = glob 34 end
Public Instance Methods
paths()
click to toggle source
Return all paths in the tar archive available at uri
.
# File lib/webgen/source/tar_archive.rb 37 def paths 38 if !defined?(@paths) 39 stream = (RUBY_VERSION < '2.5' ? open(@uri) : URI.open(@uri)) 40 stream = Zlib::GzipReader.new(stream) if @uri.to_s =~ /(\.tar\.gz|\.tgz)$/ 41 Archive::Tar::Minitar::Input.open(stream) do |input| 42 @paths = input.collect do |entry| 43 path = entry.full_name 44 next unless File.fnmatch(@glob, path, File::FNM_DOTMATCH|File::FNM_CASEFOLD|File::FNM_PATHNAME) 45 path += '/' if entry.directory? && path[-1] != ?/ 46 path = '/' + path unless path[0] == ?/ 47 data = entry.read.to_s 48 Path.new(path, 'modified_at' => Time.at(entry.mtime)) {|mode| StringIO.new(data, mode) } 49 end.compact.to_set 50 end 51 end 52 @paths 53 end