Class: ZipContainer::File

Inherits:
Container show all
Extended by:
Forwardable
Defined in:
lib/zip-container/file.rb

Overview

This class represents a ZipContainer file in PK Zip format. See the OCF and UCF specifications for more details.

This class provides most of the facilities of the Zip::File class in the rubyzip gem. Please also consult the rubyzip documentation alongside these pages.

There are code examples available with the source code of this library.

Constant Summary

Constants inherited from Container

Container::MIMETYPE_FILE

Instance Attribute Summary

Attributes inherited from Container

#mimetype

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Container

open, verify, #verify, verify!, #verify!, verify?, #verify?

Methods included from ManagedEntries

#hidden_directory?, #hidden_entry?, #hidden_file?, #managed_directories, #managed_directory?, #managed_directory_names, #managed_entries, #managed_entry?, #managed_entry_names, #managed_file?, #managed_file_names, #managed_files, #verify_managed_entries, #verify_managed_entries!

Methods included from ReservedNames

#reserved_entry?, #reserved_names

Constructor Details

#initialize(location) ⇒ File

:stopdoc:



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/zip-container/file.rb', line 61

def initialize(location)
  super(location)

  @on_disk = true

  # Here we fake up the connection to the rubyzip filesystem classes so
  # that they also respect the reserved names that we define.
  mapped_zip = ::Zip::FileSystem::ZipFileNameMapper.new(self)
  @fs_dir  = ::Zip::FileSystem::Dir.new(mapped_zip)
  @fs_file = ::Zip::FileSystem::File.new(mapped_zip)
  @fs_dir.file = @fs_file
  @fs_file.dir = @fs_dir
end

Class Method Details

.create(filename, mimetype) ⇒ Object

:call-seq:

File.create(filename, mimetype) -> container
File.create(filename, mimetype) {|container| ...}

Create a new ZipContainer file on disk with the specified mimetype.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/zip-container/file.rb', line 81

def self.create(filename, mimetype)
  # The mimetype file must be first in the archive, and uncompressed.
  mimetype_entry = Zip::Entry.new(
    nil, MIMETYPE_FILE, compression_method: Zip::Entry::STORED
  )

  Zip::OutputStream.open(filename) do |stream|
    stream.put_next_entry(mimetype_entry)
    stream.write mimetype
  end

  # Now open the newly created container.
  c = new(filename)

  if block_given?
    begin
      yield c
    ensure
      c.close
    end
  end

  c
end

.each_entry(filename, &block) ⇒ Object

:call-seq:

File.each_entry -> Enumerator
File.each_entry {|entry| ...}

Iterate over the entries in the ZipContainer file. The entry objects returned by this method are Zip::Entry objects. Please see the rubyzip documentation for details.



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/zip-container/file.rb', line 113

def self.each_entry(filename, &block)
  c = new(filename)

  if block
    begin
      c.each(&block)
    ensure
      c.close
    end
  end

  c.each
end

Instance Method Details

#add(entry, src_path, &continue_on_exists_proc) ⇒ Object

:call-seq:

add(entry, src_path, &continue_on_exists_proc)

Convenience method for adding the contents of a file to the ZipContainer file. If asked to add a file with a reserved name, such as the special mimetype header file, this method will raise a ReservedNameClashError.

See the rubyzip documentation for details of the continue_on_exists_proc parameter.



137
138
139
140
141
142
143
# File 'lib/zip-container/file.rb', line 137

def add(entry, src_path, &continue_on_exists_proc)
  if reserved_entry?(entry) || managed_directory?(entry)
    raise ReservedNameClashError, entry.to_s
  end

  @container.add(entry, src_path, &continue_on_exists_proc)
end

#commitObject Also known as: close

:call-seq:

commit -> boolean
close -> boolean

Commits changes that have been made since the previous commit to the ZipContainer file. Returns false if no commit was required, true otherwise.



152
153
154
155
156
157
158
# File 'lib/zip-container/file.rb', line 152

def commit
  return false unless commit_required?

  @container.commit if on_disk?

  true
end

#dirObject

:call-seq:

dir -> Zip::FileSystem::Dir

Returns an object which can be used like ruby’s built in Dir (class) object, except that it works on the ZipContainer file on which this method is invoked.

See the rubyzip documentation for details.



170
171
172
# File 'lib/zip-container/file.rb', line 170

def dir
  @fs_dir
end

#fileObject

:call-seq:

file -> Zip::FileSystem::File

Returns an object which can be used like ruby’s built in File (class) object, except that it works on the ZipContainer file on which this method is invoked.

See the rubyzip documentation for details.



182
183
184
# File 'lib/zip-container/file.rb', line 182

def file
  @fs_file
end

#find_entry(entry_name, options = {}) ⇒ Object

:call-seq:

find_entry(entry_name, options = {}) -> Zip::Entry or nil

Searches for the entry with the specified name. Returns nil if no entry is found or if the specified entry is hidden for normal use. You can specify :include_hidden => true to include hidden entries in the search.



193
194
195
196
197
198
199
# File 'lib/zip-container/file.rb', line 193

def find_entry(entry_name, options = {})
  options = { include_hidden: false }.merge(options)

  return if !options[:include_hidden] && hidden_entry?(entry_name)

  @container.find_entry(entry_name)
end

#get_entry(entry, options = {}) ⇒ Object

:call-seq:

get_entry(entry, options = {}) -> Zip::Entry or nil

Searches for an entry like find_entry, but throws Errno::ENOENT if no entry is found or if the specified entry is hidden for normal use. You can specify :include_hidden => true to include hidden entries in the search.

Raises:

  • (Errno::ENOENT)


208
209
210
211
212
213
214
# File 'lib/zip-container/file.rb', line 208

def get_entry(entry, options = {})
  options = { include_hidden: false }.merge(options)

  raise Errno::ENOENT, entry if !options[:include_hidden] && hidden_entry?(entry)

  @container.get_entry(entry)
end

#get_output_stream(entry, permissions: nil, &block) ⇒ Object

:call-seq:

get_output_stream(entry, permissions: nil) -> stream
get_output_stream(entry, permissions: nil) {|stream| ...}

Returns an output stream to the specified entry. If a block is passed the stream object is passed to the block and the stream is automatically closed afterwards just as with ruby’s built-in File.open method.

See the rubyzip documentation for details of the permission_int parameter.



226
227
228
229
230
231
232
# File 'lib/zip-container/file.rb', line 226

def get_output_stream(entry, permissions: nil, &block)
  if reserved_entry?(entry) || managed_directory?(entry)
    raise ReservedNameClashError, entry.to_s
  end

  @container.get_output_stream(entry, permissions: permissions, &block)
end

#glob(pattern, *params) ⇒ Object

:call-seq:

glob(pattern) -> Array
glob(pattern) { |entry| ... }
glob(pattern, *parameters) -> Array
glob(pattern, *parameters) { |entry| ... }

Searches for entries given a glob. Hidden files are ignored by default.

The parameters that can be supplied are:

  • flags - A bitwise OR of the FNM_xxx parameters defined in File::Constants. The default value is ::File::FNM_PATHNAME | ::File::FNM_DOTMATCH

  • options - :include_hidden => true will include hidden entries in the search.



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/zip-container/file.rb', line 248

def glob(pattern, *params) # rubocop:disable Metrics/CyclomaticComplexity
  flags = ::File::FNM_PATHNAME | ::File::FNM_DOTMATCH
  options = { include_hidden: false }

  params.each do |param|
    case param
    when Hash
      options = options.merge(param)
    else
      flags = param
    end
  end

  entries.filter_map do |entry|
    next if !options[:include_hidden] && hidden_entry?(entry)
    next unless ::File.fnmatch(pattern, entry.name.chomp('/'), flags)

    yield(entry) if block_given?
    entry
  end
end

#in_memory?Boolean

:call-seq:

in_memory? -> boolean

Is this ZipContainer file memory resident as opposed to stored on disk?

Returns:

  • (Boolean)


274
275
276
# File 'lib/zip-container/file.rb', line 274

def in_memory?
  !@on_disk
end

#mkdir(name, permission = 0o0755) ⇒ Object

:call-seq:

mkdir(name, permission = 0755)

Creates a directory in the ZipContainer file. If asked to create a directory with a name reserved for use by a file this method will raise a ReservedNameClashError.

The new directory will be created with the supplied unix-style permissions. The default (0755) is owner read, write and list; group read and list; and world read and list.



288
289
290
291
292
# File 'lib/zip-container/file.rb', line 288

def mkdir(name, permission = 0o0755)
  raise ReservedNameClashError, name if reserved_entry?(name) || managed_file?(name)

  @container.mkdir(name, permission)
end

#on_disk?Boolean

:call-seq:

on_disk? -> boolean

Is this ZipContainer file stored on disk as opposed to memory resident?

Returns:

  • (Boolean)


298
299
300
# File 'lib/zip-container/file.rb', line 298

def on_disk?
  @on_disk
end

#remove(entry) ⇒ Object

:call-seq:

remove(entry)

Removes the specified entry from the ZipContainer file. If asked to remove any reserved files such as the special mimetype header file this method will do nothing.



308
309
310
311
312
# File 'lib/zip-container/file.rb', line 308

def remove(entry)
  return if reserved_entry?(entry)

  @container.remove(entry)
end

#rename(entry, new_name, &continue_on_exists_proc) ⇒ Object

:call-seq:

rename(entry, new_name, &continue_on_exists_proc)

Renames the specified entry in the ZipContainer file. If asked to rename any reserved files such as the special mimetype header file this method will do nothing. If asked to rename a file to one of the reserved names a ReservedNameClashError is raised.

See the rubyzip documentation for details of the continue_on_exists_proc parameter.



324
325
326
327
328
329
# File 'lib/zip-container/file.rb', line 324

def rename(entry, new_name, &continue_on_exists_proc)
  return if reserved_entry?(entry)
  raise ReservedNameClashError, new_name if reserved_entry?(new_name)

  @container.rename(entry, new_name, &continue_on_exists_proc)
end

#replace(entry, src_path) ⇒ Object

:call-seq:

replace(entry, src_path)

Replaces the specified entry of the ZipContainer file with the contents of src_path (from the file system). If asked to replace any reserved files such as the special mimetype header file this method will do nothing.



338
339
340
341
342
# File 'lib/zip-container/file.rb', line 338

def replace(entry, src_path)
  return if reserved_entry?(entry)

  @container.replace(entry, src_path)
end

#to_sObject

:call-seq:

to_s -> String

Return a textual summary of this ZipContainer file.



348
349
350
# File 'lib/zip-container/file.rb', line 348

def to_s
  @container.to_s + " - #{@mimetype}"
end