IOS Lzma class compression algorithm uses

Source: Internet
Author: User

LZMA (abbreviated Lempel-ziv-markov chain-algorithm) is a data compression algorithm developed since 2001 and is used to 7-zip the 7z format in the Archive tool and the XZ format under Unix-like. It uses a dictionary encoding mechanism similar to the LZ77, in general the compression rate is higher than bzip2, the dictionary file size for compression can be up to 4GB.

The Lzma open source compression library, written in the C + + language, uses a LZ77 improved compression algorithm supported by the interval encoding and a special preprocessing program for binary processing. LZMA the data flow, the size of the repeating sequence, and the location of the re-sequence are individually compressed. LZMA supports several hash chain variants, binary trees, and cardinality trees as a basis for its dictionary lookup algorithm.

LZMA Algorithm Introduction

For data transmission, transmission time and transmission quality are the main two reference dimensions. For compression of transmission time, data compression is also a good option. Currently processing Bluetooth BLE (bluetooth® low energy (Bluetooth LE, BLE, marketed as Bluetooth smart[1]) debugging, wherein the data transmission process, found due to the speed limit, the transmission time is longer, In order to shorten the transmission time, the compression algorithm is used to compress the data to be transmitted, and then transmit after compressing, in order to improve the transmission efficiency and save the transmission time. After reviewing various data, we use LZMA compression algorithm to compress.

The reason why the LZMA algorithm is chosen for compression processing is as follows:

    • Open source
    • Supported on IOS & Android platforms and can be used on platforms
    • Using a wide range of stability, 7zip is using this algorithm and the derived algorithm
    • High compression efficiency
    • Multithreading support
iOS introduced using the LZMA compression algorithm method option 1. Using the LZMA compression algorithm supported by the system default

Apple offers a common set of lossless compression algorithms that support LZMA, LZMA2 compression.

The Libcompression Library provides an API for both styles of data compression:

  • Block compression, where all of the input data was compressed or decompressed by one call to the compression or Decompressi on function.
  • Streaming compression, where the compression or decompression function is called repeatedly to compress or decompress data From a source buffer to a destination buffer. Between calls, processed data is moved out of the destination buffer and new data are loaded into the source buffer.
Supported compression types
    • Block Compression
    • Stream Compression
The supported platforms are as follows:
    • IOS 9.0+
    • MacOS 10.11+
    • TvOS 9.0+
    • WatchOS 2.0+
Supported compression algorithms
    • Compression_lz4
    • Compression_zlib
    • Compression_lzma
    • Compression_lzfse

The code call provided is simple, and I'm using the following method as I need it:

Compression algorithm

size_t compression_encode_buffer(uint8_t *restrict dst_buffer, size_t dst_size, const uint8_t *restrict src_buffer, size_t src_size, void *restrict scratch_buffer, compression_algorithm algorithm);

Decompression method

size_t compression_decode_buffer(uint8_t *restrict dst_buffer, size_t dst_size, const uint8_t *restrict src_buffer, size_t src_size, void *restrict scratch_buffer, compression_algorithm algorithm);

The detailed calling code is as follows

- (void) Testlzma {Data source file path.NSString *sourcefilepath = [NSString stringWithFormat:@ "%@/source_data.txt", System_document_path];Compressed file path.NSString *zipfilepath = [NSString stringWithFormat:@ "%@/compressed_data.7z", System_document_path];NSData *filedata = [NSData Datawithcontentsoffile:sourcefilepath]; Ddlogdebug (@ "Before Compress:%ld bytes", filedata.length); uint8_t Dstbuffer[filedata.length]; memset (Dstbuffer,0, Filedata.length); size_t compressresultlength = Compression_encode_buffer (Dstbuffer, filedata.length, [fileData bytes], fileData.length , NULL, Compression_lzma); if (Compressresultlength > 0) { nsdata *dataaftercompress = [nsdata datawithbytes:dstbuffer Length: Compressresultlength]; Ddlogdebug (@ "Compress successfully. After compress:%ld bytes ", dataaftercompress.length; //Write compressed data into file. [Dataaftercompress writetofile:zipfilepath atomically:YES];} else {ddlogerror (@ "Compress FAILED!!!");}}          

This method of integration is very simple to use, for the basic compression requirements enough to meet the size of the app will not have too much impact, will not greatly increase, if there is no special requirements, the method is preferred.

Option 2. Integration with third-party libraries Lzmasdkojbcframework

This is my first way to go, by consulting the relevant data, the introduction of the relevant open source Library, the implementation of an iOS support LZMA compression algorithm for the build Framework, is now open source to GitHub, that is, lzmasdkojbcframework.

Although the final adoption of the programme one, but in the production of lzmasdkojbcframework process, there are some gains, is now shared to everyone, is willing to help you some.

The Lzma iOS support library that was originally found is LZMASDKOBJC, but this library is in the process of introducing the development project, because the use of the cocoapods, must be used to use use_frameworks! , but because there are other third-party libraries introduced in Podfile, these libraries do not apply use_frameworks!restrictions.

At this point in the dilemma, using the use_frameworks! other libraries that do not support the framework is not available, if not used, LZMASDKOBJC will report the following error:

Codec was not compiled in or stripped by static linking. Make sure you are using ‘use_frameworks!‘ and/or dynamic linking ...

And Cocoapods does not support the provision of a third-party library for useuse_frameworks!

In that case, I'm going to create an open source for iOS framework project, which is easy for the team and everyone to use.

Use Step STEP1

In the Lzmasdkojbcframework project directory, because the project requires a Inlineobjc library, use Cocoapods for installation, the command line executes the following command:

install
Step2

Open the workspace project file, and what you see in Xcode is as follows:


ImgStep3

Archive the project and export the LzmaSDKObjC.framework file to the desired project path, use the following:

- (void) Testlzma {NSString *sourcefilepath = [NSString stringWithFormat:@ "%@/source_data.txt", System_document_path];NSString *zipfilepath = [NSString stringWithFormat:@ "%@/compressed_data.7z", System_document_path]; Ddlogdebug (@ "\ n \ ********** LZMA ********** \nsrc File:%@\n7zip file:%@\n\n", Sourcefilepath, Zipfilepath);Create writer Lzmasdkobjcwriter * writer = [[Lzmasdkobjcwriter alloc] initwithfileurl:[Nsurl Fileurlwithpath:zipfilepath]];ADD file data ' s or paths[Writer Adddata:[nsdata ...] forpath:@ "MyArchiveFileName.txt"]; ADD file data [writer Addpath:sourcefilepath Forpath:@"."];ADD file at path[Writer addpath:@ "/path/somedirectory" forpath:@ "somedirectory"]; Recursively add directory with all contentsSetup writer writer.delegate =SelfTrack progressWriter.passwordgetter = ^nsstring* (void) {//Password getterreturn @ "1234";// };//Optional settings writer.method = Lzmasdkobjcmethodlzma; //or Lzmasdkobjcmethodlzma writer.solid = YES; Writer.compressionlevel = 7; writer.encodecontent = YES; Writer.encodeheader = yes; writer.compressheader = YES; Writer.compressheaderfull = yes; writer.writemodificationtime = no; writer.writecreationtime = no; writer.writeAccessTime = Span class= "Hljs-literal" >NO; //Open archive file nserror * error = NIL; [Writer open:&error]; //Write archive within current thread [writer Write];}      

The advantage of this approach is the wide range of support options, can be widely customized parameters, but the disadvantage is that after importing the library will lead to the size of the application package becomes larger, so need to choose according to their own needs.

ENJOY.

IOS Lzma class compression algorithm uses

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.