rubyzip/example.rb

50 lines
1.3 KiB
Ruby
Raw Normal View History

2002-01-03 01:48:31 +08:00
#!/usr/bin/env ruby
2002-01-05 08:52:47 +08:00
system("zip example.zip example.rb NEWS")
2002-01-03 01:48:31 +08:00
require 'zip'
####### Using ZipInputStream alone: #######
2002-01-05 08:52:47 +08:00
Zip::ZipInputStream.open("example.zip") {
2002-01-03 01:48:31 +08:00
|zis|
entry = zis.getNextEntry
2002-01-05 08:52:47 +08:00
puts "********* Zip entry '#{entry.name} (#{entry.size} bytes)' contains: ********"
puts zis.read
entry = zis.getNextEntry
puts "********* Zip entry '#{entry.name} (#{entry.size} bytes)' contains: ********"
2002-01-03 01:48:31 +08:00
puts zis.read
}
2002-01-05 08:52:47 +08:00
2002-02-02 07:14:58 +08:00
####### Using BasicZipFile to read the directory of a zip file: #######
2002-02-02 07:14:58 +08:00
zf = Zip::BasicZipFile.new("example.zip")
2002-01-05 08:52:47 +08:00
zf.each_with_index {
|entry, index|
puts "entry #{index} is #{entry.name}, size = #{entry.size}, compressed size = #{entry.compressedSize}"
# use zf.getInputStream(entry) to get a ZipInputStream for the entry
# entry can be the ZipEntry object or any object which has a to_s method that
# returns the name of the entry.
}
####### Using ZipOutputStream to write a zip file: #######
Zip::ZipOutputStream.open("exampleout.zip") {
|zos|
zos.putNextEntry("the first little entry")
zos.puts "Hello hello hello hello hello hello hello hello hello"
zos.putNextEntry("the second little entry")
zos.puts "Hello again"
# Use rubyzip or your zip client of choice to verify
# the contents of exampleout.zip
}
2002-01-03 01:48:31 +08:00
# For other examples, look at zip.rb and ziptest.rb