forked from astaxie/gopkg
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2d927c5
commit 687ef3a
Showing
4 changed files
with
111 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
# Comprss | ||
|
||
二级包列表 | ||
|
||
- [compress/gzip](gzip) | ||
# Comprss | ||
|
||
二级包列表 | ||
|
||
- [compress/bzip2](bzip2) | ||
- [compress/flate](flate) | ||
- [compress/gzip](gzip) | ||
- [compress/lzw](lzw) | ||
- [compress/zlib](zlib) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# func NewReader(r io.Reader) io.Reader | ||
|
||
参数列表: | ||
|
||
- r bzip2压缩数据 | ||
|
||
返回值:解压后的数据 | ||
|
||
功能说明: | ||
|
||
返回一个从r读取bzip2压缩数据,然后返回一个解压后io.Reader | ||
|
||
示例: | ||
|
||
package main | ||
|
||
import ( | ||
"archive/tar" | ||
"compress/bzip2" | ||
"fmt" | ||
"io" | ||
"os" | ||
"path" | ||
) | ||
|
||
func main() { | ||
//打开一个bz2压缩文件 | ||
bzip2File, _ := os.Open("demo.tar.bz2") | ||
defer bzip2File.Close() | ||
|
||
//进行解压 | ||
r := bzip2.NewReader(bzip2File) | ||
//使用tar读取输出文件 | ||
tr := tar.NewReader(r) | ||
for { | ||
tarHead, err := tr.Next() | ||
if err == io.EOF { | ||
break | ||
} | ||
if err != nil { | ||
fmt.Println("the tar file err is ", err) | ||
continue | ||
} | ||
fmt.Println(tarHead.Name) | ||
|
||
os.MkdirAll(path.Dir(tarHead.Name), os.ModePerm) | ||
fw, _ := os.Create(tarHead.Name) | ||
defer fw.Close() | ||
|
||
_, err = io.Copy(fw, tr) | ||
if err != nil { | ||
fmt.Println("copy file err is ", err) | ||
continue | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
# 包名 | ||
|
||
函数列表 | ||
|
||
- xxx1 | ||
- xxx2 | ||
# 包名 compress/bzip2 | ||
|
||
函数列表 | ||
|
||
- [func NewReader(r io.Reader) io.Reader](NewReader.md) | ||
- [func (s StructuralError) Error() string](StructuralError.Error.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# func (s StructuralError) Error() string | ||
|
||
返回值:非法的bzip2数据错误信息 | ||
|
||
功能说明:StructuralError其实是一个string,他实现了error接口,用于很方便的返回非法的bzip2数据的错误信息 | ||
|
||
示例: | ||
|
||
package main | ||
|
||
import ( | ||
"archive/tar" | ||
"compress/bzip2" | ||
"fmt" | ||
"io" | ||
"log" | ||
"os" | ||
"reflect" | ||
) | ||
|
||
func main() { | ||
//打开一个非bzip2压缩文件 | ||
bzip2File, _ := os.Open("main.go") | ||
defer bzip2File.Close() | ||
|
||
//尝试进行解压 | ||
r := bzip2.NewReader(bzip2File) | ||
//使用tar读取输出文件 | ||
tr := tar.NewReader(r) | ||
for { | ||
_, err := tr.Next() | ||
if err == io.EOF { | ||
break | ||
} | ||
if err != nil { | ||
fmt.Println("the type is ", reflect.TypeOf(err)) | ||
log.Fatalln(err) | ||
} | ||
} | ||
} |