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.
完成compress、compress/flate、compress/lzw包示例
- Loading branch information
1 parent
687ef3a
commit 30eb3f0
Showing
17 changed files
with
495 additions
and
16 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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# func (e CorruptInputError) Error() string | ||
|
||
返回值:在输入的指定偏移量位置存在损坏的错误信息 | ||
|
||
功能说明:CorruptInputError其实是一个int64,他实现了error接口,用于很方便的返回输入的指定偏移量位置存在损坏的错误信息 | ||
|
||
示例: | ||
|
||
可能是一些读取、写入、拷贝等函数返回的error信息,这里不再举例。 |
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,9 @@ | ||
# func (e InternalError) Error() string | ||
|
||
返回值:表示flate数据自身的错误信息 | ||
|
||
功能说明:InternalError其实是一个string,他实现了error接口,用于很方便的返回flate数据自身的错误信息 | ||
|
||
示例: | ||
|
||
函数返回的error信息,这里不再举例。 |
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,46 @@ | ||
# func NewReader(r io.Reader) io.ReadCloser | ||
|
||
参数列表 | ||
|
||
- r DEFLATE压缩的数据 | ||
|
||
返回值:解压后的ReadCloser数据 | ||
|
||
功能说明: | ||
|
||
从r读取DEFLATE压缩数据,返回一个解压过的io.ReadCloser,使用后需要调用者关闭该io.ReadCloser | ||
|
||
示例: | ||
|
||
package main | ||
|
||
import ( | ||
"bytes" | ||
"compress/flate" | ||
"fmt" | ||
"io" | ||
"log" | ||
"os" | ||
) | ||
|
||
func main() { | ||
//一个缓冲区存储压缩的内容 | ||
buf := bytes.NewBuffer(nil) | ||
|
||
//创建一个flate.Writer | ||
flateWrite, err := flate.NewWriter(buf, flate.BestCompression) | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
defer flateWrite.Close() | ||
//写入待压缩内容 | ||
flateWrite.Write([]byte("compress/flate\n")) | ||
flateWrite.Flush() | ||
fmt.Println(buf) | ||
|
||
//解压刚刚压缩的内容 | ||
flateReader := flate.NewReader(buf) | ||
defer flateReader.Close() | ||
//输出 | ||
io.Copy(os.Stdout, flateReader) | ||
} |
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,48 @@ | ||
# func NewReaderDict(r io.Reader, dict []byte) io.ReadCloser | ||
|
||
参数列表 | ||
|
||
- r DEFLATE压缩的数据 | ||
- dict 解压数据时预设的字典,和NewWriterDict函数里多的dict相同 | ||
|
||
返回值:解压后的ReadCloser数据 | ||
|
||
功能说明: | ||
|
||
从r读取DEFLATE压缩数据,使用预设的dict字典解压数据,返回一个解压过的io.ReadCloser,使用后需要调用者关闭该io.ReadCloser。主要用来读取NewWriterDict压缩的数据 | ||
|
||
示例: | ||
|
||
package main | ||
|
||
import ( | ||
"bytes" | ||
"compress/flate" | ||
"fmt" | ||
"io" | ||
"log" | ||
"os" | ||
) | ||
|
||
func main() { | ||
//一个缓冲区存储压缩的内容 | ||
buf := bytes.NewBuffer(nil) | ||
|
||
//创建一个flate.Writer | ||
flateWrite, err := flate.NewWriterDict(buf, flate.BestCompression, []byte("key")) | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
defer flateWrite.Close() | ||
//写入待压缩内容 | ||
flateWrite.Write([]byte("compress/flate\n")) | ||
flateWrite.Flush() | ||
fmt.Println(buf) | ||
|
||
//解压刚刚压缩的内容 | ||
flateReader := flate.NewReaderDict(buf, []byte("key")) | ||
//flateReader := flate.NewReader(buf) | ||
defer flateReader.Close() | ||
//输出 | ||
io.Copy(os.Stdout, flateReader) | ||
} |
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,44 @@ | ||
# func NewWriter(w io.Writer, level int) (*Writer, error) | ||
|
||
参数列表; | ||
|
||
* w 输出数据的Writer | ||
* level 压缩级别 | ||
|
||
返回列表: | ||
|
||
* *Writer 基于压缩级别新生成的压缩数据的Writer | ||
* error 该函数的错误信息 | ||
|
||
功能说明: | ||
|
||
该函数返回一个压缩级别为level的新的压缩用的Writer。压缩级别的范围是1 (BestSpeed) to 9 (BestCompression)。压缩效果越好的意味着压缩速度越慢。0 (NoCompression)表示不做任何压缩;仅仅只需要添加必要的DEFLATE信息。-1 (DefaultCompression)表示用默认的压缩级别。 | ||
如果压缩级别在-1到9的范围内,error返回nil,否则将返回非nil的错误信息 | ||
|
||
示例: | ||
|
||
package main | ||
|
||
import ( | ||
"bytes" | ||
"compress/flate" | ||
"fmt" | ||
"log" | ||
) | ||
|
||
func main() { | ||
//一个缓冲区存储压缩的内容 | ||
buf := bytes.NewBuffer(nil) | ||
|
||
//创建一个flate.Writer,压缩级别最好 | ||
flateWrite, err := flate.NewWriter(buf, flate.BestCompression) | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
defer flateWrite.Close() | ||
//写入待压缩内容 | ||
flateWrite.Write([]byte("compress/flate\n")) | ||
flateWrite.Flush() | ||
fmt.Println(buf) | ||
|
||
} |
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,44 @@ | ||
# func NewWriterDict(w io.Writer, level int, dict []byte) (*Writer, error) | ||
|
||
参数列表; | ||
|
||
* w 输出数据的Writer | ||
* level 压缩级别 | ||
* dict 压缩预设字典 | ||
|
||
返回列表: | ||
|
||
* *Writer 基于压缩级别和预设字典新生成的压缩数据的Writer | ||
* error 该函数的错误信息 | ||
|
||
功能说明: | ||
|
||
该函数和NewWriter差不多,只不过使用了预设字典进行初始化Writer,使用该Writer压缩的数据只能被使用同样字典初始化的Reader解压。可以实现基于密码的解压缩。 | ||
|
||
示例: | ||
|
||
package main | ||
|
||
import ( | ||
"bytes" | ||
"compress/flate" | ||
"fmt" | ||
"log" | ||
) | ||
|
||
func main() { | ||
//一个缓冲区存储压缩的内容 | ||
buf := bytes.NewBuffer(nil) | ||
|
||
//创建一个flate.Writer,压缩级别最好 | ||
flateWrite, err := flate.NewWriterDict(buf, flate.BestCompression,[]byte("key")) | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
defer flateWrite.Close() | ||
//写入待压缩内容 | ||
flateWrite.Write([]byte("compress/flate\n")) | ||
flateWrite.Flush() | ||
fmt.Println(buf) | ||
|
||
} |
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,26 @@ | ||
# 包名 | ||
|
||
函数列表 | ||
|
||
- xxx1 | ||
- xxx2 | ||
# 包名 compress/flate | ||
|
||
常量列表 | ||
|
||
const ( | ||
NoCompression = 0//不雅所 | ||
BestSpeed = 1//最快速度压缩 | ||
|
||
BestCompression = 9//最佳压缩比压缩 | ||
DefaultCompression = -1//默认压缩 | ||
) | ||
|
||
函数列表 | ||
|
||
- [func NewReader(r io.Reader) io.ReadCloser](NewReader.mds) | ||
- [func NewReaderDict(r io.Reader, dict []byte) io.ReadCloser](NewReaderDict.md) | ||
- [func (e CorruptInputError) Error() string](CorruptInputError.Error.md) | ||
- [func (e InternalError) Error() string](InternalError.Error.md) | ||
- [func (e *ReadError) Error() string](ReadError.Error.md) | ||
- [func (e *WriteError) Error() string](WriteError.Error.md) | ||
- [func NewWriter(w io.Writer, level int) (*Writer, error)](NewWriter.md) | ||
- [func NewWriterDict(w io.Writer, level int, dict []byte) (*Writer, error)](NewWriterDict.md) | ||
- [func (w *Writer) Close() error](Writer.Close.md) | ||
- [func (w *Writer) Flush() error](Writer.Flush.md) | ||
- [func (w *Writer) Reset(dst io.Writer)](Writer.Reset.md) | ||
- [func (w *Writer) Write(data []byte) (n int, err error)](Writer.Write.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,9 @@ | ||
# func (e *ReadError) Error() string | ||
|
||
返回值:表示flate读取拷贝数据时的错误信息 | ||
|
||
功能说明:ReadError其实是一个struct,他实现了error接口,用于很方便的返回flate读取拷贝数据时的错误信息 | ||
|
||
示例: | ||
|
||
flate读取拷贝数据时的错误信息,这里不再举例。 |
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,9 @@ | ||
# func (e *WriteError) Error() string | ||
|
||
返回值:表示flate输出数据的错误信息 | ||
|
||
功能说明:WriteError是一个struct,他实现了error接口,用于很方便的返回flate输出数据的错误信息 | ||
|
||
示例: | ||
|
||
返回输出数据的错误信息,这里不再举例。 |
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,36 @@ | ||
# func (w *Writer) Close() error | ||
|
||
返回值:返回一个error,没有错误时该error为nil | ||
|
||
功能说明: | ||
|
||
刷新缓冲并关闭w | ||
|
||
示例: | ||
|
||
package main | ||
|
||
import ( | ||
"bytes" | ||
"compress/flate" | ||
"fmt" | ||
"log" | ||
) | ||
|
||
func main() { | ||
//一个缓冲区存储压缩的内容 | ||
buf := bytes.NewBuffer(nil) | ||
|
||
//创建一个flate.Writer | ||
flateWrite, err := flate.NewWriterDict(buf, flate.BestCompression, []byte("key")) | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
//刷新缓存并关闭flateWrite | ||
defer flateWrite.Close() | ||
//写入待压缩内容 | ||
flateWrite.Write([]byte("compress/flate\n")) | ||
flateWrite.Flush() | ||
fmt.Println(buf) | ||
|
||
} |
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,38 @@ | ||
# func (w *Writer) Flush() error | ||
|
||
返回值:返回一个error,没有错误时该error为nil | ||
|
||
功能说明: | ||
|
||
Flush将缓存中的压缩数据刷新到下层的io.writer中。它主要用在压缩的网络协议中,目的是确保远程读取器有足够的数据重建一个数据包。Flush是阻塞的,直到缓冲中的数据都被写入到下层io.writer中才返回。如果下层io.writer返回一个error,那么Flush也会返回该error。 | ||
|
||
在zlib库的术语中,Flush等同于Z_SYNC_FLUSH。 | ||
|
||
示例: | ||
|
||
package main | ||
|
||
import ( | ||
"bytes" | ||
"compress/flate" | ||
"fmt" | ||
"log" | ||
) | ||
|
||
func main() { | ||
//一个缓冲区存储压缩的内容 | ||
buf := bytes.NewBuffer(nil) | ||
|
||
//创建一个flate.Writer | ||
flateWrite, err := flate.NewWriterDict(buf, flate.BestCompression, []byte("key")) | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
//刷新缓存并关闭flateWrite | ||
defer flateWrite.Close() | ||
//写入待压缩内容 | ||
flateWrite.Write([]byte("compress/flate\n")) | ||
flateWrite.Flush() | ||
fmt.Println(buf) | ||
|
||
} |
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,42 @@ | ||
# func (w *Writer) Reset(dst io.Writer) | ||
|
||
参数列表: | ||
|
||
- dst 重置时将为作w的下层io.Writer | ||
|
||
功能说明: | ||
|
||
Reset会丢弃当前的w的状态,这相当于把dst、w的级别和字典作为参数,重新调用NewWriter或者NewWriterDict函数一样。 | ||
|
||
示例: | ||
|
||
package main | ||
|
||
import ( | ||
"bytes" | ||
"compress/flate" | ||
"fmt" | ||
"log" | ||
) | ||
|
||
func main() { | ||
//一个缓冲区存储压缩的内容 | ||
buf := bytes.NewBuffer(nil) | ||
|
||
//创建一个flate.Writer | ||
flateWrite, err := flate.NewWriterDict(buf, flate.BestCompression, []byte("key")) | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
defer flateWrite.Close() | ||
|
||
//reset | ||
buf1 := bytes.NewBuffer(nil) | ||
flateWrite.Reset(buf1) | ||
//写入待压缩内容 | ||
flateWrite.Write([]byte("compress/flate\n")) | ||
flateWrite.Flush() | ||
fmt.Println(buf) //什么都没有一个空行,因为我们reset重置了 | ||
fmt.Println(buf1) //这个会输出结果,因为下层io.Writer被替换为buf1了 | ||
} | ||
|
Oops, something went wrong.