module Grizzled::FileUtil::ZipUtil::ZipMixin
ZipMixin
provides convenient front-end methods for zipping
files; it uses the 'rubyzip' gem under the covers, so you must have
'rubyzip' installed to use this class. Mixing this module into your
class mixes in methods that will allow you to zip up files. Related modules
and classes:
- Grizzled::FileUtil::Zipper
-
A class that includes this module and can be instantiated by itself.
- Grizzled::FileUtil::UnzipMixin
-
A module mixin for unzipping zip files.
- Grizzled::FileUtil::Unzipper
-
A class that includes `UnzipMixin' and can be instantiated by itself.
Public Instance Methods
Create a zip file from the contents of a directory.
Parameters:
- zip_file
-
The zip file to open. The file is created if it doesn't already exists.
- directory
-
The directory whose contents are to be included in the file.
- options
-
Options hash, as described below.
- select
-
If a block (
select
) is given, thenzip
passes each file or directory to the block and only adds the entry to the zip file if the block returnstrue
. If no block is given, then all files and directories are added (subject also to the:recursive
option, below).
Options:
*NOTE*: Options are symbols (e.g.,
:recursive
).
- recursive
-
If
false
, only zip the files in the directory; iftrue
(the default), recursively zip the entire directory. - dir_at_top
-
If
false
, don't include zip the directory itself (i.e., the top-level files will be at the top level of the zip file). Iftrue
(the default), the the directory itself (the basename) will be the top-level element of the zip file. - recreate
-
If
true
, remove the zip file if it exists already, so it's recreated from scratch. Iffalse
(the default), don't recreate the zip file if it doesn't exist; instead, update the existing file.
Returns:
The zip_file
argument, for convenience.
Example:
require 'grizzled/fileutil/ziputil' import 'tmpdir' include Grizzled::FileUtil::ZipUtil::ZipMixin Dir.mktmpdir do |d| zip zipfile_path, d end
# File lib/grizzled/fileutil/ziputil.rb, line 114 def zip(zip_file, directory, options = {}, &select) recurse = options.fetch(:recursive, true) dir_at_top = options.fetch(:dir_at_top, true) recreate = options.fetch(:recreate, true) if dir_at_top abs_dir = File.expand_path(directory) entry_dir = File.basename(abs_dir) chdir_to = File.dirname(abs_dir) glob = File.join(entry_dir, '**', '*').to_s else chdir_to = directory entry_dir = '.' glob = File.join('**', '*').to_s end # Remove the existing zip file, if asked to do so. FileUtils::rm_f zip_file if recreate # Open the zip file. Then, CD to the appropriate directory # and pack it up. zip = Zip::ZipFile.open(zip_file, Zip::ZipFile::CREATE) FileUtils::cd chdir_to do |d| Dir[glob].each do |path| # If the caller supplied a block, only add the file if the block # says we can. add = block_given? ? select.call(path) : true if add if File.directory? path zip.mkdir path if recurse else zip.add(path, path) if (File.dirname(path) == '.') or recurse end end end zip.close end zip_file end