Code Monkey home page Code Monkey logo

tinylzma's Introduction

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:

tinylzma's People

Contributors

wangxuan95 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

tinylzma's Issues

Determining required memory for decompression

Hello,

I really like the simplicity of this library, but there is something i cannot understand about the decompression routine,

    switch (mode) {
        case 1  :
            dst_len = DECOMPRESS_OUTPUT_MAX_LEN;
            break;
        default :  // case 2 or 3 : 
            dst_len = src_len + (src_len>>2) + 4096;
            if (dst_len < src_len)                        // size_t data type overflow
                dst_len = (~((size_t)0));                 // max value of size_t
            break;
    }
    
    
    p_dst = (uint8_t*)malloc(dst_len);

Isn't there a better way to determine how much memory is going to be needed for decompression? Do we really need to allocate a huge memory buffer(512MB) to decompress?

Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.