Class: Omnizip::Formats::Tar::Reader

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/omnizip/formats/tar/reader.rb

Overview

TAR archive reader

Reads and extracts TAR archives

Constant Summary

Constants included from Constants

Constants::BLOCK_SIZE, Constants::CHECKSUM_OFFSET, Constants::CHECKSUM_SIZE, Constants::DEVMAJOR_OFFSET, Constants::DEVMAJOR_SIZE, Constants::DEVMINOR_OFFSET, Constants::DEVMINOR_SIZE, Constants::GID_OFFSET, Constants::GID_SIZE, Constants::GNAME_OFFSET, Constants::GNAME_SIZE, Constants::HEADER_SIZE, Constants::LINKNAME_OFFSET, Constants::LINKNAME_SIZE, Constants::MAGIC_OFFSET, Constants::MAGIC_SIZE, Constants::MAX_FILE_SIZE, Constants::MODE_OFFSET, Constants::MODE_SIZE, Constants::MTIME_OFFSET, Constants::MTIME_SIZE, Constants::NAME_OFFSET, Constants::NAME_SIZE, Constants::PREFIX_OFFSET, Constants::PREFIX_SIZE, Constants::SIZE_OFFSET, Constants::SIZE_SIZE, Constants::TYPEFLAG_OFFSET, Constants::TYPEFLAG_SIZE, Constants::TYPE_BLOCK_DEVICE, Constants::TYPE_CHAR_DEVICE, Constants::TYPE_CONTIGUOUS, Constants::TYPE_DIRECTORY, Constants::TYPE_EXTENDED, Constants::TYPE_FIFO, Constants::TYPE_GLOBAL_EXTENDED, Constants::TYPE_GNU_LONGLINK, Constants::TYPE_GNU_LONGNAME, Constants::TYPE_HARD_LINK, Constants::TYPE_REGULAR, Constants::TYPE_SYMLINK, Constants::UID_OFFSET, Constants::UID_SIZE, Constants::UNAME_OFFSET, Constants::UNAME_SIZE, Constants::USTAR_MAGIC, Constants::USTAR_VERSION, Constants::VERSION_OFFSET, Constants::VERSION_SIZE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path) ⇒ Reader

Initialize TAR reader

Parameters:

  • file_path (String)

    Path to TAR archive



17
18
19
20
21
# File 'lib/omnizip/formats/tar/reader.rb', line 17

def initialize(file_path)
  @file_path = file_path
  @entries = []
  @file = nil
end

Instance Attribute Details

#entriesObject (readonly)

Returns the value of attribute entries.



12
13
14
# File 'lib/omnizip/formats/tar/reader.rb', line 12

def entries
  @entries
end

#file_pathObject (readonly)

Returns the value of attribute file_path.



12
13
14
# File 'lib/omnizip/formats/tar/reader.rb', line 12

def file_path
  @file_path
end

Class Method Details

.open(file_path) {|Reader| ... } ⇒ Object

Open TAR archive and yield reader

Parameters:

  • file_path (String)

    Path to TAR archive

Yields:

  • (Reader)

    Reader instance



79
80
81
82
83
84
# File 'lib/omnizip/formats/tar/reader.rb', line 79

def self.open(file_path)
  reader = new(file_path)
  reader.read
  yield reader if block_given?
  reader
end

Instance Method Details

#extract_all(output_dir) ⇒ Object

Extract all entries to a directory

Parameters:

  • output_dir (String)

    Output directory path



37
38
39
40
41
42
43
44
45
# File 'lib/omnizip/formats/tar/reader.rb', line 37

def extract_all(output_dir)
  read unless @entries.any?

  FileUtils.mkdir_p(output_dir)

  @entries.each do |entry|
    extract_entry(entry, output_dir)
  end
end

#extract_entry(entry, output_dir) ⇒ Object

Extract a specific entry

Parameters:

  • entry (Entry)

    Entry to extract

  • output_dir (String)

    Output directory



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/omnizip/formats/tar/reader.rb', line 51

def extract_entry(entry, output_dir)
  full_path = File.join(output_dir, entry.full_name)

  if entry.directory?
    FileUtils.mkdir_p(full_path)
  elsif entry.file?
    FileUtils.mkdir_p(File.dirname(full_path))
    File.binwrite(full_path, entry.data)
    File.chmod(entry.mode, full_path) if entry.mode
    File.utime(entry.mtime, entry.mtime, full_path) if entry.mtime
  elsif entry.symlink?
    FileUtils.mkdir_p(File.dirname(full_path))
    File.symlink(entry.linkname, full_path)
  end
end

#list_entriesArray<Entry>

List all entries

Returns:

  • (Array<Entry>)

    List of entries



70
71
72
73
# File 'lib/omnizip/formats/tar/reader.rb', line 70

def list_entries
  read unless @entries.any?
  @entries
end

#readself

Read TAR archive and parse all entries

Returns:

  • (self)

    Returns self for method chaining



26
27
28
29
30
31
32
# File 'lib/omnizip/formats/tar/reader.rb', line 26

def read
  File.open(@file_path, "rb") do |file|
    @file = file
    parse_entries
  end
  self
end