Methods

Included Modules

Archive::Zip::Codec::TraditionalEncryption::Encrypt

Archive::Zip::Codec::TraditionalEncryption::Encrypt is a writable, IO-like object which encrypts data written to it using the traditional encryption algorithm as documented in the ZIP specification and writes the result to a delegate IO object. A close method is also provided which can optionally close the delegate object.

Instances of this class should only be accessed via the Archive::Zip::Codec::TraditionalEncryption#compressor method.

Public Class Methods

new(io, password, mtime) click to toggle source

Creates a new instance of this class using io as a data sink. io must be writable and must provide a write method as IO does or errors will be raised when performing write operations. password should be the encryption key. mtime must be the last modified time of the entry to be encrypted/decrypted.

The flush_size attribute is set to 0 by default under the assumption that io is already buffered.

# File lib/archive/zip/codec/traditional_encryption.rb, line 108
def initialize(io, password, mtime)
  # Keep track of the total number of bytes written.
  # Set this here so that the call to #initialize_keys caused by the call
  # to super below does not cause errors in #unbuffered_write due to this
  # attribute being uninitialized.
  @total_bytes_in = 0

  # This buffer is used to hold the encrypted version of the string most
  # recently sent to #unbuffered_write.
  @encrypt_buffer = ''

  super(io, password, mtime)

  # Assume that the delegate IO object is already buffered.
  self.flush_size = 0
end
open(io, password, mtime) click to toggle source

Creates a new instance of this class with the given argument using new and then passes the instance to the given block. The close method is guaranteed to be called after the block completes.

Equivalent to new if no block is given.

# File lib/archive/zip/codec/traditional_encryption.rb, line 89
def self.open(io, password, mtime)
  encrypt_io = new(io, password, mtime)
  return encrypt_io unless block_given?

  begin
    yield(encrypt_io)
  ensure
    encrypt_io.close unless encrypt_io.closed?
  end
end

Public Instance Methods

close(close_delegate = true) click to toggle source

Closes the stream after flushing the encryption buffer to the delegate. If close_delegate is true, the delegate object used as a data sink will also be closed using its close method.

Raises IOError if called more than once.

# File lib/archive/zip/codec/traditional_encryption.rb, line 130
def close(close_delegate = true)
  flush()
  begin
    until @encrypt_buffer.empty? do
      @encrypt_buffer.slice!(0, io.write(@encrypt_buffer))
    end
  rescue Errno::EAGAIN, Errno::EINTR
    retry if write_ready?
  end

  super()
  io.close if close_delegate
  nil
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.