WangXuan95 / TinyLZMA

A minimal LZMA data compressor & decompressor. Only hundreds of lines of C.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

language build build

TinyLZMA

A minimal LZMA data compressor & decompressor. Only hundreds of lines of C.

LZMA is a lossless data compression method with a higher compression ratio than Deflate and BZIP. Several container formats supports LZMA:

  • ".7z" and ".xz" format, whose default compression method is LZMA.
  • ".zip" format also supports LZMA, although its default compression method is Deflate.
  • ".lzma" is a very simple format for containing LZMA, which is legacy and gradually replaced by ".xz" format.

This code, TinyLZMA, supports 3 modes:

  • compress a file into a ".zip" file (compress method=LZMA)
  • compress a file into a ".lzma" file
  • decompress a ".lzma" file

Linux Build

On Linux, run command:

gcc src/*.c -o tlzma -O3 -Wall

or just run the script I provide:

sh build.sh

The output executable file is tlzma

Windows Build

First, you should add the Microsoft C compiler cl.exe (from Visual Studio or Visual C++) to environment variables. Then run command:

cl.exe  src\*.c  /Fetlzma.exe  /Ox

or just run the script I provide:

.\build.bat

The output executable file is tlzma.exe

Usage

Run TinyLZMA to show usage:

└─$ ./tlzma
  Tiny LZMA compressor & decompressor v0.2
  Source from https://github.com/WangXuan95/TinyLzma

  Usage :
     mode1 : decompress .lzma file :
       tlzma  <input_file(.lzma)>  <output_file>

     mode2 : compress a file to .lzma file :
       tlzma  <input_file>  <output_file(.lzma)>

     mode3 : compress a file to .zip file (use lzma algorithm) :
       tlzma  <input_file>  <output_file(.zip)>

  Note : on Windows, use 'tlzma.exe' instead of 'tlzma'

Example Usage

Example of mode3: You can compress the file data3.txt in directory testdata to data3.txt.zip using command:

./tlzma testdata/data3.txt data3.txt.zip

The outputting ".zip" file can be extracted by other compression software, such as 7ZIP, WinZip, WinRAR, etc.

Example of mode2: You can use following command to compress a file to a ".lzma" file :

./tlzma testdata/data3.txt data3.txt.lzma

Besides TinyLZMA itself, you can use other LZMA official softwares to decompress ".lzma" file. See How to decompress .lzma file

Example of mode1: You can use following command to decompress a ".lzma" file :

./tlzma data3.txt.lzma data3.txt

Notice

  • TinyLZMA is verified on hundreds of files using automatic scripts.
  • To be simpler, TinyLZMA loads the whole file data to memory to perform compresses/decompresses, so it is limited by memory capacity and cannot handle files that are too large.
  • The search strategy of TinyLZMA's compressor is a simple hash-chain.
  • The compression ratio of TinyLZMA's compressor is mostly like the -1 to -4 level of XZ-Utils's LZMA compressor [2].
  • The performance of TinyLZMA's compressor is mostly like the -2 level of XZ-Utils's LZMA compressor.

👉 XZ-Utils's LZMA compressor has a total of 10 levels, from -0 to -9 . The larger, the higher the compression ratio, but the lower the performance. For example, if you want to use XZ-Utils to compress "a.txt" to "a.txt.lzma" using level 4, the command should be lzma -zk -4 a.txt

Appendix: How to decompress ".lzma" file

on Windows

On Windows, you can use the official 7ZIP/LZMA software to decompress the generated ".lzma" file. To get it, download the "LZMA SDK", extract it. In the "bin" directory, you can see "lzma.exe".

To decompress a ".lzma" file, run command as format:

.\lzma.exe d [input_lzma_file] [output_file]

on Linux

On Linux, you can decompress ".lzma" file using the official "p7zip" software. You should firstly install it:

apt-get install p7zip

Then use following command to decompress the ".lzma" file.

7z x [input_lzma_file]

It may report a error : "ERROR: There are some data after the end of the payload data" . Just ignore it, because there may be a extra "0x00" at the end of ".lzma" file. It won't affect the normal data decompression.

Related Links

The official code of LZMA & 7ZIP & XZ:

To quickly understand the algorithm of LZMA, see:

An FPGA-based hardware data compressor:

About

A minimal LZMA data compressor & decompressor. Only hundreds of lines of C.

License:GNU General Public License v3.0


Languages

Language:C 99.9%Language:Batchfile 0.1%Language:Shell 0.0%