Giter Site home page Giter Site logo

go-quicklz's Introduction

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 ([email protected]), 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.

go-quicklz's People

Contributors

hiroko103 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.