Module: ZipUtils
- Defined in:
- lib/folio/ziputils.rb
Overview
ZipUtils
Function module for compression methods.
TODO: Much of this shells out. It would be best to internalize.
Defined Under Namespace
Modules: DryRun, NoWrite, Verbose
Constant Summary collapse
- COMPRESS_FORMAT =
{ '.tar.gz' => 'tar_gzip', '.tgz' => 'tar_gzip', '.tar.bz2' => 'tar_bzip2', '.zip' => 'zip' }
Class Method Summary collapse
- .bzip2(file, option = {}) ⇒ Object (also: bzip)
-
.compress(folder, file, options = {}) ⇒ Object
Compress folder or file based on given extension.
- .gzip(file, option = {}) ⇒ Object
- .tar(folder, file = nil, options = {}) ⇒ Object
-
.tar_bzip2(folder, file = nil, options = {}) ⇒ Object
(also: tar_bzip, tar_j)
Tar Bzip2.
-
.tar_gzip(folder, file = nil, options = {}) ⇒ Object
(also: tar_z)
Tar Gzip.
- .unbzip2(file, options = {}) ⇒ Object (also: unbzip)
- .ungzip(file, options = {}) ⇒ Object
- .untar(file, options = {}) ⇒ Object
-
.untar_bzip2(file, options = {}) ⇒ Object
(also: untar_bzip, untar_j)
Untar Bzip2.
-
.untar_gzip(file, options = {}) ⇒ Object
(also: untar_z)
Untar Gzip.
-
.unzip(file, options = {}) ⇒ Object
Unzip.
-
.zip(folder, file = nil, options = {}) ⇒ Object
Zip.
Class Method Details
.bzip2(file, option = {}) ⇒ Object Also known as: bzip
69 70 71 72 73 74 |
# File 'lib/folio/ziputils.rb', line 69 def bzip2(file, option={}) cmd = "bzip2 #{file}" puts cmd if [:dryrun] or [:verbose] system cmd unless [:dryrun] or [:noop] return File.(file + '.bz2') end |
.compress(folder, file, options = {}) ⇒ Object
Compress folder or file based on given extension. Supported extensions are:
-
.tar.gz
-
.tgz
-
.tar.bz2
-
.zip
TODO: support gzip and bzip2 as well.
30 31 32 33 34 35 36 37 |
# File 'lib/folio/ziputils.rb', line 30 def compress(folder, file, ={}) format = COMPRESS_FORMAT[File.extname(file)] if format send(format, folder, file, ) else raise ArgumentError, "unknown compression format -- #{format_extension}" end end |
.gzip(file, option = {}) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/folio/ziputils.rb', line 41 def gzip(file, option={}) require 'zlib' fname = File.basename(file) + '.gz' if [:dryrun] or [:verbose] puts "gzip #{file}" end Zlib::GzipWriter.open(fname) do |gz| gz.write(File.read(file)) end unless [:dryrun] or [:noop] return File.(fname) end |
.tar(folder, file = nil, options = {}) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/folio/ziputils.rb', line 91 def tar(folder, file=nil, ={}) require 'folio/minitar' file ||= File.basename(File.(folder)) + '.tar' cmd = "tar -cf #{file} #{folder}" puts cmd if [:verbose] or [:dryrun] unless [:noop] or [:dryrun] gzIO = File.open(file, 'wb') Archive::Tar::Minitar.pack(folder, gzIO) end return File.(file) end |
.tar_bzip2(folder, file = nil, options = {}) ⇒ Object Also known as: tar_bzip, tar_j
Tar Bzip2
150 151 152 153 154 155 156 157 |
# File 'lib/folio/ziputils.rb', line 150 def tar_bzip2(folder, file=nil, ={}) # name of file to create file ||= File.basename(File.(folder)) + '.tar.bz2' cmd = "tar --bzip2 -cf #{file} #{folder}" puts cmd if [:dryrun] or [:verbose] system cmd unless [:dryrun] or [:noop] return File.(file) end |
.tar_gzip(folder, file = nil, options = {}) ⇒ Object Also known as: tar_z
Tar Gzip
119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/folio/ziputils.rb', line 119 def tar_gzip(folder, file=nil, ={}) require 'zlib' require 'folio/minitar' file ||= File.basename(File.(folder)) + '.tar.gz' # '.tgz' which ? cmd = "tar --gzip -czf #{file} #{folder}" puts cmd if [:verbose] or [:dryrun] unless [:noop] or [:dryrun] gzIO = Zlib::GzipWriter.new(File.open(file, 'wb')) Archive::Tar::Minitar.pack(folder, gzIO) end return File.(file) end |
.unbzip2(file, options = {}) ⇒ Object Also known as: unbzip
80 81 82 83 84 85 |
# File 'lib/folio/ziputils.rb', line 80 def unbzip2(file, ={}) cmd = "unbzip2 #{file}" puts cmd if [:dryrun] or [:verbose] system cmd unless [:dryrun] or [:noop] return File.(file.chomp(File.extname(file))) end |
.ungzip(file, options = {}) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/folio/ziputils.rb', line 55 def ungzip(file, ={}) require 'zlib' fname = File.basename(file).chomp(File.extname(file)) if [:dryrun] or [:verbose] puts "ungzip #{file}" end Zlib::GzipReader.open(file) do |gz| File.open(fname, 'wb'){ |f| f << gz.read } end unless [:dryrun] or [:noop] return File.(fname) end |
.untar(file, options = {}) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/folio/ziputils.rb', line 105 def untar(file, ={}) require 'folio/minitar' #file ||= File.basename(File.expand_path(folder)) + '.tar' cmd = "untar #{file}" puts cmd if [:verbose] or [:dryrun] unless [:noop] or [:dryrun] gzIO = File.open(file, 'wb') Archive::Tar::Minitar.unpack(gzIO) end return File.(file) end |
.untar_bzip2(file, options = {}) ⇒ Object Also known as: untar_bzip, untar_j
Untar Bzip2
165 166 167 168 169 |
# File 'lib/folio/ziputils.rb', line 165 def untar_bzip2(file, ={}) cmd = "tar --bzip2 -xf #{file}" puts cmd if [:dryrun] or [:verbose] system cmd unless [:dryrun] or [:noop] end |
.untar_gzip(file, options = {}) ⇒ Object Also known as: untar_z
Untar Gzip
TODO: Write unified untar_gzip function.
142 143 144 |
# File 'lib/folio/ziputils.rb', line 142 def untar_gzip(file, ={}) untar(ungzip(file, ), ) end |
.unzip(file, options = {}) ⇒ Object
Unzip
188 189 190 191 192 |
# File 'lib/folio/ziputils.rb', line 188 def unzip(file, ={}) cmd = "unzip #{file}" puts cmd if [:dryrun] or [:verbose] system cmd unless [:dryrun] or [:noop] end |
.zip(folder, file = nil, options = {}) ⇒ Object
Zip
177 178 179 180 181 182 183 184 |
# File 'lib/folio/ziputils.rb', line 177 def zip(folder, file=nil, ={}) raise ArgumentError if folder == '.*' file ||= File.basename(File.(folder)) + '.zip' cmd = "zip -rqu #{file} #{folder}" puts cmd if [:dryrun] or [:verbose] system cmd unless [:dryrun] or [:noop] return File.(file) end |