quicklz

package module
v0.0.0-...-59904ab Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 15, 2019 License: GPL-3.0 Imports: 3 Imported by: 1

README

go-quicklz

GoDoc

go-quicklz is a complete port of QuickLZ compression algorithm in go.

The official website for QuickLZ is available here.

Installation

go get github.com/Hiroko103/go-quicklz

Using

import "github.com/Hiroko103/go-quicklz"

Features

  • Supports all compression levels and buffering modes
  • Memory safe

Notices

Be aware that compressing files with STREAMING_BUFFER_0 mode will require an equal sized buffer.

If you plan to compress large files (>100 MB), use either STREAMING_BUFFER_100000 or STREAMING_BUFFER_1000000 mode.

Examples

File compress

qlz, err := quicklz.New(quicklz.COMPRESSION_LEVEL_1, quicklz.STREAMING_BUFFER_0)
if err != nil {
    fmt.Println(err)
    return
}
f, _ := os.Open(filePath)
source, _ := ioutil.ReadAll(f)
f.Close()
destination := make([]byte, len(source) + 400)
compressed_size, err := qlz.Compress(&source, &destination)
if err != nil {
    fmt.Println(err)
    return
}
compressed_data := destination[:compressed_size]
ioutil.WriteFile("outputFile", compressed_data, 0777)

File decompress

qlz, err := quicklz.New(quicklz.COMPRESSION_LEVEL_1, quicklz.STREAMING_BUFFER_0)
if err != nil {
    fmt.Println(err)
    return
}
f, _ := os.Open(compressedFile)
source, _ := ioutil.ReadAll(f)
f.Close()
decompressed_size := quicklz.Size_decompressed(&source)
destination := make([]byte, decompressed_size)
decompressed_size, err = qlz.Decompress(&source, &destination)
if err != nil {
    fmt.Println(err)
    return
}
ioutil.WriteFile("originalFile", destination, 0777)

Stream compress

qlz, err := quicklz.New(quicklz.COMPRESSION_LEVEL_1, quicklz.STREAMING_BUFFER_100000)
if err != nil {
    fmt.Println(err)
    return
}
f, _ := os.Open(filePath)
source := make([]byte, 10000)
destination := make([]byte, len(source) + 400)
of, _ := os.Create("compressedStream")
for {
    n, err := f.Read(source)
    if err != nil {
        if err == io.EOF {
            break
        }
    }
    part := source[:n]
    compressed_size, _ := qlz.Compress(&part, &destination)
    of.Write(destination[:compressed_size])
}
f.Close()
of.Close()

Stream decompress

qlz, err := quicklz.New(quicklz.COMPRESSION_LEVEL_1, quicklz.STREAMING_BUFFER_100000)
if err != nil {
    fmt.Println(err)
    return
}
f, _ := os.Open(compressedStream)
source := make([]byte, 10000 + 400)
destination := make([]byte, 10000)
of, _ := os.Create("originalFile")
for {
    header := source[:9]
    n, err := f.Read(header)
    if err != nil {
        if err == io.EOF {
            break
        }
    }
    if n != 9 {
        break
    }
    c := quicklz.Size_compressed(&header)
    f.Read(source[9:c])
    part := source[:c]
    d, _ := qlz.Decompress(&part, &destination)
    of.Write(destination[:d])
}
f.Close()
of.Close()

Credit

All credit goes to Lasse Mikkel Reinhold (lar@quicklz.com), the author of the original C version.

License

This software is released under the GNU General Public License v3.0.

Permissions of this strong copyleft license are conditioned on making available complete source code of licensed works and modifications, which include larger works using a licensed work, under the same license. Copyright and license notices must be preserved. Contributors provide an express grant of patent rights.

Refer to the LICENSE file for the complete text.

Documentation

Index

Constants

View Source
const (
	COMPRESSION_LEVEL_1 = 1
	COMPRESSION_LEVEL_2 = 2
	COMPRESSION_LEVEL_3 = 3
)
View Source
const (
	STREAMING_BUFFER_0       = 0
	STREAMING_BUFFER_100000  = 100000
	STREAMING_BUFFER_1000000 = 1000000
)

Variables

This section is empty.

Functions

func Size_compressed

func Size_compressed(source *[]byte) int64

Get the size of the compressed data in source buffer

func Size_decompressed

func Size_decompressed(source *[]byte) int64

Get the size of the data in source buffer after decompression

Types

type Qlz

type Qlz struct {
	// contains filtered or unexported fields
}

func New

func New(compression_level uint, streaming_buffer uint) (*Qlz, error)

Create new compressor/decompressor

func (*Qlz) Compress

func (q *Qlz) Compress(source, destination *[]byte) (int64, error)

Compress the data in source to destination and return the compressed data length

func (*Qlz) Decompress

func (q *Qlz) Decompress(source, destination *[]byte) (int64, error)

Decompress the data in source to destination and return the decompressed data length

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL