Skip to content

Commit

Permalink
This closes qax-os#1889, refs qax-os#1732 and qax-os#1735
Browse files Browse the repository at this point in the history
Saving workbook with reverse sorted internal part path to keep same hash of identical files
  • Loading branch information
xuri committed Apr 30, 2024
1 parent d570802 commit 8deaf25
Showing 1 changed file with 2 additions and 55 deletions.
57 changes: 2 additions & 55 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,7 @@ func (f *File) writeToZip(zw *zip.Writer) error {
files = append(files, path.(string))
return true
})

sortExcelFiles(files)

sort.Sort(sort.Reverse(sort.StringSlice(files)))
for _, path := range files {
var fi io.Writer
if fi, err = zw.Create(path); err != nil {
Expand All @@ -230,9 +228,7 @@ func (f *File) writeToZip(zw *zip.Writer) error {
tempFiles = append(tempFiles, path.(string))
return true
})

sortExcelFiles(tempFiles)

sort.Sort(sort.Reverse(sort.StringSlice(tempFiles)))
for _, path := range tempFiles {
var fi io.Writer
if fi, err = zw.Create(path); err != nil {
Expand All @@ -242,52 +238,3 @@ func (f *File) writeToZip(zw *zip.Writer) error {
}
return err
}

//
func sortExcelFiles(files []string) {
dirMap := make(map[string][]string)

for _, f := range files {
s := strings.SplitN(f, "/", 2)
if len(s) > 1 {
dirMap[s[0]] = append(dirMap[s[0]], s[1])
} else {
dirMap[""] = append(dirMap[""], s[0])
}
}

for k, fl := range dirMap {
sort.Strings(fl)
dirMap[k] = fl
}

out := files[0:0]

out = appendExcelFile(out, "xl", dirMap["xl"])

delete(dirMap, "xl")

var dirs []string
for k := range dirMap {
dirs = append(dirs, k)
}

sort.Strings(dirs)

for _, d := range dirs {
out = appendExcelFile(out, d, dirMap[d])
}
}

//
func appendExcelFile(inFiles []string, d string, files []string) []string {
if len(d) > 0 {
d += "/"
}

for _, f := range files {
inFiles = append(inFiles, d+f)
}

return inFiles
}

0 comments on commit 8deaf25

Please sign in to comment.