better zip

This commit is contained in:
Pierre Dubouilh 2021-11-07 16:39:58 +01:00 committed by Pierre Dubouilh
parent 688be87923
commit 2f73fd9df8
2 changed files with 16 additions and 7 deletions

View file

@ -193,7 +193,10 @@ func walkZip(wz *zip.Writer, fp, baseInZip string) {
if !file.IsDir() {
data, err := ioutil.ReadFile(fp + file.Name())
check(err)
f, err := wz.Create(baseInZip + file.Name())
f, err := wz.CreateHeader(&zip.FileHeader{
Name: baseInZip + file.Name(),
Method: zip.Store, // dont compress
})
check(err)
_, err = f.Write(data)
check(err)

View file

@ -1,8 +1,8 @@
package main
import (
"archive/zip"
"bytes"
"crypto/sha256"
"fmt"
"io/ioutil"
"net/http"
@ -30,6 +30,13 @@ func getRaw(t *testing.T, url string) []byte {
return body
}
func getZip(t *testing.T, dest string) []*zip.File {
b := getRaw(t, dest)
archive, err := zip.NewReader(bytes.NewReader(b), int64(len(b)))
dieMaybe(t, err)
return archive.File
}
func get(t *testing.T, url string) string {
body := getRaw(t, url)
return trimSpaces(string(body))
@ -119,11 +126,10 @@ func doTestRegular(t *testing.T, url string, testExtra bool) {
}
// ~~~~~~~~~~~~~~~~~
fmt.Println("\r\n~~~~~~~~~~ test zip")
bodyRaw := getRaw(t, url+"zip?zipPath=%2F%E4%B8%AD%E6%96%87%2F&zipName=%E4%B8%AD%E6%96%87")
hashStr := fmt.Sprintf("%x", sha256.Sum256(bodyRaw))
if hashStr != "b02436a76b149e6c4458bbbe622ab7c5e789bb0d26b87f604cf0f989cfaf669f" {
t.Fatal("invalid zip checksum", hashStr)
fmt.Println("\r\n~~~~~~~~~~ test zipping of folder 中文")
files := getZip(t, url+"zip?zipPath=%2F%E4%B8%AD%E6%96%87%2F&zipName=%E4%B8%AD%E6%96%87")
if len(files) != 1 || files[0].Name != "檔案.html" {
t.Fatal("invalid zip generated")
}
// ~~~~~~~~~~~~~~~~~