Skip to content
Pure Lua compressor and decompressor with high compression ratio using DEFLATE/zlib format.
Branch: master
Clone or download
Latest commit 07f9d36 Jul 29, 2018
Type Name Latest commit message Commit time
Failed to load latest commit information.
.travis Dont run luadoc in travis May 20, 2018
docs v1.0.0-release version number and changelog. Jul 29, 2018
.appveyor.yml alpha2 update May 21, 2018
.gitignore Add script for convenient performance evaluation. Jul 26, 2018
.luacheckrc Exclude LibCompress.lua from linter Jul 26, 2018
LibDeflate.lua v1.0.0-release version number and changelog. Jul 29, 2018
LibDeflate.toc Update project description May 22, 2018 Example Update Jul 29, 2018
lib.xml Add WoW and curseforge meta files May 20, 2018

Build Status Build status AppVeyor tests branch license LuaRocks GitHub issues


Pure Lua compressor and decompressor with high compression ratio using DEFLATE/zlib format.

Copyright (C) 2018 Haoqian He


LibDeflate is pure Lua compressor and decompressor with high compression ratio, which compresses almost as good as zlib. The purpose of this project is to give a reasonable good compression when you only have access to a pure Lua environment, without accessing to Lua C bindings or any external Lua libraries. LibDeflate does not have any dependencies except you need to have a working Lua interpreter.

LibDeflate uses the following compression formats:

  1. DEFLATE, as defined by the specification RFC1951. DEFLATE is the default compression method of ZIP.
  2. zlib, as defined by the specification RFC1950. zlib format uses DEFLATE formats to compress data and adds several bytes as headers and checksum.

A simple C program utilizing zlib should be compatible with LibDeflate. If you are not sure how to write this program, goto the zlib repository, or read tests/zdeflate.c in this repository.

Supported Lua Versions

LibDeflate supports and is fully tested under Lua 5.1/5.2/5.3, LuaJIT 2.0/2.1, for Linux, MaxOS and Windows. Click the Travis CI(Linux/MaxOS) and Appveyor(Windows) badge on the top of this README for the test results. Click the CodeCov badge to see the test coverage (should be 100%).


Documentation is hosted on Github. Beside run as a library, LibDeflate can also be run directly in commmandline. See the documentation for detail.


Though many performance optimization has been done in the source code, as a pure Lua implementation, the compression speed of LibDeflate is significantly slower than a C compressor. LibDeflate aims to compress small files, and it is suggested to not compress files with the order of several Megabytes. If you need to compress files hundreds of MetaBytes, please use a C compressor, or a Lua compressor with C binding.


Below is a simple benchmark compared with another pure Lua compressor LibCompress. More benchmarks can be viewed in the documentation.

LibDeflate LibDeflate LibDeflate LibCompress LibCompress LibCompress
CompressDeflate Level 1 CompressDeflate Level 5 CompressDeflate Level 8 Compress CompressLZW CompressHuffman
compress ratio 3.15 3.68 3.71 1.36 1.20 1.36
compress time(ms) 70 120 200 127 58 64
decompress time(ms) 35 32 32 62 36 62
compress+decompress time(ms) 105 152 232 189 94 126

LibDeflate with compression level 1 compresses as fast as LibCompress, but already produces significantly smaller data than LibCompress. High compression level takes a bit more time to get better compression.

Download And Install

  • The official repository locates on Github. LibDeflate.lua is the only file of LibDeflate. Copy the file to your LUA_PATH to install it.

  • To download as a World of Warcraft library, goto LibDeflate Curseforge Page

  • You can also install via Luarocks using the command "luarocks install libdeflate"

  • To use after installation, require("LibDeflate") (case sensitive) in your Lua interpreter, or LibStub:GetLibrary("LibDeflate") (case sensitive) for World of Warcraft.


local LibDeflate
if LibStub then -- You are using LibDeflate as WoW addon
	LibDeflate = LibStub:GetLibrary("LibDeflate")
	LibDeflate = require("LibDeflate")

local example_input = "12123123412345123456123456712345678123456789"

--- Compress using raw deflate format
local compress_deflate = LibDeflate:CompressDeflate(example_input)

-- decompress
local decompress_deflate = LibDeflate:DecompressDeflate(compress_deflate)

-- Check if the first return value of DecompressXXXX is non-nil to know if the
-- decompression succeeds.
if decompress_deflate == nil then
	error("Decompression fails.")
	-- Decompression succeeds.
	assert(example_input == decompress_deflate)

-- To transmit through WoW addon channel, data must be encoded so NULL ("\000")
-- is not in the data.
local data_to_trasmit_WoW_addon = LibDeflate:EncodeForWoWAddonChannel(
-- When the receiver gets the data, decoded it first.
local data_decoded_WoW_addon = LibDeflate:DecodeForWoWAddonChannel(
-- Then decomrpess it
local decompress_deflate = LibDeflate:DecompressDeflate(data_decoded_WoW_addon)

assert(decompress_deflate == example_input)

-- The compressed output is not printable. EncodeForPrint will convert to
-- a printable format, in case you want to export to the user to
-- copy and paste. This encoding will make the data 25% bigger.
local printable_compressed = LibDeflate:EncodeForPrint(compress_deflate)

-- DecodeForPrint to convert back.
-- DecodeForPrint will remove prefixed and trailing control or space characters
-- in the string before decode it.
assert(LibDeflate:DecodeForPrint(printable_compressed) == compress_deflate)

See Full examples in examples/example.lua


  1. zlib, by Jean-loup Gailly (compression) and Mark Adler (decompression). Licensed under zlib License.
  2. puff, by Mark Adler. Licensed under zlib License.
  3. LibCompress, by jjsheets and Galmok of European Stormrage (Horde). Licensed under GPLv2.
  4. WeakAuras2. Licensed under GPLv2.


LibDeflate is licensed under GNU General Public License Version 3 or later.

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.