Skip to content

Commit c73c821

Browse files
craig65535cmaglie
andauthored
Update code examples in docs (#18)
* Update README.md * Update extract.go * Update README.md * Update README.md * Update extract.go Co-authored-by: Cristian Maglie <[email protected]>
1 parent 7464e47 commit c73c821

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

README.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,20 @@
99
Package extract allows to extract archives in zip, tar.gz or tar.bz2 formats
1010
easily.
1111

12-
Most of the time you'll just need to call the proper function with a buffer and
12+
Most of the time you'll just need to call the proper function with a Reader and
1313
a destination:
1414

15+
```go
16+
file, _ := os.Open("path/to/file.tar.bz2")
17+
extract.Bz2(context.TODO, file, "/path/where/to/extract", nil)
18+
```
19+
20+
or also:
21+
1522
```go
1623
data, _ := ioutil.ReadFile("path/to/file.tar.bz2")
1724
buffer := bytes.NewBuffer(data)
18-
extract.Bz2(context.Background(), buffer, "/path/where/to/extract", nil)
25+
extract.Bz2(context.TODO, buffer, "/path/where/to/extract", nil)
1926
```
2027

2128
Sometimes you'll want a bit more control over the files, such as extracting a
@@ -28,13 +35,13 @@ var shift = func(path string) string {
2835
parts = parts[1:]
2936
return strings.Join(parts, string(filepath.Separator))
3037
}
31-
extract.Bz2(data, "/path/where/to/extract", shift)
38+
extract.Bz2(context.TODO, file, "/path/where/to/extract", shift)
3239
```
3340

3441
If you don't know which archive you're dealing with (life really is always a surprise) you can use Archive, which will infer the type of archive from the first bytes
3542

3643
```go
37-
extract.Archive(data, "/path/where/to/extract", nil)
44+
extract.Archive(context.TODO, file, "/path/where/to/extract", nil)
3845
```
3946

4047
If you need more control over how your files will be extracted you can use an Extractor.
@@ -58,5 +65,5 @@ extractor := extract.Extractor{
5865
FS: fs,
5966
}
6067

61-
extractor.Archive(data, "path/where/to/extract", nil)
68+
extractor.Archive(context.TODO, file, "/path/where/to/extract", nil)
6269
```

extract.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
// Package extract allows to extract archives in zip, tar.gz or tar.bz2 formats
22
// easily.
33
//
4-
// Most of the time you'll just need to call the proper function with a buffer and
4+
// Most of the time you'll just need to call the proper function with a Reader and
55
// a destination:
66
//
7-
// data, _ := ioutil.ReadFile("path/to/file.tar.bz2")
8-
// buffer := bytes.NewBuffer(data)
9-
// extract.TarBz2(data, "/path/where/to/extract", nil)
7+
// file, _ := os.Open("path/to/file.tar.bz2")
8+
// extract.Bz2(context.TODO, file, "/path/where/to/extract", nil)
109
// ```
1110
//
1211
// Sometimes you'll want a bit more control over the files, such as extracting a
@@ -18,12 +17,12 @@
1817
// parts = parts[1:]
1918
// return strings.Join(parts, string(filepath.Separator))
2019
// }
21-
// extract.TarBz2(data, "/path/where/to/extract", shift)
20+
// extract.Bz2(context.TODO, file, "/path/where/to/extract", shift)
2221
// ```
2322
//
2423
// If you don't know which archive you're dealing with (life really is always a surprise) you can use Archive, which will infer the type of archive from the first bytes
2524
//
26-
// extract.Archive(data, "/path/where/to/extract", nil)
25+
// extract.Archive(context.TODO, file, "/path/where/to/extract", nil)
2726
package extract
2827

2928
import (

0 commit comments

Comments
 (0)