Framework

Compression

Leverage common compression algorithms for lossless data compression.

Overview

The Compression framework enables your app to provide lossless compression when saving or sharing files and data. Compression is a process in which you compress (encode) and decompress (decode) data. For example, a text editor may save its files in a compressed format, and automatically decompress the saved file when opened by the user.

Graphic illustrating how original data is encoded to compressed data, then decoded back to its original decompressed form.

The framework offers two methods of compression:

  • Buffer compression uses a single-step method for compressing files, making it perfect for use with uncompressed files under 8 MB, or compressed files under 1 MB.

  • Stream compression uses multiple steps for compressing files, making it ideal for compressing larger files or streamed data, such as an incoming audio signal or downloading files.

To use buffer compression, you compress or decompress the input data with one call to the corresponding function. To learn more about buffer compression, including a walk-through of the code used to encode and decode a string, see Compressing and Decompressing Data with Buffer Compression.

To use stream compression, you call the compression or decompression function repeatedly to compress or decompress data from a source buffer to a destination buffer. Between calls, the compressor or decompressor moves processed data out of the source buffer and loads new data into the destination buffer. To learn more about stream compression, see the sample code project Compressing and Decompressing Files with Stream Compression.

Topics

Single-Step Compression

Buffer compression functions compress or decompress a block of data stored contiguously in memory.

Compressing and Decompressing Data with Buffer Compression

Compress a string, write it to the file system, and decompress the same file using buffer compression.

func compression_encode_scratch_buffer_size(compression_algorithm) -> Int

Returns the required compression scratch buffer size for the selected algorithm.

func compression_decode_scratch_buffer_size(compression_algorithm) -> Int

Returns the required decompression scratch buffer size for the selected algorithm.

Multiple-Step Compression

Stream compression functions compress or decompress sequential blocks of data.

Compressing and Decompressing Files with Stream Compression

Perform compression or the appropriate kind of decompression to a file based on its path extension.

struct compression_stream

A structure representing a compression stream.

func compression_stream_process(UnsafeMutablePointer<compression_stream>, Int32) -> compression_status

Performs compression or decompression using an initialized compression stream structure.

struct compression_status

A set of values used to represent the status of stream compression.

struct compression_stream_flags

A set of values used to represent stream compression flags.

struct compression_stream_operation

A set of values used to represent a stream compression operation.

Compression Algorithm Specification

struct compression_algorithm

A structure for values that represent compression algorithms.