class Zip::Entry

Overview

File entry in Archive.

Use Zip.read() to read a Zip archive, then #[] to fetch a specific archive entry.

Example:

# create MemoryIO
io = MemoryIO.new

# open "foo.zip"
Zip.read("foo.zip") do |zip|
  # get "bar.txt" entry from "foo.zip"
  e = zip["bar.txt"]

  # read contents of "bar.txt" into io
  e.read(io)
end

Included Modules

Defined in:

Class Method Summary

Instance Method Summary

Class Method Detail

def self.new(io : Source) #

Instantiate a new Entry object from the given IO.

You should not need to call this method directly (use Zip::Archive#[] instead).


Instance Method Detail

def comment : String #

Get comment for this Entry as a String.

Zip.read("foo.zip") do |zip|
  # print comment for each entry
  zip.each do |e|
    puts "#{e.path} comment: #{e.comment}"
  end
end

def compressed_size : UInt32 #

Get compressed size for this Entry as a UInt32.

Zip.read("foo.zip") do |zip|
  # print compressed size for each entry
  zip.each do |e|
    puts "#{e.path} compressed size: #{e.compressed_size}"
  end
end

def crc : UInt32 #

Get CRC-32 for this Entry as a UInt32.

Zip.read("foo.zip") do |zip|
  # print crc for each entry
  zip.each do |e|
    puts "#{e.path} CRC-32: #{e.crc}"
  end
end

def dir? : Bool #

Returns true if this entry a directory.

Example:

Zip.read("foo.zip") do |zip|
  type = zip["some-dir/"].dir? ? "directory" : "file"
  puts "#{path} is a #{type}"
end

def external : UInt32 #

Get external attributes for this Entry as a UInt32.

Zip.read("foo.zip") do |zip|
  # print external attributes for each entry
  zip.each do |e|
    puts "#{e.path} external attributes: #{e.external}"
  end
end

def extras : Array(Zip::Extra) #

Get Extra data for this Entry as an Array.

Zip.read("foo.zip") do |zip|
  # print number of extra data items for each entry
  zip.each do |e|
    puts "#{e.path} extras: #{e.extras.size}"
  end
end

def flags : UInt16 #

Get GeneralFlags for this Entry.

Zip.read("foo.zip") do |zip|
  # print flags for each entry
  zip.each do |e|
    puts "#{e.path} flags: #{e.flags}"
  end
end

def internal : UInt16 #

Get internal attributes for this Entry as a UInt16.

Zip.read("foo.zip") do |zip|
  # print internal attributes for each entry
  zip.each do |e|
    puts "#{e.path} internal attributes: #{e.internal}"
  end
end

def local_extras : Array(Extra) #

Returns an array of Extra attributes for this Entry.

Zip archives can (and do) have separate Extra attributes associated with the file entry itself, and the file's entry in the Central Directory.

The #extras method returns the Extra attributes from the file's entry in the Central Directory, and this method returns the Extra data from the file entry itself.

Example:

# open "./foo.zip"
Zip.read("./foo.zip") do |zip|
  # get array of local extra attributes from "bar.txt"
  extras = zip["bar.txt"].local_extras
end

def method : Zip::CompressionMethod #

Get CompressionMethod for this Entry.

Zip.read("foo.zip") do |zip|
  # print compression method for each entry
  zip.each do |e|
    puts "#{e.path} compression method: #{e.method}"
  end
end

def path : String #

Get path for this Entry as a String.

Zip.read("foo.zip") do |zip|
  # print uncompressed size for each entry
  zip.each do |e|
    puts "#{e.path}"
  end
end

def pos : UInt32 #

Get position for this Entry as a UInt32.

Zip.read("foo.zip") do |zip|
  # print position for each entry
  zip.each do |e|
    puts "#{e.path} position: #{e.pos}"
  end
end

def size : UInt32 #

Get uncompressed size for this Entry as a UInt32.

Zip.read("foo.zip") do |zip|
  # print uncompressed size for each entry
  zip.each do |e|
    puts "#{e.path} uncompressed size: #{e.size}"
  end
end

def time : Time #

Get Time for this Entry.

Zip.read("foo.zip") do |zip|
  # print time for each entry
  zip.each do |e|
    puts "#{e.path} time: #{e.time}"
  end
end

def version : UInt16 #

Get Version used to generate this Entry.

Zip.read("foo.zip") do |zip|
  # print version used for each entry
  zip.each do |e|
    puts "#{e.path} version used: #{e.version}"
  end
end

def version_needed : UInt16 #

Get Version needed to generate this Entry.

Zip.read("foo.zip") do |zip|
  # print version needed to extract each entry
  zip.each do |e|
    puts "#{e.path} version needed: #{e.version_needed}"
  end
end

def write(path : String) : UInt32 #

Write contents of Entry into given path path and return the number of bytes written.

Raises an Error if the file contents could not be read or if the compression method is unsupported.

Example:

# open "output-bar.txt" for writing
File.open("output-bar.txt", "wb") do |io|
  # open archive "./foo.zip"
  Zip.read("foo.zip") do |zip|
    # write contents of "bar.txt" to "output-bar.txt"
    zip["foo.txt"].write(io)
  end
end

def write(dst_io : IO) : UInt32 #

Write contents of Entry into given IO.

Raises an Error if the file contents could not be read or if the compression method is unsupported.

Example:

# open "output-bar.txt" for writing
File.open("output-bar.txt", "wb") do |io|
  # open archive "./foo.zip"
  Zip.read("foo.zip") do |zip|
    # write contents of "bar.txt" to "output-bar.txt"
    zip["foo.txt"].write(io)
  end
end