Class: Zippo::ZipMember

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/zippo/zip_member.rb

Overview

A member of a Zip archive file.

Instance Method Summary collapse

Constructor Details

#initialize(io, header) ⇒ ZipMember

Returns a new instance of ZipMember.



10
11
12
13
# File 'lib/zippo/zip_member.rb', line 10

def initialize(io, header)
  @io = io
  @header = header
end

Instance Method Details

#directory?Boolean

Returns True if the member is a directory, False otherwise.

Returns:

  • (Boolean)

    True if the member is a directory, False otherwise



22
23
24
# File 'lib/zippo/zip_member.rb', line 22

def directory?
  name.end_with? '/'
end

#nameString

Returns the name of the member.

Returns:

  • (String)

    the name of the member



16
17
18
# File 'lib/zippo/zip_member.rb', line 16

def name
  @name ||= @header.name
end

#readString

Reads (and possibly uncompresses) the member’s data

Returns:

  • (String)

    the uncompressed member data



32
33
34
35
# File 'lib/zippo/zip_member.rb', line 32

def read
  seek_to_compressed_data
  uncompressor.uncompress
end

#with_name(name) ⇒ ZipMember

Duplicates this zip member and overrides the name.

Parameters:

  • name (String)

    the name to use

Returns:



41
42
43
44
45
# File 'lib/zippo/zip_member.rb', line 41

def with_name(name)
  dup.tap do |obj|
    obj.instance_variable_set :@name, name
  end
end

#write_to(out, preferred_method = Filter::DeflateCompressor::METHOD, recompress = false) ⇒ Integer

Writes the member data to the specified IO using the specified compression method.

Parameters:

  • out (IO)

    the IO to write to

  • preferred_method (Integer) (defaults to: Filter::DeflateCompressor::METHOD)

    the compression method to use

  • recompress (Boolean) (defaults to: false)

    whether or not to recompress the data

Returns:

  • (Integer, Integer, Integer)

    the amount written, the original size of the data, the crc32 of the data



56
57
58
59
60
61
62
63
64
# File 'lib/zippo/zip_member.rb', line 56

def write_to(out, preferred_method = Filter::DeflateCompressor::METHOD, recompress = false)
  seek_to_compressed_data
  if recompress
    Filter::Compressor.for(preferred_method).new(uncompressor).compress_to(out)
  else
    IO.copy_stream @io, out, @header.compressed_size
    return @header.compressed_size, @header.uncompressed_size, @header.crc32
  end
end