Sha256: 12b569346dde8d6544cb23992911d6ae9cef500693bb8d20a6b2df81d6ebc7a3

Contents?: true

Size: 1.75 KB

Versions: 4

Compression:

Stored size: 1.75 KB

Contents

require 'JenkinsUtil/command_line_script'
require 'JenkinsUtil/logger_util'
require 'zip'

class ZipError < Exception
  attr_reader :code, :message

  def initialize(code, message)
    @code = code
    @message = message
  end

  def to_s
    format('code: %d, message: %s', @code, @message)
  end
end

class ZipUtil
  def self.compress(*sources, destination)
    Zip::File.open(destination, ::Zip::File::CREATE) do |zip_archive|
      sources.each do |source|
        write_path_to_archive(zip_archive, source)
      end
    end
  end

  def self.uncompress(source, destination)
    Zip::File.open(source) do |zip_file|
      zip_file.each do |entry|
        path = File.join(destination, entry.name)
        LoggerUtil.log.debug("Extracting #{path}")
        entry.extract(File.join(destination, entry.name))
      end
    end
  end

  def self.write_path_to_archive(zip_archive, disk_path)
    unless File.exist?(disk_path)
      LoggerUtil.log.error("Path does not exist: #{disk_path}")
      exit(-1)
    end

    LoggerUtil.log.debug("Archiving #{disk_path}")

    if File.directory?(disk_path)
      write_directory_to_archive(zip_archive, disk_path)
    else
      write_file_to_archive(zip_archive, disk_path)
    end
  end

  def self.write_directory_to_archive(zip_archive, disk_path)
    zip_archive.mkdir disk_path
    entries = Dir.entries(disk_path) - %w(. ..)

    entries.each do |entry|
      write_path_to_archive(zip_archive, File.join(disk_path, entry))
    end
  end

  def self.write_file_to_archive(zip_archive, disk_path)
    zip_archive.get_output_stream(disk_path) do |output_stream|
      output_stream.write(File.open(disk_path, 'rb').read)
    end
  end

  private_class_method :write_path_to_archive, :write_directory_to_archive, :write_file_to_archive
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
jenkinsutil-1.0.49 lib/JenkinsUtil/zip_util.rb
jenkinsutil-0.8.48 lib/JenkinsUtil/zip_util.rb
jenkinsutil-0.8.47 lib/JenkinsUtil/zip_util.rb
jenkinsutil-0.8.46 lib/JenkinsUtil/zip_util.rb