class Zip::Archive

Overview

Main zip archive class. Use the Archive.create and Archive.open class methods to create a new archive or open an existing archive, respectively.

Examples

# create a new archive named foo.zip and populate it
Zip::Archive.create("foo.zip") do |zip|
  # add file "/path/to/foo.txt" to archive as "bar.txt"
  zip.add_file("bar.txt", "/path/to/foo.txt")

  # add file "baz.txt" with contents "hello world!"
  zip.add("baz.txt", "hello world!")
end

# read "bar.txt" as string from "foo.zip"
str = Zip::Archive.open("foo.zip") do |zip|
  String.new(zip.read("bar.txt"))
end

# print contents of bar.txt
puts "contents of bar.txt: #{str}"

You can also use Zip::Archive imperatively, like this:

# create foo.zip
zip = Zip::Archive.create("foo.zip")

# add bar.txt
zip.add("bar.txt", "sample contents of bar.txt")

# close and write zip file
zip.close

Included Modules

Defined in:

zip/archive.cr

Constant Summary

CREATE_FLAGS = (OpenFlag.flags(CREATE, EXCL)).value

Default flags for Archive#create

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.new(path : String, flags : Int32 = 0) #

Create a Archive instance from the file path.

Raises an exception if Archive could not be opened.

Example

# create foo.zip
zip = Zip::Archive.new("foo.zip", Zip::OpenFlags::CREATE.value)

# add bar.txt
zip.add("bar.txt", "sample contents of bar.txt")

# close and write zip file
zip.close

[View source]
def self.new(fd : IO::FileDescriptor, flags : Int32 = 0) #

Returns Archive instance from IO::FileDescriptor fd.

Raises an exception if Archive could not be opened.

FIXME This method does not currently work (see spec for details).


[View source]

Class Method Detail

def self.create(path : String, flags : Int32 = CREATE_FLAGS, &) #

Create Archive instance from file path, pass the instance to the given block block, then close the archive when the block exits.

Raises an exception if Archive could not be opened.

Example

# create a new archive named foo.zip and populate it
Zip::Archive.create("foo.zip") do |zip|
  # add file "/path/to/foo.txt" to archive as "bar.txt"
  zip.add_file("bar.txt", "/path/to/foo.txt")

  # add file "baz.txt" with contents "hello world!"
  zip.add("baz.txt", "hello world!")
end

[View source]
def self.create(path : String, flags : Int32 = CREATE_FLAGS) #

Create Archive instance from file path.

Raises an exception if Archive could not be opened.

Example

# create foo.zip
zip = Zip::Archive.create("foo.zip")

# add bar.txt
zip.add("bar.txt", "sample contents of bar.txt")

# close and write zip file
zip.close

See Also:


[View source]
def self.open(path : String, flags : Int32 = 0, &) #

Create Archive instance from file path, pass instance to the given block block, then close the archive when the block exits.

Raises an exception if Archive could not be opened.

Example

# open existing archive "foo.zip" and extract "bar.txt" from it
Zip::Archive.open("foo.zip") do |zip|
  # create string builder
  str = String.build do |b|
    # open file from zip
    zip.read("bar.txt") do |buf, len|
      b.write(buf[0, len])
    end
  end

  # print contents of bar.txt
  puts "contents of bar.txt: #{str}"
end

[View source]
def self.open(fd : IO::FileDescriptor, flags : Int32 = 0, &) #

Create Archive instance from IO::FileDescriptor fd, pass instance to the given block block, then close the archive when the block exits.

Raises an exception if Archive could not be opened.

Example

# open existing archive "foo.zip" and extract "bar.txt" from it
Zip::Archive.open("foo.zip") do |zip|
  # create string builder
  str = String.build do |b|
    # open file from zip
    zip.read("bar.txt") do |buf, len|
      b.write(buf[0, len])
    end
  end

  # print contents of bar.txt
  puts "contents of bar.txt: #{str}"
end

[View source]

Instance Method Detail

def add(path : String, body : String, flags : UInt32 = 0_u32) #

Add archive entry path with content body.

Raises an exception if this Archive is not open.

Example

# add "foo.txt" to archive with body "hello from foo.txt"
zip.add("foo.txt", "hello from foo.txt")

[View source]
def add(path : String, slice : Slice, flags : UInt32 = 0_u32) #

Add archive entry path with content body.

Raises an exception if this Archive is not open.

Example

# create slice
slice = Slice.new(10) { |i| i + 10 }

# add slice to archive as "foo.txt"
zip.add("foo.txt", slice)

[View source]
def add(fname : String, source : Source, flags : LibZip::Flags = 0) : Int64 #

Add given Zip::Source source to archive as path and return index of new entry.

Raises an exception if this Archive is not open or if source could not be added.

Example

# create a new string source
src = Zip::StringSource.new("test string")

# add to archive as "foo.txt"
zip.add("foo.txt", src)

[View source]
def add_dir(path : String, flags : Int32 = 0) #

Add directory to archive at path path.

Raises an exception if this Archive is not open, or if dir could not be added.

Example

# add directory "some-dir" to archive
zip.add_dir("some-dir")

[View source]
def add_file(dst_path : String, src_path : String, start : UInt64 = 0_u64, len : Int64 = -1_i64, flags : UInt32 = 0_u32) #

Add file src_path to archive as dst_path.

Raises an exception if this Archive is not open.

Example

# add "/path/to/file.txt" to archive as "foo.txt"
zip.add("foo.txt", "/path/to/file.txt")

[View source]
def add_file(path : String, start : UInt64 = 0_u64, len : Int64 = -1_i64, flags : UInt32 = 0_u32) #

Add file path to archive as path.

Raises an exception if this Archive is not open.

Example

# add "/path/to/file.txt" to archive
zip.add_file("/path/to/file.txt")

[View source]
def clear_error #

Clear last archive error code.

Raises an exception if this Archive is not open.

Example

# clear last error
zip.clear_error

[View source]
def close(discard : Bool = false) #

Close this Archive. If discard is true, then discard any changes.

Raises an exception if this Archive is not open or if the archive could not be closed.

Example

# close zip file
zip.close

[View source]
def comment(flags : Int32 = 0) : String | Nil #

Returns comment for the entire archive, or nil if there is no comment set.

Raises an exception if this Archive is not open.

Example

# get archive comment and print it
if comment = zip.comment
  puts comment
end

[View source]
def comment=(str : String) : String #

Set and return the comment for the entire archive. Comment must be encoded in ASCII or UTF-8. Returns comment string.

Raises an exception if this Archive is not open or if the comment could not be set.

Example

# set archive comment
zip.comment = "this is a test comment"

[View source]
def compression_flags : Int32 #

[View source]
def compression_flags=(compression_flags : Int32) #

[View source]
def compression_method : CompressionMethod #

[View source]
def compression_method=(compression_method : CompressionMethod) #

[View source]
def default_password=(password : String | Nil) #

Set the default password used when accessing encrypted files, or nil for no password.

Raises an exception if this Archive is not open, or if the default password could not be set.

Example

# set default password to "foobar"
zip.default_password = "foobar"

[View source]
def delete(index : UInt64) : Nil #

Delete file at given index index.

Raises an exception if this Archive is not open, or if file could not be deleted.

Example

# delete first file in archive
zip.delete(0)

[View source]
def delete(path : String) #

Delete file at given path path.

Raises an exception if this Archive is not open, or if file could not be deleted.

Example

# delete "foo.txt" from archive
zip.delete("foo.txt")

[View source]
def discard #

Discard any changes and close this Archive. Equivalent to #close(true).

Raises an exception if this Archive is not open or if the archive could not be closed.

Example

# close zip file and discard changes
zip.discard

[View source]
def each(flags : Int32 = 0, &block : String -> ) #

Iterate over names of files in archive.

Raises an exception if this Archive is not open.

Example

# get file names
file_names = [] of String
zip.each { |name| file_names << name }

[View source]
def each(flags : Int32 = 0) #

Return iterator over names of files in archive.

Raises an exception if this Archive is not open.

Example

# create name iterator
names = zip.each

# iterate names 2 at a time
names.take(2).each do |name|
  puts name
end

[View source]
def error : ErrorCode #

Return last archive error as an ErrorCode instance.

Raises an exception if this Archive is not open.

Example

# get and print last error
puts zip.error

[View source]
def get_file_comment(index : UInt64, flags : Int32 = 0) : String | Nil #

Returns comment of file at index index or nil if there is no comment set.

Raises an exception if this Archive is not open, or if there was an error fetching the file comment.

Example

# get comment of first file in archive
zip.get_file_comment(0)

[View source]
def get_file_comment(path : String, flags : Int32 = 0) #

Returns comment of file at path path, or nil if there was no comment set.

Raises an exception if this Archive is not open, or if there was an error fetching the file comment.

Example

# get comment of "foo.txt"
zip.get_file_comment("foo.txt")

[View source]
def get_name(index : UInt64, flags : Int32) : String #

Returns path of given index, or nil if there was an error or the given index could not be found.

Raises an exception if this Archive is not open.

Example

# get name of first file in archive
path = zip.get_name(0)

[View source]
def name_locate(path : String, flags : Int32 = 0) : Int64 #

Returns index of given path, or -1 if the given path could not be found.

Raises an exception if this Archive is not open.

Example

# get index of "foo.txt"
index = zip.name_locate("foo.txt")

[View source]
def name_locate!(path : String, flags : Int32 = 0) : UInt64 #

Returns index of given path or raises an exception if the given path could not be found.

Raises an exception if this Archive is not open.

Example

# get index of "foo.txt" or raise exception if "foo.txt" could
# not be found
index = zip.name_locate!("foo.txt")

[View source]
def num_entries(flags : Int32 = 0) : Int64 #

Get the number of files in this archive.

Raises an exception if this Archive is not open, or if there was an error counting the number of entries.

Example

# get number of entries
count = zip.num_entries

[View source]
def open(fname : String, flags : Int32 = 0, password : String | Nil = nil) : ZipFile #

Open path in Archive and return it as File instance.

Raises an exception if this Archive is not open, or if file could not be opened.

Example

# declare buffer
buf = Slice(UInt8).new(10)

# open "foo.txt"
file = zip.open("foo.txt")

# read up to 10 bytes of contents
len = file.read(buf)

# close "foo.txt"
file.close

[View source]
def open(fname : String, flags : Int32 = 0, password : String | Nil = nil, &) #

Opens given path in Archive as a File instance, passes it to block block, and closes the file when the block exits.

Raises an exception if this Archive is not open, or if file could not be opened.

Example

# read up to 10 bytes from "foo.txt"
zip.open("foo.txt") do |file|
  # declare buffer
  buf = Slice(UInt8).new(10)

  # read up to 10 bytes of contents
  len = file.read(buf)

  # return buffer
  buf[0, len]
end

[View source]
def open(index : UInt64, flags : Int32 = 0, password : String | Nil = nil) #

Returns File instance of given index index in Archive.

Raises an exception if this Archive is not open, or if file could not be opened.

Example

# declare buffer
buf = Slice(UInt8).new(10)

# open first file in archive
file = zip.open(0)

# read up to 10 bytes of contents
len = file.read(buf)

# close "foo.txt"
file.close

[View source]
def open(index : UInt64, flags : Int32 = 0, password : String | Nil = nil, &) #

Opens index index in Archive as a File instance, passes it to block block, and closes the file when the block exits.

Raises an exception if this Archive is not open, or if file could not be opened.

Example

# open first file in archive
zip.open(0) do |file|
  # declare buffer
  buf = Slice(UInt8).new(10)

  # read up to 10 bytes of contents
  len = file.read(buf)

  # return buffer
  buf[0, len]
end

[View source]
def open? : Bool #

Returns true if this Archive is open, or false otherwise.

Example

# clear last error
puts "zip is %s" % [zip.open? ? "open" : "closed"]

[View source]
def read(fname : String, flags : Int32 = 0, password : String | Nil = nil) : String #

Read contents of file fname and return it as a `String.

Raises an exception if this Archive is not open, if file could not be opened, or if the file could not be read.

Example

# read "foo.txt" from zip file as string
str = zip.read("foo.txt")

[View source]
def read_bytes(fname : String, flags : Int32 = 0, password : String | Nil = nil) : Bytes #

Read contents of file fname and return it as a Slice(UInt8).

Raises an exception if this Archive is not open, if file could not be opened, or if the file could not be read.

Example

# read "foo.txt" from zip file as string
str = String.new(zip.read_bytes("foo.txt"))

[View source]
def rename(index : UInt64, new_path : String, flags : Int32 = 0) #

Rename file at index to path new_path.

Raises an exception if this Archive is not open, or if file could not be renamed.

Example

# rename first file to "new-file.txt"
zip.rename(0, "new-file.txt")

[View source]
def rename(old_path : String, new_path : String, flags : Int32 = 0) #

Rename file named old_path to new path new_path.

Raises an exception if this Archive is not open, or if file could not be renamed.

Example

# rename "foo.txt" to "new-file.txt"
zip.rename("foo.txt", "new-file.txt")

[View source]
def replace(index : UInt64, body : String, flags : Int32 = 0) #

Replace entry at index index with contents body.

Raises an exception if this Archive is not open, or if file could not be replaced.

Example

# replace contents of first file with "new content"
zip.replace(0, "new content")

[View source]
def replace(index : UInt64, source : Source, flags : Int32 = 0) #

Replace entry at index index with Source source.

Raises an exception if this Archive is not open, or if file could not be replaced.

Example

# replace contents of first file with "new content"
zip.replace(0, StringSource.new(zip, "new content"))

[View source]
def replace(path : String, body : String, flags : Int32 = 0) #

Replace entry at path path with contents body.

Raises an exception if this Archive is not open, or if file could not be replaced.

Example

# replace contents of "foo.txt" with "new content"
zip.replace("foo.txt", "new content")

[View source]
def replace(path : String, source : Source, flags : Int32 = 0) #

Replace entry at path path with Source source.

Raises an exception if this Archive is not open, or if file could not be replaced.

Example

# replace contents of "foo.txt" with "new content"
zip.replace("foo.txt", StringSource.new(zip, "new content"))

[View source]
def set_compression(index : Int64, compression_method = @compression_method, compression_flags = @compression_flags) #

[View source]
def set_file_comment(index : UInt64, comment : String | Nil, flags : Int32 = 0) : Nil #

Set comment of file at index to string comment.

Raises an exception if this Archive is not open, or if there was an error setting the file comment.

Example

# set comment of first file to "example comment"
zip.set_file_comment(0, "example comment")

[View source]
def set_file_comment(path : String, comment : String | Nil, flags : Int32 = 0) #

Set comment of file path path to string comment.

Raises an exception if this Archive is not open, or if there was an error setting the file comment.

Example

# set comment of "foo.txt" to "example comment"
zip.set_file_comment("foo.txt", "example comment")

[View source]
def stat(path : String, flags : Int32 = 0) : LibZip::ZipStatT #

Return stat information about path as a LibZip::Stat structure.

Raises an exception if this Archive is not open or if file could not be statted.

Example

# get size of "foo.txt"
st = zip.stat("foo.txt")
puts "file size = #{st.size}"

[View source]
def stat(index : LibC::Int, flags : Int32 = 0) : LibZip::Stat #

Return stat information about file at index index as a LibZip::Stat structure.

Raises an exception if this Archive is not open or if file could not be statted.

Example

# get size of first file in archive
st = zip.stat(0)
puts "file size = #{st.size}"

[View source]
def system_error : LibC::Int #

Return last system error.

Raises an exception if this Archive is not open.

Example

# get and print last system error
puts zip.system_error

[View source]
def to_unsafe : LibZip::ZipT #

[View source]