forked from unidoc/unioffice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
90 lines (82 loc) · 2.38 KB
/
helpers.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright 2017 Baliance. All rights reserved.
//
// Use of this source code is governed by the terms of the Affero GNU General
// Public License version 3.0 as published by the Free Software Foundation and
// appearing in the file LICENSE included in the packaging of this file. A
// commercial license can be purchased by contacting sales@baliance.com.
package zippkg
import (
"archive/zip"
"encoding/xml"
"fmt"
"io"
"io/ioutil"
"os"
"sort"
"strings"
"baliance.com/gooxml/algo"
"baliance.com/gooxml/schema/soo/pkg/relationships"
)
// RelationsPathFor returns the relations path for a given filename.
func RelationsPathFor(path string) string {
sp := strings.Split(path, "/")
pathPortion := strings.Join(sp[0:len(sp)-1], "/")
filePortion := sp[len(sp)-1]
pathPortion += "/_rels/"
filePortion += ".rels"
return pathPortion + filePortion
}
// Decode unmarshals the content of a *zip.File as XML to a given destination.
func Decode(f *zip.File, dest interface{}) error {
rc, err := f.Open()
if err != nil {
return fmt.Errorf("error reading %s: %s", f.Name, err)
}
defer rc.Close()
dec := xml.NewDecoder(rc)
if err := dec.Decode(dest); err != nil {
return fmt.Errorf("error decoding %s: %s", f.Name, err)
}
// this ensures that relationship ID is increasing, which we apparently rely
// on....
if ds, ok := dest.(*relationships.Relationships); ok {
sort.Slice(ds.Relationship, func(i, j int) bool {
lhs := ds.Relationship[i]
rhs := ds.Relationship[j]
return algo.NaturalLess(lhs.IdAttr, rhs.IdAttr)
})
}
return nil
}
// AddFileFromDisk reads a file from disk and adds it at a given path to a zip file.
func AddFileFromDisk(z *zip.Writer, zipPath, diskPath string) error {
w, err := z.Create(zipPath)
if err != nil {
return fmt.Errorf("error creating %s: %s", zipPath, err)
}
f, err := os.Open(diskPath)
if err != nil {
return fmt.Errorf("error opening %s: %s", diskPath, err)
}
_, err = io.Copy(w, f)
return err
}
// ExtractToDiskTmp extracts a zip file to a temporary file in a given path,
// returning the name of the file.
func ExtractToDiskTmp(f *zip.File, path string) (string, error) {
tmpFile, err := ioutil.TempFile(path, "zz")
if err != nil {
return "", err
}
defer tmpFile.Close()
rc, err := f.Open()
if err != nil {
return "", err
}
defer rc.Close()
_, err = io.Copy(tmpFile, rc)
if err != nil {
return "", err
}
return tmpFile.Name(), nil
}