Class: Sprout::ZipUtil
- Inherits:
-
Object
- Object
- Sprout::ZipUtil
- Defined in:
- lib/sprout/zip_util.rb
Overview
:nodoc:
Class Method Summary collapse
- .add_file_to_zip(zip, path, excludes) ⇒ Object
- .excluded?(str, excludes) ⇒ Boolean
-
.pack(input, archive, excludes) ⇒ Object
Pack up an archive from a directory on disk.
-
.unpack(archive, destination) ⇒ Object
Unpack an archive to a directory on disk.
- .unpack_file(zip, destination, path = '') ⇒ Object
Class Method Details
.add_file_to_zip(zip, path, excludes) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/sprout/zip_util.rb', line 34 def self.add_file_to_zip(zip, path, excludes) if(File.directory?(path)) zip.dir.mkdir(path) Dir.open(path).each do |child| if(!excluded?(child, excludes)) add_file_to_zip(zip, File.join(path, child), excludes) end end else File.open(path) do |src| zip.file.open(path, 'w') do |dest| dest.write src.read end end end end |
.excluded?(str, excludes) ⇒ Boolean
51 52 53 54 55 56 57 58 |
# File 'lib/sprout/zip_util.rb', line 51 def self.excluded?(str, excludes) excludes.each do |exc| if(str == exc) return true end end return false end |
.pack(input, archive, excludes) ⇒ Object
Pack up an archive from a directory on disk
6 7 8 9 10 |
# File 'lib/sprout/zip_util.rb', line 6 def self.pack(input, archive, excludes) Zip::ZipFile.open(archive, Zip::ZipFile::CREATE) do |zip| add_file_to_zip(zip, input, excludes) end end |
.unpack(archive, destination) ⇒ Object
Unpack an archive to a directory on disk
13 14 15 16 17 |
# File 'lib/sprout/zip_util.rb', line 13 def self.unpack(archive, destination) Zip::ZipFile.open(archive) do |zip| unpack_file(zip, destination) end end |
.unpack_file(zip, destination, path = '') ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/sprout/zip_util.rb', line 19 def self.unpack_file(zip, destination, path='') if(zip.file.file?(path)) File.open(File.join(destination, path), 'w') do |dest| zip.file.open(path) do |src| dest.write src.read end end else Dir.mkdir(File.join(destination, path)) zip.dir.foreach(path) do |dir| unpack_file(zip, destination, File.join(path, dir)) end end end |