Class: Rant::Archive::Rubyzip::ZipFile

Inherits:
ZipCentralDirectory show all
Defined in:
lib/rant/archive/rubyzip.rb

Overview

ZipFile is modeled after java.util.zip.ZipFile from the Java SDK. The most important methods are those inherited from ZipCentralDirectory for accessing information about the entries in the archive and methods such as get_input_stream and get_output_stream for reading from and writing entries to the archive. The class includes a few convenience methods such as #extract for extracting entries to the filesystem, and #remove, #replace, #rename and #mkdir for making simple modifications to the archive.

Modifications to a zip archive are not committed until #commit or #close is called. The method #open accepts a block following the pattern from File.open offering a simple way to automatically close the archive when the block returns.

The following example opens zip archive my.zip (creating it if it doesn’t exist) and adds an entry first.txt and a directory entry a_dir to it.

require 'zip/zip'

Zip::ZipFile.open("my.zip", Zip::ZipFile::CREATE) {
 |zipfile|
  zipfile.get_output_stream("first.txt") { |f| f.puts "Hello from ZipFile" }
  zipfile.mkdir("a_dir")
}

The next example reopens my.zip writes the contents of first.txt to standard out and deletes the entry from the archive.

require 'zip/zip'

Zip::ZipFile.open("my.zip", Zip::ZipFile::CREATE) {
  |zipfile|
  puts zipfile.read("first.txt")
  zipfile.remove("first.txt")
}

ZipFileSystem offers an alternative API that emulates ruby’s interface for accessing the filesystem, ie. the File and Dir classes.

Constant Summary collapse

CREATE =
1

Constants inherited from ZipCentralDirectory

Rant::Archive::Rubyzip::ZipCentralDirectory::END_OF_CENTRAL_DIRECTORY_SIGNATURE, Rant::Archive::Rubyzip::ZipCentralDirectory::MAX_END_OF_CENTRAL_DIRECTORY_STRUCTURE_SIZE, Rant::Archive::Rubyzip::ZipCentralDirectory::STATIC_EOCD_SIZE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ZipCentralDirectory

#==, #each, #entries, #get_e_o_c_d, #read_central_directory_entries, #read_e_o_c_d, #read_from_stream, read_from_stream, #size, #write_to_stream

Methods included from Enumerable

#inject, #select_map

Constructor Details

#initialize(fileName, create = nil) ⇒ ZipFile

Opens a zip archive. Pass true as the second parameter to create a new archive if it doesn’t exist already.



1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
# File 'lib/rant/archive/rubyzip.rb', line 1025

def initialize(fileName, create = nil)
  super()
  @name = fileName
  @comment = ""
  if (File.exists?(fileName))
	File.open(name, "rb") { |f| read_from_stream(f) }
  elsif (create)
	@entrySet = ZipEntrySet.new
  else
	raise ZipError, "File #{fileName} not found"
  end
  @create = create
  @storedEntries = @entrySet.dup
end

Instance Attribute Details

#commentObject

Returns the zip files comment, if it has one



1057
1058
1059
# File 'lib/rant/archive/rubyzip.rb', line 1057

def comment
  @comment
end

#nameObject (readonly)

Returns the value of attribute name.



1021
1022
1023
# File 'lib/rant/archive/rubyzip.rb', line 1021

def name
  @name
end

Class Method Details

.foreach(aZipFileName, &block) ⇒ Object

Iterates over the contents of the ZipFile. This is more efficient than using a ZipInputStream since this methods simply iterates through the entries in the central directory structure in the archive whereas ZipInputStream jumps through the entire archive accessing the local entry headers (which contain the same information as the central directory).



1065
1066
1067
1068
1069
1070
# File 'lib/rant/archive/rubyzip.rb', line 1065

def ZipFile.foreach(aZipFileName, &block)
  ZipFile.open(aZipFileName) {
	|zipFile|
	zipFile.each(&block)
  }
end

.open(fileName, create = nil) ⇒ Object

Same as #new. If a block is passed the ZipFile object is passed to the block and is automatically closed afterwards just as with ruby’s builtin File.open method.



1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
# File 'lib/rant/archive/rubyzip.rb', line 1043

def ZipFile.open(fileName, create = nil)
  zf = ZipFile.new(fileName, create)
  if block_given?
	begin
	  yield zf
	ensure
	  zf.close
	end
  else
	zf
  end
end

Instance Method Details

#add(entry, srcPath, &continueOnExistsProc) ⇒ Object

Convenience method for adding the contents of a file to the archive



1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
# File 'lib/rant/archive/rubyzip.rb', line 1104

def add(entry, srcPath, &continueOnExistsProc)
  continueOnExistsProc ||= proc { false }
  check_entry_exists(entry, continueOnExistsProc, "add")
  newEntry = entry.kind_of?(ZipEntry) ? entry : ZipEntry.new(@name, entry.to_s)
  if is_directory(newEntry, srcPath)
	@entrySet << ZipStreamableDirectory.new(newEntry)
  else
	@entrySet << ZipStreamableFile.new(newEntry, srcPath)
  end
end

#closeObject

Closes the zip file committing any changes that has been made.



1163
1164
1165
# File 'lib/rant/archive/rubyzip.rb', line 1163

def close
  commit
end

#commitObject

Commits changes that has been made since the previous commit to the zip archive.



1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
# File 'lib/rant/archive/rubyzip.rb', line 1147

def commit
 return if ! commit_required?
  on_success_replace(name) {
	|tmpFile|
	ZipOutputStream.open(tmpFile) {
	  |zos|

	  @entrySet.each { |e| e.write_to_zip_output_stream(zos) }
	  zos.comment = comment
	}
	true
  }
  initialize(name)
end

#commit_required?Boolean

Returns true if any changes has been made to this archive since the previous commit

Returns:

  • (Boolean)


1169
1170
1171
# File 'lib/rant/archive/rubyzip.rb', line 1169

def commit_required?
  return @entrySet != @storedEntries || @create == ZipFile::CREATE
end

#extract(entry, destPath, &onExistsProc) ⇒ Object

Extracts entry to file destPath.



1135
1136
1137
1138
1139
1140
1141
1142
1143
# File 'lib/rant/archive/rubyzip.rb', line 1135

def extract(entry, destPath, &onExistsProc)
  onExistsProc ||= proc { false }
  foundEntry = get_entry(entry)
  if foundEntry.is_directory
	create_directory(foundEntry, destPath, &onExistsProc)
  else
	write_file(foundEntry, destPath, &onExistsProc) 
  end
end

#find_entry(entry) ⇒ Object

Searches for entry with the specified name. Returns nil if no entry is found. See also get_entry



1175
1176
1177
1178
1179
1180
# File 'lib/rant/archive/rubyzip.rb', line 1175

def find_entry(entry)
  @entrySet.detect { 
	|e| 
	e.name.sub(/\/$/, "") == entry.to_s.sub(/\/$/, "")
  }
end

#get_entry(entry) ⇒ Object

Searches for an entry just as find_entry, but throws Errno::ENOENT if no entry is found.



1184
1185
1186
1187
1188
1189
1190
# File 'lib/rant/archive/rubyzip.rb', line 1184

def get_entry(entry)
  selectedEntry = find_entry(entry)
  unless selectedEntry
	raise Errno::ENOENT, entry
  end
  return selectedEntry
end

#get_input_stream(entry, &aProc) ⇒ Object

Returns an input 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 builtin File.open method.



1075
1076
1077
# File 'lib/rant/archive/rubyzip.rb', line 1075

def get_input_stream(entry, &aProc)
  get_entry(entry).get_input_stream(&aProc)
end

#get_output_stream(entry, &aProc) ⇒ Object

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 builtin File.open method.



1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
# File 'lib/rant/archive/rubyzip.rb', line 1082

def get_output_stream(entry, &aProc)
  newEntry = entry.kind_of?(ZipEntry) ? entry : ZipEntry.new(@name, entry.to_s)
  if newEntry.directory?
	raise ArgumentError,
	  "cannot open stream to directory entry - '#{newEntry}'"
  end
  zipStreamableEntry = ZipStreamableStream.new(newEntry)
  @entrySet << zipStreamableEntry
  zipStreamableEntry.get_output_stream(&aProc)      
end

#mkdir(entryName, permissionInt = 0) ⇒ Object

Creates a directory



1193
1194
1195
1196
1197
1198
# File 'lib/rant/archive/rubyzip.rb', line 1193

def mkdir(entryName, permissionInt = 0) #permissionInt ignored
  if find_entry(entryName)
    raise Errno::EEXIST, "File exists - #{entryName}"
  end
  @entrySet << ZipStreamableDirectory.new(ZipEntry.new(name, entryName.to_s.ensure_end("/")))
end

#read(entry) ⇒ Object

Returns a string containing the contents of the specified entry



1099
1100
1101
# File 'lib/rant/archive/rubyzip.rb', line 1099

def read(entry)
  get_input_stream(entry) { |is| is.read } 
end

#remove(entry) ⇒ Object

Removes the specified entry.



1116
1117
1118
# File 'lib/rant/archive/rubyzip.rb', line 1116

def remove(entry)
  @entrySet.delete(get_entry(entry))
end

#rename(entry, newName, &continueOnExistsProc) ⇒ Object

Renames the specified entry.



1121
1122
1123
1124
1125
# File 'lib/rant/archive/rubyzip.rb', line 1121

def rename(entry, newName, &continueOnExistsProc)
  foundEntry = get_entry(entry)
  check_entry_exists(newName, continueOnExistsProc, "rename")
  foundEntry.name=newName
end

#replace(entry, srcPath) ⇒ Object

Replaces the specified entry with the contents of srcPath (from the file system).



1129
1130
1131
1132
# File 'lib/rant/archive/rubyzip.rb', line 1129

def replace(entry, srcPath)
  check_file(srcPath)
  add(remove(entry), srcPath)
end

#to_sObject

Returns the name of the zip archive



1094
1095
1096
# File 'lib/rant/archive/rubyzip.rb', line 1094

def to_s
  @name
end