2
2
mirror of https://github.com/octoleo/restic.git synced 2024-12-23 19:38:57 +00:00
restic/internal/dump/zip.go

67 lines
1.4 KiB
Go
Raw Normal View History

2020-11-10 00:44:03 +00:00
package dump
import (
"archive/zip"
"context"
"io"
"path/filepath"
2021-09-24 13:38:23 +00:00
"github.com/restic/restic/internal/bloblru"
2020-11-10 00:44:03 +00:00
"github.com/restic/restic/internal/errors"
"github.com/restic/restic/internal/restic"
)
type zipDumper struct {
2021-09-24 13:38:23 +00:00
cache *bloblru.Cache
w *zip.Writer
2020-11-10 00:44:03 +00:00
}
// Statically ensure that zipDumper implements dumper.
2021-09-24 13:38:23 +00:00
var _ dumper = &zipDumper{}
2020-11-10 00:44:03 +00:00
// WriteZip will write the contents of the given tree, encoded as a zip to the given destination.
func WriteZip(ctx context.Context, repo restic.Repository, tree *restic.Tree, rootPath string, dst io.Writer) error {
2021-09-24 13:38:23 +00:00
dmp := &zipDumper{
cache: NewCache(),
w: zip.NewWriter(dst),
}
return writeDump(ctx, repo, tree, rootPath, dmp)
2020-12-18 23:42:46 +00:00
}
2020-11-10 00:44:03 +00:00
2021-09-24 13:38:23 +00:00
func (dmp *zipDumper) Close() error {
2020-11-10 00:44:03 +00:00
return dmp.w.Close()
}
2021-09-24 13:38:23 +00:00
func (dmp *zipDumper) dumpNode(ctx context.Context, node *restic.Node, repo restic.Repository) 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 := dmp.w.CreateHeader(header)
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
}
2021-09-24 13:38:23 +00:00
return WriteNodeData(ctx, w, repo, node, dmp.cache)
2020-11-10 00:44:03 +00:00
}