File: xz.go

package info (click to toggle)
golang-github-smira-go-xz 0.0~git20150414.0c531f0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 360 kB
  • sloc: makefile: 2
file content (71 lines) | stat: -rw-r--r-- 1,282 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Package xz implements simple .xz decompression using external xz program
//
// No shared library (liblzma) dependencies.
package xz

import (
	"io"
	"os/exec"
)

// Reader does decompression using xz utility
type Reader struct {
	cmd    *exec.Cmd
	input  io.WriteCloser
	output io.ReadCloser
}

// NewReader creates .xz decompression reader
//
// Internally it starts xz program, sets up input and output pipes
func NewReader(src io.Reader) (*Reader, error) {
	var err error

	result := &Reader{}

	result.cmd = exec.Command("xz", "--decompress", "--stdout")
	result.input, err = result.cmd.StdinPipe()
	if err != nil {
		return nil, err
	}
	result.output, err = result.cmd.StdoutPipe()
	if err != nil {
		return nil, err
	}

	err = result.cmd.Start()
	if err != nil {
		return nil, err
	}

	go func() {
		io.Copy(result.input, src)
		result.input.Close()
	}()

	return result, nil
}

// Read implements io.Reader interface
func (r *Reader) Read(p []byte) (n int, err error) {
	return r.output.Read(p)
}

// Close implements io.Closer interface
func (r *Reader) Close() error {
	if r.input != nil {
		r.input.Close()
	}
	if r.output != nil {
		r.output.Close()
	}
	if r.cmd != nil {
		return r.cmd.Wait()
	}
	return nil
}

// Check interface
var (
	_ io.ReadCloser = &Reader{}
)