|
@ -20,38 +20,43 @@ func ZipFiles(filename string, files []string, oldform, newform string) error { |
|
|
|
|
|
|
|
|
// 把files添加到zip中
|
|
|
// 把files添加到zip中
|
|
|
for _, file := range files { |
|
|
for _, file := range files { |
|
|
|
|
|
|
|
|
zipfile, err := os.Open(file) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return err |
|
|
|
|
|
} |
|
|
|
|
|
defer zipfile.Close() |
|
|
|
|
|
|
|
|
|
|
|
// 获取file的基础信息
|
|
|
|
|
|
info, err := zipfile.Stat() |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return err |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
header, err := zip.FileInfoHeader(info) |
|
|
|
|
|
|
|
|
err = func(file string) error { |
|
|
|
|
|
zipFile, err := os.Open(file) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return err |
|
|
|
|
|
} |
|
|
|
|
|
defer zipFile.Close() |
|
|
|
|
|
|
|
|
|
|
|
// 获取file的基础信息
|
|
|
|
|
|
info, err := zipFile.Stat() |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return err |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
header, err := zip.FileInfoHeader(info) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return err |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 使用上面的FileInforHeader() 就可以把文件保存的路径替换成我们自己想要的了,如下面
|
|
|
|
|
|
header.Name = strings.Replace(file, oldform, newform, -1) |
|
|
|
|
|
|
|
|
|
|
|
// 优化压缩
|
|
|
|
|
|
// 更多参考see http://golang.org/pkg/archive/zip/#pkg-constants
|
|
|
|
|
|
header.Method = zip.Deflate |
|
|
|
|
|
|
|
|
|
|
|
writer, err := zipWriter.CreateHeader(header) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return err |
|
|
|
|
|
} |
|
|
|
|
|
if _, err = io.Copy(writer, zipFile); err != nil { |
|
|
|
|
|
return err |
|
|
|
|
|
} |
|
|
|
|
|
return nil |
|
|
|
|
|
}(file) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return err |
|
|
return err |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 使用上面的FileInforHeader() 就可以把文件保存的路径替换成我们自己想要的了,如下面
|
|
|
|
|
|
header.Name = strings.Replace(file, oldform, newform, -1) |
|
|
|
|
|
|
|
|
|
|
|
// 优化压缩
|
|
|
|
|
|
// 更多参考see http://golang.org/pkg/archive/zip/#pkg-constants
|
|
|
|
|
|
header.Method = zip.Deflate |
|
|
|
|
|
|
|
|
|
|
|
writer, err := zipWriter.CreateHeader(header) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return err |
|
|
|
|
|
} |
|
|
|
|
|
if _, err = io.Copy(writer, zipfile); err != nil { |
|
|
|
|
|
return err |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
return nil |
|
|
return nil |
|
|
} |
|
|
} |