, ,

Compress and Decompress Files across Multiple Platforms with Brotli Compression Algorithm

Blake Bradford Avatar

·

Have you ever needed to compress or decompress files across different programming languages and platforms? Look no further – BrotliHaxe is here to save the day! BrotliHaxe is a handported version of the Brotli compression algorithm, which allows you to compress and decompress files seamlessly in JavaScript, PHP, Python, Java, C#, ActionScript, and more.

What is Brotli?

Brotli is a versatile and efficient lossless compression algorithm. It combines a modern variant of the LZ77 algorithm, Huffman coding, and 2nd order context modeling to achieve compression ratios comparable to the best general-purpose compression methods available today. Brotli offers denser compression than deflate, while maintaining similar compression speeds. If you want to learn more about Brotli, check out the Brotli Github.

Introducing BrotliHaxe

BrotliHaxe, developed by Dominik Homberger, is a powerful toolkit built on the Haxe Cross-platform-Toolkit. With BrotliHaxe, you can easily compress and decompress files using the Brotli compression algorithm in JavaScript, PHP, Python, Java, C#, and other programming languages. Whether you’re building a cross-platform application or need to optimize the programming language source, BrotliHaxe has got you covered.

Getting Started with BrotliHaxe

To start using BrotliHaxe, make sure you have Haxe 3.x installed on your system. Once you have installed Haxe, you can compile the BrotliHaxe code and generate the necessary source files for your desired programming language.

BrotliHaxe requires the following four files to be present in the build directory:
– dictionary.txt
– DictionaryBuckets.txt
– DictionaryHash.txt
– DictionaryWords.txt

Depending on your deployment environment, remember to upload these files as binary to your web server to avoid errors during decoding and encoding.

Building BrotliHaxe for Different Platforms

The following instructions demonstrate how to build and execute BrotliHaxe for different platforms:

JavaScript

#sh
$ haxe -cp src -js ./javascript/brotli.js -main Main

Check out the BrotliHaxe source code at BrotliHaxe Github Page.

PHP

#sh
$ haxe -cp src -php ./php/ -main Main
$ php ./php/index.php

Python

#sh
$ haxe -cp src -python ./python/brotli.py -main Main
$ python ./python/brotli.py

Java

#sh
$ haxe -cp src -java ./java/ -main Main
$ java -jar ./java/Main.jar

C

#sh
$ haxe -cp src -cs ./cs/ -main Main

ActionScript

#sh
$ Not implemented yet

Implementing BrotliHaxe as a Library

If you want to use BrotliHaxe as a library in your own applications, follow these steps:

  1. Include the Brotli file by referencing its path in your project. The path may vary depending on the programming language and file structure:
    #include Brotli[.ext]
  2. Create a new instance of the Brotli class:
    #var brotli = new Brotli();
  3. You have four options for compressing and decompressing data:
  • Compressing a string:
    #var compressedstring = brotli.compress(decompressedstring, quality); //quality [1...11]
  • Decompressing a string:
    #var decompressedstring = brotli.decompress(compressedstring);
  • Compressing a byte array (0-255):
    #var compressedbytearray = brotli.compressArray(decompressedbytearray, quality); //quality [1...11]
  • Decompressing a byte array (0-255):
    #var decompressedbytearray = brotli.decompressArray(compressedbytearray);

Benchmark Results

To give you an idea of the performance of BrotliHaxe, here are some benchmark results obtained on an i7 2600k CPU:

| Language | compress | decompress |
|—————|———-|————|
| JavaScript | 414 | 190 |
| PHP | 82114 | 4672 |
| Python | 11339 | 5157 |
| Java | 353 | 100 |
| C# | 402 | 80 |
| Original C(++) | 24 | 13 |

Keep in mind that lower values indicate better performance (in milliseconds).

Development and Future Improvements

BrotliHaxe is based on an older version of the Brotli library but still works effectively. Dominik Homberger is actively working towards updating it to the latest Brotli version. If you have ideas or contributions to optimize the different outputs, Dominik would greatly appreciate your help. However, it is essential to ensure that the source code remains as close to the original as possible to facilitate future updates.

Conclusion

BrotliHaxe is a valuable tool for software engineers and solution architects who need to compress and decompress files across various platforms. With its wide language support and powerful Brotli compression algorithm, BrotliHaxe simplifies cross-platform development and allows for efficient file compression. Give it a try and see how it can enhance your development workflow.

Do you have any questions or suggestions about BrotliHaxe? Feel free to leave a comment below!

References

License

BrotliHaxe is released under the MIT License. To view the license details, refer to the license file included in the repository.

Written by Blake Bradford for GroupSum.xyz

Leave a Reply

Your email address will not be published. Required fields are marked *