Skip to content
This repository has been archived by the owner on Nov 22, 2022. It is now read-only.
/ swift-lz4 Public archive

Cross-platform Swift LZ4 (de)compression

License

Notifications You must be signed in to change notification settings

tuplestream/swift-lz4

Repository files navigation

LZ4 For Swift

CircleCI Gitter

This is an LZ4 implementation that directly wraps Yann Collet's C implementation. It is completely separate from Apple's own Compression framework and additionally is tested on Linux.

Getting started

Installing liblz4

You need the lz4 library present for this package to link against. You can get it:

  • By building from source: grab it from here, cd into the cloned folder and follow the installation instructions
  • By installing via a package manager, e.g. on recent versions of Ubuntu this can be done via apt-get install zlib1g-dev

Adding the package

swift-lz4 uses SwiftPM as its build tool. Add the package in the usual way, first with a new dependencies clause:

dependencies: [
    .package(url: "https://github.com/tuplestream/swift-lz4.git", from: "0.1.0")
]

then add the LZ4 module to your target dependencies:

dependencies: [.product(name: "LZ4", package: "swift-lz4"),]

Integrating in your code

// 1) Import the LZ4 module
import LZ4
// 2) Import the higher level LZ4NIO module if you want to work with ByteBuffers
import LZ4NIO

Simple example

// 3) Using NIO extensions is the most convenient way to get started
import NIO
import LZ4NIO

let allocator = ByteBufferAllocator()
let inputBuffer = allocator.buffer(string: "hello, world")
// return another ByteBuffer with LZ4 compression applied
let compressedBuffer = inputBuffer.lz4Compress()

let decompressed = compressedBuffer.lz4Decompress()

// "hello, world"
print(decompressed.readString(length: decompressed.readableBytes))

Local development

Building and testing

Requirements:

  • Swift 5.3 toolchain
  • liblz4 (detailed above)

Building swift-lz4 is like any other Swift package.

  • clone the repository: https://github.com/tuplestream/swift-lz4.git (feel free to fork it)
  • cd into the repository root and build: cd swift-lz4 && swift build

Contributing

Create a branch off master and submit a pull request.

Cross-platform compatibility

Swift-lz4 is designed to run on Linux as well as MacOS. You can build and test it on Linux via the tuplestream base Docker image (you will need the Docker runtime installed on your machine) by running the linux-test.bash script in the repository root. CircleCI also runs these tests on PR builds.

About

Cross-platform Swift LZ4 (de)compression

Resources

License

Stars

Watchers

Forks

Languages