This repository has been archived on 2025-06-10. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
server/fileexporter/archive.go

42 lines
789 B
Go

package main
import (
"archive/zip"
"errors"
"io"
"os"
"path"
)
type archiveFileCreator interface {
Create(name string) (io.Writer, error)
}
func init() {
OutputFormats["archive"] = func(args ...string) (func(string) (io.WriteCloser, error), error) {
if len(args) != 1 {
return nil, errors.New("archive has 1 required argument: [destination-file]")
}
fd, err := os.Create(args[0])
if err != nil {
return nil, err
}
var w archiveFileCreator
if path.Ext(args[0]) == ".zip" {
w = zip.NewWriter(fd)
} else {
return nil, errors.New("destination file has to have .zip extension")
}
return func(dest string) (io.WriteCloser, error) {
fw, err := w.Create(dest)
if err != nil {
return nil, err
}
return NopCloser(fw), nil
}, nil
}
}