Skip to content

Commit

Permalink
compress和compress/bzip2包完成
Browse files Browse the repository at this point in the history
  • Loading branch information
flysnoworg committed Jan 31, 2015
1 parent 2d927c5 commit 687ef3a
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 11 deletions.
14 changes: 9 additions & 5 deletions compress/README.md
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)
56 changes: 56 additions & 0 deletions compress/bzip2/NewReader.md
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
}
}
}
12 changes: 6 additions & 6 deletions compress/bzip2/README.md
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)
40 changes: 40 additions & 0 deletions compress/bzip2/StructuralError.Error.md
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)
}
}
}

0 comments on commit 687ef3a

Please sign in to comment.