Class: Raykit::Zip

Inherits:
Object
  • Object
show all
Defined in:
lib/raykit/zip.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Zip

Returns a new instance of Zip.



10
11
12
13
14
15
16
# File 'lib/raykit/zip.rb', line 10

def initialize(filename)
  @filename = filename
  @source_dir = Dir.pwd
  @include_globs = []
  @exclude_globs = []
  self
end

Class Method Details

.zip_directory(directory, zipfile_name, overwrite = true) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/raykit/zip.rb', line 64

def self.zip_directory(directory, zipfile_name, overwrite = true)
  File.delete(zipfile_name) if File.exist?(zipfile_name) && overwrite
  ::Zip::File.open(zipfile_name, ::Zip::File::CREATE) do |zipfile|
    Dir[File.join(directory, "**", "**")].each do |file|
      zipfile.add(file.sub(directory + "/", ""), file)
    end
  end
end

Instance Method Details

#exclude_glob(glob) ⇒ Object



28
29
30
31
# File 'lib/raykit/zip.rb', line 28

def exclude_glob(glob)
  @exclude_globs << glob
  self
end

#include_glob(glob) ⇒ Object



23
24
25
26
# File 'lib/raykit/zip.rb', line 23

def include_glob(glob)
  @include_globs << glob
  self
end

#source_dir(dir) ⇒ Object



18
19
20
21
# File 'lib/raykit/zip.rb', line 18

def source_dir(dir)
  @source_dir = dir
  self
end

#zipObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/raykit/zip.rb', line 33

def zip
  path = File.dirname(@filename)
  FileUtils.mkdir_p(path) unless Dir.exist?(path)

  files_to_archive = Array::new()
  Dir.chdir(@source_dir) do
    #include_files = []
    @include_globs.each do |include_glob|
      Dir.glob(include_glob) { |f|
        #puts "\n" + f
        files_to_archive << f
      }
    end
  end

  ::Zip::File::open(@filename, ::Zip::File::CREATE) { |zipfile|
    count = 0
    files_to_archive.each do |file|
      zipfile.get_output_stream(file) { |f|
        fr = ::File.open("#{@source_dir}/#{file}", "rb")
        f.puts(fr.read)
        fr.close()
        f.close()
        count = count + 1
      }
    end
    zipfile.close
    puts "  added " << count.to_s << " files to " << @filename
  }
end