/
zip.go
54 lines (46 loc) · 1.47 KB
/
zip.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Package ziputil provides some utility functions for zip archive handling.
package ziputil // import "go.yhsif.com/url2epub/ziputil"
import (
"archive/zip"
"fmt"
"io"
"strings"
)
// WriteFile writes a single file inside a zip archive.
func WriteFile(z *zip.Writer, filename string, src io.WriterTo) error {
header := &zip.FileHeader{
Name: filename,
Method: zip.Deflate,
}
return write(z, header, src)
}
// StoreFile is similar to WriteFile except it uses Store instead of Deflate.
func StoreFile(z *zip.Writer, filename string, src io.WriterTo) error {
header := &zip.FileHeader{
Name: filename,
Method: zip.Store,
}
return write(z, header, src)
}
func write(z *zip.Writer, header *zip.FileHeader, src io.WriterTo) error {
writer, err := z.CreateHeader(header)
if err != nil {
return fmt.Errorf("ziputil.WriteFile: unable to create %q: %w", header.Name, err)
}
if _, err := src.WriteTo(writer); err != nil {
return fmt.Errorf("ziputil.WriteFile: unable to write %q: %w", header.Name, err)
}
return nil
}
// StringWriterTo wraps string into io.WriterTo.
type StringWriterTo string
// WriteTo implements io.WriterTo.
func (s StringWriterTo) WriteTo(w io.Writer) (int64, error) {
return io.Copy(w, strings.NewReader(string(s)))
}
// WriterToWrapper helps wrapping lambdas into io.WriterTo.
type WriterToWrapper func(w io.Writer) (int64, error)
// WriteTo implements io.WriterTo.
func (w WriterToWrapper) WriteTo(writer io.Writer) (int64, error) {
return w(writer)
}