Commit d47887c8 authored by Lukas Böhm's avatar Lukas Böhm 🎱
Browse files

fix zipping

parent 1ff4d5b6
...@@ -2,7 +2,6 @@ package main ...@@ -2,7 +2,6 @@ package main
import ( import (
"archive/zip" "archive/zip"
"bytes"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
...@@ -301,23 +300,34 @@ func DownloadZip(w http.ResponseWriter, r *http.Request) *HTTPError { ...@@ -301,23 +300,34 @@ func DownloadZip(w http.ResponseWriter, r *http.Request) *HTTPError {
return er return er
} }
//comp, _ := zip2.Compressor(w)
zipWriter := zip.NewWriter(w) zipWriter := zip.NewWriter(w)
for _, file := range share.Attachments { for _, file := range share.Attachments {
filePath := filepath.Join(config.mediaDir, "data", file.ShareID.String(), file.ID.String()) filePath := filepath.Join(config.mediaDir, "data", file.ShareID.String(), file.ID.String())
fw, err := zipWriter.Create(filePath)
fileToZip, err := os.Open(filePath)
if err != nil {
return &HTTPError{ err, "error opening file", 500}
}
defer fileToZip.Close()
info, err := fileToZip.Stat()
if err != nil { if err != nil {
return &HTTPError{ err, "error when zipping", 500} return &HTTPError{ err, "error getting file info", 500 }
} }
fileContent, err := ioutil.ReadFile(filePath) header, err := zip.FileInfoHeader(info)
if err != nil { if err != nil {
return &HTTPError{ err, "error when reading file to zip", 500 } return &HTTPError{ err, "error creating file header", 500 }
} }
_, err = io.Copy(fw, bytes.NewReader(fileContent))
header.Name = file.Filename
header.Method = zip.Deflate
writer, err := zipWriter.CreateHeader(header)
if err != nil { if err != nil {
return &HTTPError{ err, "error when writing file to zip", 500 } return &HTTPError{ err, "error creating header", 500 }
} }
_, err = io.Copy(writer, fileToZip)
} }
err = zipWriter.Close() err = zipWriter.Close()
if err != nil { if err != nil {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment