funcmain(){// Open a zip file for reading// 打开压缩文件包
zr, err := zip.OpenReader(filepath.Join("D:/","Go WorkSpace/src/a_golearning_library_of_everything/go-packages/archive/zip/reader","archive.zip"))if err !=nil{
log.Fatalln(err)}// Close the archive when we're done// 压缩完成后关闭deferfunc(){if err := zr.Close(); err !=nil{
log.Fatalln(err)}}()// Iterate through the files in the archive// 循环逐个读取zip包内的文件for_, f :=range zr.File {// Open the current file to read it's contents// 打开包内的文件
rc, err := f.Open()if err !=nil{
log.Fatalln(err)}// Print the current file's name// 打印文件名//fmt.Printf("%s:\n", f.Name)// Copy the contents of the file to stdout// 把文件内容输出到打印台_, err = io.Copy(os.Stdout, rc)if err !=nil{
log.Fatalln(err)}
fw,_:=os.Create(path.Join("D:/","Go WorkSpace/src/a_golearning_library_of_everything/go-packages/archive/zip/reader/",f.Name))_,err = io.Copy(fw,rc)// Close the file handle// 关闭文件if err := rc.Close(); err !=nil{
log.Fatalln(err)}
fmt.Println("\n")}}
你传入的
writer
和reader
是啥?试试执行下Seek
?seek是什么意思
看了下
zip
包,没有实现io.Seek
接口,所以,我说的不行。因为流读到末尾了,你接着Copy
,自然啥也 Copy 不到,如果实现了 Seek 接口,可以重置到流的开始。多谢
不用zip 去实现Seek接口,直接rc.Seek(0,0)回到起始位置
这里 rc 根本没有 Seek 方法啊