class Zip::Archive
- Zip::Archive
- Reference
- Object
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
- Enumerable(String)
- Iterable(String)
Defined in:
zip/archive.crConstant Summary
-
CREATE_FLAGS =
(OpenFlag.flags(CREATE, EXCL)).value
-
Default flags for
Archive#create
Constructors
-
.new(path : String, flags : Int32 = 0)
Create a
Archive
instance from the file path. -
.new(fd : IO::FileDescriptor, flags : Int32 = 0)
Returns
Archive
instance fromIO::FileDescriptor
fd.
Class Method Summary
-
.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. -
.create(path : String, flags : Int32 = CREATE_FLAGS)
Create
Archive
instance from file path. -
.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. -
.open(fd : IO::FileDescriptor, flags : Int32 = 0, &)
Create
Archive
instance fromIO::FileDescriptor
fd, pass instance to the given block block, then close the archive when the block exits.
Instance Method Summary
-
#add(path : String, body : String, flags : UInt32 = 0_u32)
Add archive entry path with content body.
-
#add(path : String, slice : Slice, flags : UInt32 = 0_u32)
Add archive entry path with content body.
-
#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. -
#add_dir(path : String, flags : Int32 = 0)
Add directory to archive at path path.
-
#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.
-
#add_file(path : String, start : UInt64 = 0_u64, len : Int64 = -1_i64, flags : UInt32 = 0_u32)
Add file path to archive as path.
-
#clear_error
Clear last archive error code.
-
#close(discard : Bool = false)
Close this
Archive
. -
#comment(flags : Int32 = 0) : String | Nil
Returns comment for the entire archive, or nil if there is no comment set.
-
#comment=(str : String) : String
Set and return the comment for the entire archive.
- #compression_flags : Int32
- #compression_flags=(compression_flags : Int32)
- #compression_method : CompressionMethod
- #compression_method=(compression_method : CompressionMethod)
-
#default_password=(password : String | Nil)
Set the default password used when accessing encrypted files, or nil for no password.
-
#delete(index : UInt64) : Nil
Delete file at given index index.
-
#delete(path : String)
Delete file at given path path.
-
#discard
Discard any changes and close this
Archive
. -
#each(flags : Int32 = 0, &block : String -> )
Iterate over names of files in archive.
-
#each(flags : Int32 = 0)
Return iterator over names of files in archive.
-
#error : ErrorCode
Return last archive error as an
ErrorCode
instance. -
#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.
-
#get_file_comment(path : String, flags : Int32 = 0)
Returns comment of file at path path, or nil if there was no comment set.
-
#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.
-
#name_locate(path : String, flags : Int32 = 0) : Int64
Returns index of given path, or -1 if the given path could not be found.
-
#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.
-
#num_entries(flags : Int32 = 0) : Int64
Get the number of files in this archive.
-
#open(fname : String, flags : Int32 = 0, password : String | Nil = nil) : ZipFile
Open path in
Archive
and return it asFile
instance. -
#open(fname : String, flags : Int32 = 0, password : String | Nil = nil, &)
Opens given path in
Archive
as aFile
instance, passes it to block block, and closes the file when the block exits. -
#open(index : UInt64, flags : Int32 = 0, password : String | Nil = nil)
Returns
File
instance of given index index inArchive
. -
#open(index : UInt64, flags : Int32 = 0, password : String | Nil = nil, &)
Opens index index in
Archive
as aFile
instance, passes it to block block, and closes the file when the block exits. -
#open? : Bool
Returns true if this
Archive
is open, or false otherwise. -
#read(fname : String, flags : Int32 = 0, password : String | Nil = nil) : String
Read contents of file fname and return it as a `String.
-
#read_bytes(fname : String, flags : Int32 = 0, password : String | Nil = nil) : Bytes
Read contents of file fname and return it as a
Slice(UInt8)
. -
#rename(index : UInt64, new_path : String, flags : Int32 = 0)
Rename file at index to path new_path.
-
#rename(old_path : String, new_path : String, flags : Int32 = 0)
Rename file named old_path to new path new_path.
-
#replace(index : UInt64, body : String, flags : Int32 = 0)
Replace entry at index index with contents body.
-
#replace(index : UInt64, source : Source, flags : Int32 = 0)
Replace entry at index index with
Source
source. -
#replace(path : String, body : String, flags : Int32 = 0)
Replace entry at path path with contents body.
-
#replace(path : String, source : Source, flags : Int32 = 0)
Replace entry at path path with
Source
source. - #set_compression(index : Int64, compression_method = @compression_method, compression_flags = @compression_flags)
-
#set_file_comment(index : UInt64, comment : String | Nil, flags : Int32 = 0) : Nil
Set comment of file at index to string comment.
-
#set_file_comment(path : String, comment : String | Nil, flags : Int32 = 0)
Set comment of file path path to string comment.
-
#stat(path : String, flags : Int32 = 0) : LibZip::ZipStatT
Return stat information about
path
as aLibZip::Stat
structure. -
#stat(index : LibC::Int, flags : Int32 = 0) : LibZip::Stat
Return stat information about file at index index as a
LibZip::Stat
structure. -
#system_error : LibC::Int
Return last system error.
- #to_unsafe : LibZip::ZipT
Constructor Detail
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
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).
Class Method Detail
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
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:
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
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
Instance Method Detail
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")
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)
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)
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")
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")
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")
Clear last archive error code.
Raises an exception if this Archive
is not open.
Example
# clear last error
zip.clear_error
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
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
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"
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"
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)
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")
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
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 }
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
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
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)
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")
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)
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")
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")
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
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
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
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
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
Returns true if this Archive
is open, or false otherwise.
Example
# clear last error
puts "zip is %s" % [zip.open? ? "open" : "closed"]
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")
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"))
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")
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")
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")
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"))
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")
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"))
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")
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")
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}"
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}"