]>
Commit | Line | Data |
---|---|---|
15c0b25d AP |
1 | # Package xz |
2 | ||
3 | This Go language package supports the reading and writing of xz | |
4 | compressed streams. It includes also a gxz command for compressing and | |
5 | decompressing data. The package is completely written in Go and doesn't | |
6 | have any dependency on any C code. | |
7 | ||
8 | The package is currently under development. There might be bugs and APIs | |
9 | are not considered stable. At this time the package cannot compete with | |
10 | the xz tool regarding compression speed and size. The algorithms there | |
11 | have been developed over a long time and are highly optimized. However | |
12 | there are a number of improvements planned and I'm very optimistic about | |
13 | parallel compression and decompression. Stay tuned! | |
14 | ||
107c1cdb | 15 | ## Using the API |
15c0b25d AP |
16 | |
17 | The following example program shows how to use the API. | |
18 | ||
107c1cdb ND |
19 | ```go |
20 | package main | |
21 | ||
22 | import ( | |
23 | "bytes" | |
24 | "io" | |
25 | "log" | |
26 | "os" | |
27 | ||
28 | "github.com/ulikunitz/xz" | |
29 | ) | |
30 | ||
31 | func main() { | |
32 | const text = "The quick brown fox jumps over the lazy dog.\n" | |
33 | var buf bytes.Buffer | |
34 | // compress text | |
35 | w, err := xz.NewWriter(&buf) | |
36 | if err != nil { | |
37 | log.Fatalf("xz.NewWriter error %s", err) | |
15c0b25d | 38 | } |
107c1cdb ND |
39 | if _, err := io.WriteString(w, text); err != nil { |
40 | log.Fatalf("WriteString error %s", err) | |
41 | } | |
42 | if err := w.Close(); err != nil { | |
43 | log.Fatalf("w.Close error %s", err) | |
44 | } | |
45 | // decompress buffer and write output to stdout | |
46 | r, err := xz.NewReader(&buf) | |
47 | if err != nil { | |
48 | log.Fatalf("NewReader error %s", err) | |
49 | } | |
50 | if _, err = io.Copy(os.Stdout, r); err != nil { | |
51 | log.Fatalf("io.Copy error %s", err) | |
52 | } | |
53 | } | |
54 | ``` | |
15c0b25d | 55 | |
107c1cdb | 56 | ## Using the gxz compression tool |
15c0b25d AP |
57 | |
58 | The package includes a gxz command line utility for compression and | |
59 | decompression. | |
60 | ||
61 | Use following command for installation: | |
62 | ||
63 | $ go get github.com/ulikunitz/xz/cmd/gxz | |
64 | ||
65 | To test it call the following command. | |
66 | ||
67 | $ gxz bigfile | |
68 | ||
69 | After some time a much smaller file bigfile.xz will replace bigfile. | |
70 | To decompress it use the following command. | |
71 | ||
72 | $ gxz -d bigfile.xz | |
73 |