66 lines
1.1 KiB
Go
66 lines
1.1 KiB
Go
package main
|
|
|
|
import (
|
|
"archive/zip"
|
|
"bytes"
|
|
"io"
|
|
"os"
|
|
)
|
|
|
|
func fillODTForm(inputPath, outputPath string, fields map[string]string) error {
|
|
reader, err := zip.OpenReader(inputPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer reader.Close()
|
|
|
|
writer, err := os.Create(outputPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer writer.Close()
|
|
|
|
zipWriter := zip.NewWriter(writer)
|
|
defer zipWriter.Close()
|
|
|
|
for _, file := range reader.File {
|
|
if file.Name == "content.xml" {
|
|
rc, err := file.Open()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer rc.Close()
|
|
content, err := io.ReadAll(rc)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for field, value := range fields {
|
|
content = bytes.Replace(content, []byte("%"+field+"%"), []byte(value), -1)
|
|
}
|
|
w, err := zipWriter.Create(file.Name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, err = w.Write(content)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
} else {
|
|
w, err := zipWriter.Create(file.Name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
rc, err := file.Open()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, err = io.Copy(w, rc)
|
|
rc.Close()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|