Class: Zip::FileSystem::ZipFileNameMapper

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/zip/filesystem.rb

Overview

All access to Zip::File from ZipFsFile and ZipFsDir goes through a ZipFileNameMapper, which has one responsibility: ensure

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(zip_file) ⇒ ZipFileNameMapper

Returns a new instance of ZipFileNameMapper.



564
565
566
567
# File 'lib/zip/filesystem.rb', line 564

def initialize(zip_file)
  @zip_file = zip_file
  @pwd = '/'
end

Instance Attribute Details

#pwdObject

Returns the value of attribute pwd.



569
570
571
# File 'lib/zip/filesystem.rb', line 569

def pwd
  @pwd
end

Instance Method Details

#eachObject

Turns entries into strings and adds leading / and removes trailing slash on directories



615
616
617
618
619
# File 'lib/zip/filesystem.rb', line 615

def each
  @zip_file.each do |e|
    yield('/' + e.to_s.chomp('/'))
  end
end

#expand_path(path) ⇒ Object



621
622
623
624
625
626
# File 'lib/zip/filesystem.rb', line 621

def expand_path(path)
  expanded = path.start_with?('/') ? path : ::File.join(@pwd, path)
  expanded.gsub!(/\/\.(\/|$)/, '')
  expanded.gsub!(/[^\/]+\/\.\.(\/|$)/, '')
  expanded.empty? ? '/' : expanded
end

#find_entry(filename) ⇒ Object



571
572
573
# File 'lib/zip/filesystem.rb', line 571

def find_entry(filename)
  @zip_file.find_entry(expand_to_entry(filename))
end

#get_entry(filename) ⇒ Object



575
576
577
# File 'lib/zip/filesystem.rb', line 575

def get_entry(filename)
  @zip_file.get_entry(expand_to_entry(filename))
end

#get_input_stream(filename, &a_proc) ⇒ Object



579
580
581
# File 'lib/zip/filesystem.rb', line 579

def get_input_stream(filename, &a_proc)
  @zip_file.get_input_stream(expand_to_entry(filename), &a_proc)
end

#get_output_stream(filename, permissions = nil, &a_proc) ⇒ Object



583
584
585
586
587
# File 'lib/zip/filesystem.rb', line 583

def get_output_stream(filename, permissions = nil, &a_proc)
  @zip_file.get_output_stream(
    expand_to_entry(filename), permissions, &a_proc
  )
end

#glob(pattern, *flags, &block) ⇒ Object



589
590
591
# File 'lib/zip/filesystem.rb', line 589

def glob(pattern, *flags, &block)
  @zip_file.glob(expand_to_entry(pattern), *flags, &block)
end

#mkdir(filename, permissions = 0o755) ⇒ Object



609
610
611
# File 'lib/zip/filesystem.rb', line 609

def mkdir(filename, permissions = 0o755)
  @zip_file.mkdir(expand_to_entry(filename), permissions)
end

#read(filename) ⇒ Object



593
594
595
# File 'lib/zip/filesystem.rb', line 593

def read(filename)
  @zip_file.read(expand_to_entry(filename))
end

#remove(filename) ⇒ Object



597
598
599
# File 'lib/zip/filesystem.rb', line 597

def remove(filename)
  @zip_file.remove(expand_to_entry(filename))
end

#rename(filename, new_name, &continue_on_exists_proc) ⇒ Object



601
602
603
604
605
606
607
# File 'lib/zip/filesystem.rb', line 601

def rename(filename, new_name, &continue_on_exists_proc)
  @zip_file.rename(
    expand_to_entry(filename),
    expand_to_entry(new_name),
    &continue_on_exists_proc
  )
end