Giter Club home page Giter Club logo

brotli-go's Introduction

Go bindings for the Brotli compression library

GoDoc Build Status

See https://github.com/google/brotli for the upstream C/C++ source, and the VERSION.md file to find out the currently vendored version.

Usage

To use the bindings, you just need to import the enc or dec package and call the Go wrapper functions enc.CompressBuffer or dec.DecompressBuffer

Naive compression + decompression example with no error handling:

import (
	"gopkg.in/kothar/brotli-go.v0/dec"
	"gopkg.in/kothar/brotli-go.v0/enc"
)

func brotliRoundtrip(input []byte) []byte {
  // passing nil to get default *BrotliParams
  // careful, q=11 is the (extremely slow) default
  compressed, _ := enc.CompressBuffer(nil, input, make([]byte, 0))
  decompressed, _ := dec.DecompressBuffer(compressed, make([]byte, 0))
  return decompressed
}

For a more complete roundtrip example, read top-level file brotli_test.go

The enc.BrotliParams type lets you specify various Brotli parameters, such as quality, lgwin (sliding window size), and lgblock (input block size).

import (
	"gopkg.in/kothar/brotli-go.v0/enc"
)

func brotliFastCompress(input []byte) []byte {
  params := enc.NewBrotliParams()
  // brotli supports quality values from 0 to 11 included
  // 0 is the fastest, 11 is the most compressed but slowest
  params.SetQuality(0)
  compressed, _ := enc.CompressBuffer(params, input, make([]byte, 0))
  return compressed
}

Advanced usage (streaming API)

When the data set is too large to fit in-memory, CompressBuffer and DecompressBuffer are not a viable option.

brotli-go also exposes a streaming interface both for encoding:

import (
	"gopkg.in/kothar/brotli-go.v0/enc"
)

func main() {
  compressedWriter,_ := os.OpenFile("data.bin.bro", os.O_CREATE|os.O_WRONLY, 0644)

  brotliWriter := enc.NewBrotliWriter(nil, compressedWriter)
  // BrotliWriter will close writer passed as argument if it implements io.Closer
  defer brotliWriter.Close()

  fileReader, _ := os.Open("data.bin")
  defer fileReader.Close()

  io.Copy(brotliWriter,fileReader)
}

..and for decoding:

import (
	"gopkg.in/kothar/brotli-go.v0/dec"
)

func main() {
  archiveReader, _ := os.Open("data.bin.bro")

  brotliReader := dec.NewBrotliReader(archiveReader)
  defer brotliReader.Close()

  decompressedWriter,_ := os.OpenFile("data.bin.unbro", os.O_CREATE|os.O_WRONLY, 0644)
  defer decompressedWriter.Close()
  io.Copy(decompressedWriter, brotliReader)
}

Bindings

This is a very basic Cgo wrapper for the enc and dec directories from the Brotli sources. I've made a few minor changes to get things working with Go.

  1. The default dictionary has been extracted to a separate 'shared' package to allow linking the enc and dec cgo modules if you use both. Otherwise there are duplicate symbols, as described in the dictionary.h header files.

  2. The dictionary variable name for the dec package has been modified for the same reason, to avoid linker collisions.

Links

License

Brotli and these bindings are open-sourced under the MIT License - see the LICENSE file.

brotli-go's People

Contributors

kothar avatar fasterthanlime avatar dsnet avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.