https://github.com/kristofferstrube/blazor.compressionstreams
A Blazor wrapper for the Compression Streams browser API.
https://github.com/kristofferstrube/blazor.compressionstreams
Last synced: about 1 year ago
JSON representation
A Blazor wrapper for the Compression Streams browser API.
- Host: GitHub
- URL: https://github.com/kristofferstrube/blazor.compressionstreams
- Owner: KristofferStrube
- License: mit
- Created: 2023-03-14T00:23:52.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2024-08-04T09:54:04.000Z (almost 2 years ago)
- Last Synced: 2025-05-07T03:04:18.875Z (about 1 year ago)
- Language: C#
- Homepage:
- Size: 8.6 MB
- Stars: 3
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](/LICENSE.md)
[](https://github.com/KristofferStrube/Blazor.CompressionStreams/issues)
[](https://github.com/KristofferStrube/Blazor.CompressionStreams/network/members)
[](https://github.com/KristofferStrube/Blazor.CompressionStreams/stargazers)
[](https://www.nuget.org/packages/KristofferStrube.Blazor.CompressionStreams/)
# Introduction
A Blazor wrapper for the browser API [Compression Streams](https://wicg.github.io/compression/)
The API defines way to compress and decompress streams of binary data. This project implements a wrapper around the API for Blazor so that we can easily and safely compress and decompress streams.
# Demo
The sample project can be demoed at https://kristofferstrube.github.io/Blazor.CompressionStreams/
On each page you can find the corresponding code for the example in the top right corner.
On the [API Coverage Status page](https://kristofferstrube.github.io/Blazor.CompressionStreams/Status) you can see how much of the WebIDL specs this wrapper has covered.
# Getting Started
The package can be used in Blazor projects.
## Prerequisites
You need to install .NET 7.0 or newer to use the library.
[Download .NET 7](https://dotnet.microsoft.com/download/dotnet/7.0)
## Installation
You can install the package via Nuget with the Package Manager in your IDE or alternatively using the command line:
```bash
dotnet add package KristofferStrube.Blazor.CompressionStreams
```
## Import
You need to reference the package in order to use it in your pages. This can be done in `_Import.razor` by adding the following.
```razor
@using KristofferStrube.Blazor.CompressionStreams
```
## Creating wrapper instance
You can create wrapper instances for `CompressionStream` and `DecompressionStream` with the two following sets of constructors which either take a format or a reference to an existing JS CompressionStream.
```csharp
// Compression Stream constructions
var newCompressionStream = await CompressionStream.CreateAsync(JSRuntime, CompressionAlgorithm.Deflate);
IJSObjectReference jSCompressionStream;
var existingCompressionStream = await CompressionStream.CreateAsync(JSRuntime, jSCompressionStream);
// Decompression Stream constructions
var newDecompressionStream = await DecompressionStream.CreateAsync(JSRuntime, CompressionAlgorithm.Deflate);
IJSObjectReference jSDecompressionStream;
var existingDecompressionStream = await DecompressionStream.CreateAsync(JSRuntime, jSDecompressionStream);
```
## Using `CompressionStream` and `DecompressionStream`
The primary usage of the `CompressionStream`s and `DecompressionStream`s are in combination with the [Blazor.Streams](https://github.com/KristofferStrube/Blazor.Streams) library where the `ReadableStream` can be piped through any stream that implements the `IGenericTransformStream` interface which both `CompressionStream` and `DecompressionStream` does.
```csharp
ReadableStream readableStream;
ReadableStream compressed = await readableStream.PipeThroughAsync(newCompressionStream);
ReadableStream decompressed = await compressed.PipeThroughAsync(newDecompressionStream);
```
This can be useful if you have limited bandwidth and want to stream some content.
# Issues
Feel free to open issues on the repository if you find any errors with the package or have wishes for features.
# Related articles
This repository was build with inspiration and help from the following series of articles:
- [Wrapping Compression Streams in Blazor](https://kristoffer-strube.dk/post/wrapping-compression-streams-in-blazor/)
- [Wrapping JavaScript libraries in Blazor WebAssembly/WASM](https://blog.elmah.io/wrapping-javascript-libraries-in-blazor-webassembly-wasm/)
- [Call anonymous C# functions from JS in Blazor WASM](https://blog.elmah.io/call-anonymous-c-functions-from-js-in-blazor-wasm/)
- [Using JS Object References in Blazor WASM to wrap JS libraries](https://blog.elmah.io/using-js-object-references-in-blazor-wasm-to-wrap-js-libraries/)
- [Blazor WASM 404 error and fix for GitHub Pages](https://blog.elmah.io/blazor-wasm-404-error-and-fix-for-github-pages/)
- [How to fix Blazor WASM base path problems](https://blog.elmah.io/how-to-fix-blazor-wasm-base-path-problems/)