Leverage common compression algorithms for lossless data compression.


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.


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.

class InputFilter

An encoder-decoder that reads input data from a stream.

class OutputFilter

An encoder-decoder that writes output data to a stream.

enum Algorithm

Algorithms used for compression or decompression.

enum FilterError

Errors that occur during compression.

enum FilterOperation

Operations that define whether input and output filters compress or decompress data.


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.

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.

Compression Algorithm Specification

struct compression_algorithm

A structure for values that represent compression algorithms.

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software