All formats

seven-zip (SevenZipRuby)

LanguageRuby, native extension
LicenseLGPL-2.1-or-later (the bundled 7-Zip code adds the unRAR restriction for RAR)
Repositorygithub.com/fontist/seven_zip_ruby
DocumentationREADME and doc comments in lib/seven_zip_ruby
Depends ona C++ compiler at install time; the gem builds its bundled p7zip source into 7z.so / 7z.dll
Read / writeyes / yes; all 7-Zip methods (LZMA, LZMA2, PPMd, BZip2, Deflate, Copy), AES-256, header encryption, solid mode

SevenZipRuby wraps the 7-Zip library: the gem contains the source of p7zip (the Unix port of 7-Zip 16.02) and compiles it during installation, so it reads and writes 7z with 7-Zip’s own code and needs no 7z program on the system. Newer 7-Zip features (ARM64 and RISC-V filters, for example) are missing. SevenZipRuby::Reader extracts archives to disk or to memory; SevenZipRuby::Writer creates them from files, folders or strings. It works on Windows, Linux and macOS.

The gem on RubyGems is seven-zip (version 1.4.2 is the latest stable release, newer versions are published as pre-releases and in the repository); it replaces the unmaintained seven_zip_ruby gem and uses the same require "seven_zip_ruby" and class names. Archives are read through a Ruby IO, so open the file in binary mode ("rb" / "wb").

Installation#

Shell
gem install seven-zip

Or in a Gemfile:

Ruby
gem "seven-zip"

Basic usage: extract and create a 7z archive#

Ruby
require "seven_zip_ruby"

# extract everything
File.open("archive.7z", "rb") do |file|
  SevenZipRuby::Reader.extract_all(file, "output")
end

# create an archive from a folder and a file
File.open("backup.7z", "wb") do |file|
  SevenZipRuby::Writer.open(file) do |szw|
    szw.add_directory("photos")
    szw.add_file("report.pdf")
    szw.add_data("created by my app\n", "notes/readme.txt")
  end
end

How to list the files in a 7z archive#

Ruby
require "seven_zip_ruby"

File.open("archive.7z", "rb") do |file|
  SevenZipRuby::Reader.open(file) do |szr|
    szr.entries.each do |entry|
      puts "#{entry.path}  #{entry.size}  #{entry.directory? ? 'dir' : ''}  #{entry.mtime}"
    end
  end
end

How to read a file from a 7z archive into memory#

Ruby
require "seven_zip_ruby"

data = nil
File.open("archive.7z", "rb") do |file|
  SevenZipRuby::Reader.open(file) do |szr|
    entry = szr.entries.find { |e| e.path == "notes/readme.txt" }
    data = szr.extract_data(entry)
  end
end
puts data

extract_data also accepts an array of entries and returns an array of strings, which is much faster than many single calls on a solid archive.

How to extract selected files#

Ruby
File.open("archive.7z", "rb") do |file|
  SevenZipRuby::Reader.open(file) do |szr|
    pdfs = szr.entries.select { |e| e.file? && e.path.end_with?(".pdf") }
    szr.extract(pdfs, "output")
  end
end

How to create and extract an encrypted 7z archive#

Ruby
require "seven_zip_ruby"

File.open("secret.7z", "wb") do |file|
  SevenZipRuby::Writer.open(file, password: "secret") do |szw|
    szw.header_encryption = true   # hide file names too
    szw.add_directory("private")
  end
end

File.open("secret.7z", "rb") do |file|
  SevenZipRuby::Reader.extract_all(file, "output", password: "secret")
end

With a wrong or missing password no data is returned for encrypted entries.

How to set compression method and level#

Ruby
File.open("small.7z", "wb") do |file|
  SevenZipRuby::Writer.open(file) do |szw|
    szw.method = "LZMA2"   # "LZMA", "LZMA2", "PPMd", "BZIP2", "DEFLATE", "COPY"
    szw.level = 9          # 0, 1, 3, 5, 7 or 9
    szw.solid = false      # one block per file
    szw.add_directory("data")
  end
end

Tips#