Sunday, 21 February 2021

Interop NSData compressed / decompressed on iOS with Inflater / Deflater on Android

With iOS 13 Apple added some convenience methods to NSData to compress and decompress data using a set of compression algorithms. Zlib is recommended as the cross-platform option for interoperating with non-Apple platforms. I recently had need of compressing data while supporting iOS & Android, I did not find much in the way of documented resources to achieve this, so this post is to document what I found.

Compressing Data

The zlib standard supports different levels of compression (an integer between 0 and 9) that tradeoff the speed verses the output size. On iOS the level is fixed to 5 and can not be changed. While with Android the developer can specify any of the levels on the constructor of the Deflater class. Additionally Deflater constructor takes a boolean flag called nowrap, by default Deflater generates a header and ADLER-32 checksum. By setting nowrap to true Deflater will not generate the header or checksum, this make it generate the same bytes as the iOS compressed method.

While the compressed / decompressed methods are available on NSData, unfortunately Apple has not exposed on the swift Data type, so casting to NSData is required.

Swift

Kotlin


Decompressing Data

Decompressing the data is similar with the decompressed method on iOS and the Inflater class on Android.

Swift

Kotlin

Conclusion

This article has looked at using the Android Inflater / Deflater to generate the same values achieved by iOS's compressed / decompressed methods. This has been achieved by not generating the header and checksum on Android. For some use cases it may be desired to go the opposite way and have iOS generate the header& checksum, for that have a look at https://github.com/mw99/DataCompression.

3 comments:

  1. This article content is really unique and amazing. This article really helpful and explained very well. So I am really thankful to you for sharing keep it up.. Mason Jacob

    ReplyDelete
  2. Thanks for sharing such a useful blog. Really! This Blog is very informative for us which contains a lot of information about the Writing. I like this post. producthunt
    blogtalkradio
    coub
    ali
    sketchfab
    viki
    speakerdeck
    magcloud
    hearthis
    musicbrainz

    ReplyDelete
  3. Service attendants of the lodge speak fluent English, Chinese, Japanese, and different international languages. Those seeking to declare a simple €100 are in luck at Mozzart,BetShah, andFezbetas well as at18Bet,1xBet, andMelbet. You can simply get them atCloudbet, CasinoFair, 1xBit, 카지노사이트 andSpin Sports.888 Sportwill knock you out with their casino bonus packages. Players in South Korea have a host of choices phrases of|in relation to} playing on-line.

    ReplyDelete