module Zip
Overview
Library for reading and writing zip files.
Features:
- Read and write zip files
- Native Crystal, no dependencies other than zlib
- Stream writing (e.g. write zip to socket, pipe or other arbitrary,
non-seekable IO)
- ZIP64 support
- Store and DEFLATE compression
- UTF-8 filename and comment support (EFS)
TODO:
- LZMA and BZip2 compression
- Encryption (Legacy and Strong Encryption)
- Split archives (e.g. multi-disk archives)
- Legacy Unicode support
Examples:
Reading from a zip file:
require "zip-crystal/zip"
# create output MemoryIO
mem_io = MemoryIO.new
# read from "foo.zip"
Zip.read("foo.zip") do |zip|
# read contents of "bar.txt" in "foo.zip" into mem_io
zip["bar.txt"].write(mem_io)
end
Writing to a zip file:
# write to "foo.zip"
Zip.write("foo.zip") do |zip|
# create "bar.txt" with contents "hello!"
zip.add("bar.txt", "hello!")
end
Defined in:
Constant Summary
-
BUFFER_SIZE =
8192
-
Size of internal buffers, in bytes.
-
EMPTY_SLICE =
Bytes.new(0)
-
:nodoc: Static, zero-length
Bytes
used when empty buffer reference is needed. :nodoc: -
MAGIC =
{cdr_header: 33639248_u32, cdr_footer: 101010256_u32, file_header: 67324752_u32, file_footer: 134695760_u32, z64_footer: 101075792_u32, z64_locator: 117853008_u32}
-
Magic numbers for various data in Zip stream.
-
VERSION =
"0.1.0"
-
Version of zip-crystal library.
Class Method Summary
-
.read(slice : Bytes, &cb : Archive -> ) : Void
Read Zip::Archive from Slice and pass it to the given block.
-
.read(path : String, &cb : Archive -> ) : Void
Read Zip::Archive from File and pass it to the given block.
-
.read(io : IO, &cb : Archive -> ) : Void
Read Zip::Archive from seekable IO instance and pass it to the given block.
-
.write(io : IO, pos : UInt64 = 0_u64, comment : String = "", version : Version = Version::DEFAULT, &cb : Writer -> ) : UInt64
Create a
Zip::Writer
for the output IO io and yield it to the given block. -
.write(path : String, pos : UInt64 = 0_u64, comment : String = "", version : Version = Version::DEFAULT, &cb : Writer -> ) : UInt64
Create a
Zip::Writer
for the output file path and yield it to the given block.
Class Method Detail
Read Zip::Archive from Slice and pass it to the given block.
Example:
# create memory io for contents of "bar.txt"
io = MemoryIO.new
# extract "bar.txt" from zip archive in Slice some_slice and
# save it to MemoryIO
Zip.read(some_slice) do |zip|
zip["bar.txt"].write(io)
end
Read Zip::Archive from File and pass it to the given block.
Example:
# create memory io for contents of "bar.txt"
io = MemoryIO.new
# extract "bar.txt" from "foo.zip" and save it to MemoryIO
Zip.read("foo.zip") do |zip|
zip["bar.txt"].write(io)
end
Read Zip::Archive from seekable IO instance and pass it to the given block.
Example:
# create memory io for contents of "bar.txt"
io = MemoryIO.new
# read "bar.txt" from "foo.zip"
Zip.read(File.open("foo.zip", "rb")) do |zip|
zip["bar.txt"].write(io)
end
Create a Zip::Writer
for the output IO io and yield it to
the given block. Returns number of bytes written.
Example:
# create output IO
File.open("foo.zip", "wb") do |io|
Zip.write(io) do |zip|
# add "bar.txt" with contents "hello!"
zip.add("bar.txt", "hello!")
end
end
Create a Zip::Writer
for the output file path and yield it to
the given block. Returns number of bytes written.
Example:
# create "foo.zip"
Zip.write("foo.zip") do |zip|
# add "bar.txt" with contents "hello!"
zip.add("bar.txt", "hello!")
end