module Zip

Overview

Library for reading and writing zip files.

Examples:

Reading from a zip file:

# 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"].read(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.

MAGIC = {cdr_header: 33639248_u32, cdr_footer: 101010256_u32, file_header: 67324752_u32, file_footer: 134695760_u32}

Magic numbers for various data in Zip stream.

VERSION = "0.1.0"

Version of zip-crystal library.

Class Method Summary

Class Method Detail

def self.read(slice : Bytes, &cb : Archive -> ) : Void #

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"].read(io)
end

def self.read(path : String, &cb : Archive -> ) : Void #

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"].read(io)
end

def self.read(io : IO, &cb : Archive -> ) : Void #

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"].read(io)
end

def self.write(io : IO, pos : UInt32 = 0_u32, comment : String = "", version : Version = Version::DEFAULT, &cb : Writer -> ) : UInt32 #

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

def self.write(path : String, pos : UInt32 = 0_u32, comment : String = "", version : Version = Version::DEFAULT, &cb : Writer -> ) : UInt32 #

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