2017-04-14 15:39:32 -03:00
|
|
|
// Package zip implements the Archive interface providing zip archiving
|
|
|
|
// and compression.
|
2017-01-06 13:23:49 -02:00
|
|
|
package zip
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/zip"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2017-01-15 08:48:27 -02:00
|
|
|
// Archive zip struct
|
2017-01-06 13:23:49 -02:00
|
|
|
type Archive struct {
|
|
|
|
z *zip.Writer
|
|
|
|
}
|
|
|
|
|
2017-01-15 08:48:27 -02:00
|
|
|
// Close all closeables
|
2017-01-06 13:23:49 -02:00
|
|
|
func (a Archive) Close() error {
|
|
|
|
return a.z.Close()
|
|
|
|
}
|
|
|
|
|
2017-01-15 08:48:27 -02:00
|
|
|
// New zip archive
|
2017-01-06 13:23:49 -02:00
|
|
|
func New(target *os.File) Archive {
|
|
|
|
return Archive{
|
|
|
|
z: zip.NewWriter(target),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-15 08:48:27 -02:00
|
|
|
// Add a file to the zip archive
|
2017-01-06 13:23:49 -02:00
|
|
|
func (a Archive) Add(name, path string) (err error) {
|
|
|
|
file, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-05-11 09:36:04 -03:00
|
|
|
stat, err := file.Stat()
|
|
|
|
if err != nil || stat.IsDir() {
|
|
|
|
return
|
|
|
|
}
|
2017-01-06 13:23:49 -02:00
|
|
|
defer func() { _ = file.Close() }()
|
|
|
|
f, err := a.z.Create(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = io.Copy(f, file)
|
|
|
|
return err
|
|
|
|
}
|