class WinRM::Transport::TmpZip
A temporary Zip file for a given directory.
@author Fletcher Nichol <fnichol@nichol.ca>
Attributes
@return [Integer] the compression used for Zip entries. Possible
values are `Zlib::BEST_COMPRESSION`, `Zlib::DEFAULT_COMPRESSION`, and `Zlib::NO_COMPRESSION`.
@api private
@return [Pathname] the directory used to create the Zip file @api private
@return [#debug] the logger @api private
@return [Integer] compression method used for Zip entries. Possible
values are `Zip::Entry::DEFLATED` and `Zip::Entry::STORED`.
@api private
@return [IO] the Zip file IO @api private
Public Class Methods
Contructs a new Zip file for the given directory.
There are 2 ways to interpret the directory path:
-
If the directory has no path separator terminator, then the directory basename will be used as the base directory in the resulting zip file.
-
If the directory has a path separator terminator (such as `/` or `\`), then the entries under the directory will be added to the resulting zip file.
The following emaples assume a directory tree structure of:
src |-- alpha.txt |-- beta.txt \-- sub \-- charlie.txt
@example Including the base directory in the zip file
TmpZip.new("/path/to/src") # produces a zip file with entries: # - src/alpha.txt # - src/beta.txt # - src/sub/charlie.txt
@example Excluding the base directory in the zip file
TmpZip.new("/path/to/src/") # produces a zip file with entries: # - alpha.txt # - beta.txt # - sub/charlie.txt
@param dir [String,Pathname,#to_s] path to the directory @param logger [#debug,#debug?] an optional logger/ui object that
responds to `#debug` and `#debug?` (default `nil`)
# File lib/winrm/transport/tmp_zip.rb, line 74 def initialize(dir, logger = nil) @logger = logger @dir = Pathname.new(dir) @method = ::Zip::Entry::DEFLATED @compression = Zlib::BEST_COMPRESSION @zip_io = Tempfile.open(["tmpzip-", ".zip"], :binmode => true) write_zip @zip_io.close end
Public Instance Methods
@return [Pathname] path to zip file
# File lib/winrm/transport/tmp_zip.rb, line 85 def path Pathname.new(zip_io.path) if zip_io.path end
Unlinks (deletes) the zip file from the filesystem.
# File lib/winrm/transport/tmp_zip.rb, line 90 def unlink zip_io.unlink end
Private Instance Methods
@return [Pathname] the path segement to be stripped off Zip entries @api private
# File lib/winrm/transport/tmp_zip.rb, line 121 def dir_strip @dir_strip ||= if dir.to_s.end_with?("/", "\\") Pathname.new(dir.to_s.chop) else dir.dirname end end
@return [Array<Pathname] all recursive files under the base
directory, excluding directories
@api private
# File lib/winrm/transport/tmp_zip.rb, line 132 def entries Pathname.glob(dir.join("**/*")).delete_if(&:directory?).sort end
(see Logging.log_subject
) @api private
# File lib/winrm/transport/tmp_zip.rb, line 138 def log_subject @log_subject ||= [self.class.to_s.split("::").last, path].join("::") end
Adds all file entries to the Zip output stream.
@param zos [Zip::OutputStream] zip output stream @api private
# File lib/winrm/transport/tmp_zip.rb, line 146 def produce_zip_entries(zos) entries.each do |entry| entry_path = entry.sub("#{dir_strip}/", "") debug { "+++ Adding #{entry_path}" } zos.put_next_entry(entry_path, nil, nil, method, compression) entry.open("rb") { |src| IO.copy_stream(src, zos) } end debug { "=== All files added." } end
Writes out a temporary Zip file.
@api private
# File lib/winrm/transport/tmp_zip.rb, line 159 def write_zip debug { "Populating files" } Zip::OutputStream.write_buffer(NoDupIO.new(zip_io)) do |zos| produce_zip_entries(zos) end end