2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-12 08:16:37 +00:00
restic/internal/dump/zip.go

62 lines
1.1 KiB
Go
Raw Normal View History

2020-11-10 00:44:03 +00:00
package dump
import (
"archive/zip"
"context"
"path/filepath"
"github.com/restic/restic/internal/errors"
"github.com/restic/restic/internal/restic"
)
func (d *Dumper) dumpZip(ctx context.Context, ch <-chan *restic.Node) (err error) {
w := zip.NewWriter(d.w)
2020-11-10 00:44:03 +00:00
defer func() {
if err == nil {
err = w.Close()
err = errors.Wrap(err, "Close")
}
}()
2020-11-10 00:44:03 +00:00
for node := range ch {
if err := d.dumpNodeZip(ctx, node, w); err != nil {
return err
}
2021-09-24 13:38:23 +00:00
}
return nil
2020-11-10 00:44:03 +00:00
}
func (d *Dumper) dumpNodeZip(ctx context.Context, node *restic.Node, zw *zip.Writer) error {
2020-11-10 00:44:03 +00:00
relPath, err := filepath.Rel("/", node.Path)
if err != nil {
return err
}
header := &zip.FileHeader{
Name: filepath.ToSlash(relPath),
UncompressedSize64: node.Size,
Modified: node.ModTime,
}
header.SetMode(node.Mode)
if IsDir(node) {
header.Name += "/"
}
w, err := zw.CreateHeader(header)
2020-11-10 00:44:03 +00:00
if err != nil {
2020-12-18 23:04:17 +00:00
return errors.Wrap(err, "ZipHeader")
2020-11-10 00:44:03 +00:00
}
if IsLink(node) {
if _, err = w.Write([]byte(node.LinkTarget)); err != nil {
return errors.Wrap(err, "Write")
}
return nil
}
return d.writeNode(ctx, w, node)
2020-11-10 00:44:03 +00:00
}