Skip to content

ensody/Kompressor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Kompressor - Kotlin Multiplatform compression algorithms

Maven Central

This project provides compression algorithms for Kotlin Multiplatform.

The underlying native libraries are provided by the NativeBuilds project.

Supported platforms

  • Android
  • JVM
  • All native (iOS, macOS, Linux, Windows, tvOS, watchOS)

Supported algorithms

  • zstd/ZStandard
  • deflate (zlib & raw)
  • gzip
  • brotli
  • ...more planned... (lz4, snappy, bzip2, bzip3)

Installation

Add the package to your build.gradle.kts's dependencies {}:

dependencies {
    // Add the BOM using the desired library version
    api(platform("com.ensody.kompressor:kompressor-bom:VERSION"))

    // zstd/Zstandard
    api("com.ensody.kompressor:kompressor-zstd--nativelib")
    // Select the native zstd library version from NativeBuilds
    api("com.ensody.nativebuilds:zstd-libzstd:1.5.7.4")
    // ZstdContentEncoder for Ktor (optional)
    api("com.ensody.kompressor:kompressor-zstd-ktor")

    // zlib (deflate & gzip)
    api("com.ensody.kompressor:kompressor-zlib--nativelib")
    // Select the native zlib library version from NativeBuilds
    api("com.ensody.nativebuilds:zlib-libz:1.3.1.4")
    // DeflateContentEncoder and GzipContentEncoder for Ktor (optional)
    api("com.ensody.kompressor:kompressor-zlib-ktor")

    // brotli
    api("com.ensody.kompressor:kompressor-brotli--nativelib")
    // Select the native brotli library version from NativeBuilds
    api("com.ensody.nativebuilds:brotli-libbrotlicommon:1.2.0")
    api("com.ensody.nativebuilds:brotli-libbrotlienc:1.2.0")
    api("com.ensody.nativebuilds:brotli-libbrotlidec:1.2.0")
    // BrotliContentEncoder for Ktor (optional)
    api("com.ensody.kompressor:kompressor-brotli-ktor")
}

IMPORTANT: One reason why you should explicitly define the NativeBuilds library version is that dependabot and other automatic update tools might otherwise not suggest updates of native libraries. Since these are usually written in C/C++ or possibly unsafe Rust, you really want to stay up to date with the latest security fixes.

Also, make sure you've integrated the Maven Central repo, e.g. in your root build.gradle.kts:

subprojects {
    repositories {
        // ...
        mavenCentral()
        // ...
    }
}

Example: zstd

Every compression algorithm implements the SliceTransform interface.

If the data fits in RAM you can simply call the .transform(ByteArray) extension function:

val testData: ByteArray = Random.nextBytes(128 * 1024 + 3)
val compressed: ByteArray = ZstdCompressor(compressionLevel = 3).transform(testData)
val decompressed: ByteArray = ZstdDecompressor().transform(compressed)
assertContentEquals(testData, decompressed)

Normally, you'll want to stream the data, so even large inputs fit in RAM. While SliceTransform provides a low-level API for this, let's better use kotlinx-io:

val source: Source = ...
val sink: Sink = ...
source.pipe(ZstdCompressor(compressionLevel = 3)).buffered().use { it.transferTo(sink) }

Or for decompression:

source.pipe(ZstdDecompressor()).buffered().use { it.transferTo(sink) }

You can also build a pipe with a sink:

ZstdDecompressor().pipe(sink).buffered().use { decompressor ->
    source.transferTo(decompressor)
}

Example: zstd with Ktor

val client = HttpClient(CIO) {
    install(ContentEncoding) {
        customEncoder(ZstdContentEncoder(compressionLevel = 1), 1.0f)
    }
}

License

Copyright 2025 Ensody GmbH, Waldemar Kornewald

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.