A Ktor server plugin that provides HTTP response compression and request decompression capabilities with support for gzip, deflate, and identity encoding algorithms.
—
The Compression plugin supports multiple HTTP compression algorithms through the ContentEncoder interface, with built-in implementations for standard algorithms.
fun CompressionConfig.gzip(block: CompressionEncoderBuilder.() -> Unit = {})Configures gzip compression (RFC 1952) with default priority 1.0:
install(Compression) {
gzip {
priority = 0.9
minimumSize(1024)
matchContentType(ContentType.Text.Plain, ContentType.Application.Json)
}
}fun CompressionConfig.deflate(block: CompressionEncoderBuilder.() -> Unit = {})Configures deflate compression (RFC 1951) with default priority 0.9:
install(Compression) {
deflate {
priority = 0.8
excludeContentType(ContentType.Image.Any)
}
}fun CompressionConfig.identity(block: CompressionEncoderBuilder.() -> Unit = {})Configures identity encoding (no compression) as a fallback option:
install(Compression) {
identity {
// Identity encoding typically used as fallback
priority = 0.1
}
}class CompressionEncoderBuilder internal constructor(
val encoder: ContentEncoder
) : ConditionsHolderBuilder {
override val conditions: ArrayList<ApplicationCall.(OutgoingContent) -> Boolean> = arrayListOf()
var priority: Double = 1.0
internal fun buildConfig(): CompressionEncoderConfig
}Encoders are selected based on client Accept-Encoding headers and configured priorities. Higher priority encoders are preferred when multiple options are available:
install(Compression) {
gzip {
priority = 1.0 // Highest priority
}
deflate {
priority = 0.9 // Second choice
}
identity {
priority = 0.1 // Fallback
}
}Each encoder can have specific conditions that must be met for it to be used:
install(Compression) {
gzip {
// Only compress JSON responses larger than 500 bytes
minimumSize(500)
matchContentType(ContentType.Application.Json)
}
deflate {
// Compress text content but exclude certain types
matchContentType(ContentType.Text.Any)
excludeContentType(ContentType.Text.EventStream)
condition { _, content ->
content.contentLength?.let { it > 100 } ?: true
}
}
}The underlying encoder implementations from io.ktor.util:
interface Encoder {
fun encode(source: ByteReadChannel, coroutineContext: CoroutineContext = EmptyCoroutineContext): ByteReadChannel
fun encode(source: ByteWriteChannel, coroutineContext: CoroutineContext = EmptyCoroutineContext): ByteWriteChannel
fun decode(source: ByteReadChannel, coroutineContext: CoroutineContext = EmptyCoroutineContext): ByteReadChannel
}
interface ContentEncoder : Encoder {
val name: String
fun predictCompressedLength(contentLength: Long): Long?
}
object GZipEncoder : ContentEncoder {
override val name: String = "gzip"
}
object DeflateEncoder : ContentEncoder {
override val name: String = "deflate"
}
object IdentityEncoder : ContentEncoder {
override val name: String = "identity"
override fun predictCompressedLength(contentLength: Long): Long = contentLength
}The plugin automatically handles HTTP content negotiation:
Accept-Encoding headerContent-Encoding response headerClient sends: Accept-Encoding: gzip, deflate;q=0.8, *;q=0.1
Server response: Content-Encoding: gzipThe server will prefer gzip (quality 1.0) over deflate (quality 0.8) when both are available and meet the configured conditions.
Install with Tessl CLI
npx tessl i tessl/maven-io-ktor--ktor-server-compression-jvm