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

Objects that Simplify Multiple-Step Compression

Simplify encoding and decoding streams of data using Compression classes for Swift.

Compressing and Decompressing Data with Input and Output Filters

Compress and decompress streamed or from-memory data, using input and output filters.

Compressing and Decompressing Files with Swift Stream Compression

Perform compression for all files and decompression for files with supported extension types.

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.

compression_stream

A structure representing a compression stream.

compression_stream_init

Initializes a compression stream for either compression or decompression.

compression_stream_process

Performs compression or decompression using an initialized compression stream structure.

compression_stream_destroy

Frees any memory allocated by stream initialization function.

compression_status

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

compression_stream_flags

A set of values used to represent stream compression flags.

compression_stream_operation

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

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.

compression_encode_scratch_buffer_size

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

compression_encode_buffer

Compresses the contents of a source buffer into a destination buffer.

compression_decode_scratch_buffer_size

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

compression_decode_buffer

Decompresses the contents of a source buffer into a destination buffer.

Compression Algorithm Specification

compression_algorithm

A structure for values that represent compression algorithms.