-
Notifications
You must be signed in to change notification settings - Fork 7
/
example_test.go
47 lines (38 loc) · 1004 Bytes
/
example_test.go
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
package targz_test
import (
"io/ioutil"
"os"
"path/filepath"
"github.com/walle/targz"
)
func Example() {
// Create a temporary file structiure we can use
tmpDir, dirToCompress := createExampleData()
// Comress a folder to my_archive.tar.gz
err := targz.Compress(dirToCompress, filepath.Join(tmpDir, "my_archive.tar.gz"))
if err != nil {
panic(err)
}
// Extract my_archive.tar.gz to a new folder called extracted
err = targz.Extract(filepath.Join(tmpDir, "my_archive.tar.gz"), filepath.Join(tmpDir, "extracted"))
if err != nil {
panic(err)
}
}
func createExampleData() (string, string) {
tmpDir, err := ioutil.TempDir("", "targz-example")
if err != nil {
panic(err)
}
directory := filepath.Join(tmpDir, "my_folder")
subDirectory := filepath.Join(directory, "my_sub_folder")
err = os.MkdirAll(subDirectory, 0755)
if err != nil {
panic(err)
}
_, err = os.Create(filepath.Join(subDirectory, "my_file.txt"))
if err != nil {
panic(err)
}
return tmpDir, directory
}