Class: Zip::ZipFile
- Inherits:
-
ZipCentralDirectory
- Object
- ZipCentralDirectory
- Zip::ZipFile
- Includes:
- ZipFileSystem
- Defined in:
- lib/ruby_archive/handlers/rubyzip/zip/zip.rb,
lib/ruby_archive/handlers/rubyzip/zip/zipfilesystem.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
Zip::ZipCentralDirectory::END_OF_CENTRAL_DIRECTORY_SIGNATURE, Zip::ZipCentralDirectory::MAX_END_OF_CENTRAL_DIRECTORY_STRUCTURE_SIZE, Zip::ZipCentralDirectory::STATIC_EOCD_SIZE
Instance Attribute Summary collapse
-
#comment ⇒ Object
Returns the zip files comment, if it has one.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#restore_ownership ⇒ Object
default -> false.
-
#restore_permissions ⇒ Object
default -> false.
-
#restore_times ⇒ Object
default -> true.
Class Method Summary collapse
-
.foreach(aZipFileName, &block) ⇒ Object
Iterates over the contents of the ZipFile.
-
.open(fileName, create = nil) ⇒ Object
Same as #new.
Instance Method Summary collapse
-
#add(entry, srcPath, &continueOnExistsProc) ⇒ Object
Convenience method for adding the contents of a file to the archive.
-
#close ⇒ Object
Closes the zip file committing any changes that has been made.
-
#commit ⇒ Object
Commits changes that has been made since the previous commit to the zip archive.
-
#commit_required? ⇒ Boolean
Returns true if any changes has been made to this archive since the previous commit.
-
#extract(entry, destPath, &onExistsProc) ⇒ Object
Extracts entry to file destPath.
-
#find_entry(entry) ⇒ Object
Searches for entry with the specified name.
-
#get_entry(entry) ⇒ Object
Searches for an entry just as find_entry, but throws Errno::ENOENT if no entry is found.
-
#get_input_stream(entry, &aProc) ⇒ Object
Returns an input stream to the specified entry.
-
#get_output_stream(entry, &aProc) ⇒ Object
Returns an output stream to the specified entry.
-
#initialize(fileName, create = nil) ⇒ ZipFile
constructor
Opens a zip archive.
-
#mkdir(entryName, permissionInt = 0755) ⇒ Object
Creates a directory.
-
#read(entry) ⇒ Object
Returns a string containing the contents of the specified entry.
-
#remove(entry) ⇒ Object
Removes the specified entry.
-
#rename(entry, newName, &continueOnExistsProc) ⇒ Object
Renames the specified entry.
-
#replace(entry, srcPath) ⇒ Object
Replaces the specified entry with the contents of srcPath (from the file system).
-
#to_s ⇒ Object
Returns the name of the zip archive.
Methods included from ZipFileSystem
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
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.
1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 |
# File 'lib/ruby_archive/handlers/rubyzip/zip/zip.rb', line 1387 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 @restore_ownership = false @restore_permissions = false @restore_times = true end |
Instance Attribute Details
#comment ⇒ Object
Returns the zip files comment, if it has one
1423 1424 1425 |
# File 'lib/ruby_archive/handlers/rubyzip/zip/zip.rb', line 1423 def comment @comment end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
1376 1377 1378 |
# File 'lib/ruby_archive/handlers/rubyzip/zip/zip.rb', line 1376 def name @name end |
#restore_ownership ⇒ Object
default -> false
1379 1380 1381 |
# File 'lib/ruby_archive/handlers/rubyzip/zip/zip.rb', line 1379 def restore_ownership @restore_ownership end |
#restore_permissions ⇒ Object
default -> false
1381 1382 1383 |
# File 'lib/ruby_archive/handlers/rubyzip/zip/zip.rb', line 1381 def @restore_permissions end |
#restore_times ⇒ Object
default -> true
1383 1384 1385 |
# File 'lib/ruby_archive/handlers/rubyzip/zip/zip.rb', line 1383 def restore_times @restore_times 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).
1431 1432 1433 1434 1435 1436 |
# File 'lib/ruby_archive/handlers/rubyzip/zip/zip.rb', line 1431 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.
1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 |
# File 'lib/ruby_archive/handlers/rubyzip/zip/zip.rb', line 1409 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
1470 1471 1472 1473 1474 1475 1476 |
# File 'lib/ruby_archive/handlers/rubyzip/zip/zip.rb', line 1470 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) newEntry.gather_fileinfo_from_srcpath(srcPath) @entrySet << newEntry end |
#close ⇒ Object
Closes the zip file committing any changes that has been made.
1524 1525 1526 |
# File 'lib/ruby_archive/handlers/rubyzip/zip/zip.rb', line 1524 def close commit end |
#commit ⇒ Object
Commits changes that has been made since the previous commit to the zip archive.
1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 |
# File 'lib/ruby_archive/handlers/rubyzip/zip/zip.rb', line 1508 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
1530 1531 1532 |
# File 'lib/ruby_archive/handlers/rubyzip/zip/zip.rb', line 1530 def commit_required? return @entrySet != @storedEntries || @create == ZipFile::CREATE end |
#extract(entry, destPath, &onExistsProc) ⇒ Object
Extracts entry to file destPath.
1500 1501 1502 1503 1504 |
# File 'lib/ruby_archive/handlers/rubyzip/zip/zip.rb', line 1500 def extract(entry, destPath, &onExistsProc) onExistsProc ||= proc { false } foundEntry = get_entry(entry) foundEntry.extract(destPath, &onExistsProc) end |
#find_entry(entry) ⇒ Object
Searches for entry with the specified name. Returns nil if no entry is found. See also get_entry
1536 1537 1538 1539 1540 1541 |
# File 'lib/ruby_archive/handlers/rubyzip/zip/zip.rb', line 1536 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.
1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 |
# File 'lib/ruby_archive/handlers/rubyzip/zip/zip.rb', line 1545 def get_entry(entry) selectedEntry = find_entry(entry) unless selectedEntry raise Errno::ENOENT, entry end selectedEntry.restore_ownership = @restore_ownership selectedEntry. = @restore_permissions selectedEntry.restore_times = @restore_times 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.
1441 1442 1443 |
# File 'lib/ruby_archive/handlers/rubyzip/zip/zip.rb', line 1441 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.
1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 |
# File 'lib/ruby_archive/handlers/rubyzip/zip/zip.rb', line 1448 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 = 0755) ⇒ Object
Creates a directory
1558 1559 1560 1561 1562 1563 |
# File 'lib/ruby_archive/handlers/rubyzip/zip/zip.rb', line 1558 def mkdir(entryName, = 0755) if find_entry(entryName) raise Errno::EEXIST, "File exists - #{entryName}" end @entrySet << ZipStreamableDirectory.new(@name, entryName.to_s.ensure_end("/"), nil, ) end |
#read(entry) ⇒ Object
Returns a string containing the contents of the specified entry
1465 1466 1467 |
# File 'lib/ruby_archive/handlers/rubyzip/zip/zip.rb', line 1465 def read(entry) get_input_stream(entry) { |is| is.read } end |
#remove(entry) ⇒ Object
Removes the specified entry.
1479 1480 1481 |
# File 'lib/ruby_archive/handlers/rubyzip/zip/zip.rb', line 1479 def remove(entry) @entrySet.delete(get_entry(entry)) end |
#rename(entry, newName, &continueOnExistsProc) ⇒ Object
Renames the specified entry.
1484 1485 1486 1487 1488 1489 1490 |
# File 'lib/ruby_archive/handlers/rubyzip/zip/zip.rb', line 1484 def rename(entry, newName, &continueOnExistsProc) foundEntry = get_entry(entry) check_entry_exists(newName, continueOnExistsProc, "rename") @entrySet.delete(foundEntry) foundEntry.name = newName @entrySet << foundEntry end |
#replace(entry, srcPath) ⇒ Object
Replaces the specified entry with the contents of srcPath (from the file system).
1494 1495 1496 1497 |
# File 'lib/ruby_archive/handlers/rubyzip/zip/zip.rb', line 1494 def replace(entry, srcPath) check_file(srcPath) add(remove(entry), srcPath) end |
#to_s ⇒ Object
Returns the name of the zip archive
1460 1461 1462 |
# File 'lib/ruby_archive/handlers/rubyzip/zip/zip.rb', line 1460 def to_s @name end |