Skip to content

Instantly share code, notes, and snippets.

@scorphus
Created August 7, 2014 17:56
Show Gist options
  • Save scorphus/2cc2f181422d9f934f87 to your computer and use it in GitHub Desktop.
Save scorphus/2cc2f181422d9f934f87 to your computer and use it in GitHub Desktop.
multipartzip.go
// Copyright 2014 gandalf authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package multipartzip
import (
"archive/zip"
"bytes"
"io"
"log"
"mime/multipart"
"path/filepath"
)
func CreateZipBuffer(files []struct{ Name, Body string }) (*bytes.Buffer, error) {
buf := new(bytes.Buffer)
w := zip.NewWriter(buf)
for _, file := range files {
f, err := w.Create(file.Name)
if err != nil {
return nil, err
}
_, err = f.Write([]byte(file.Body))
if err != nil {
return nil, err
}
}
err := w.Close()
if err != nil {
return nil, err
}
return buf, nil
}
func StreamWriteBuffer(params map[string]string, paramName, path, boundary string, writer *io.PipeWriter, buf *bytes.Buffer) {
defer writer.Close()
mpwriter := multipart.NewWriter(writer)
mpwriter.SetBoundary(boundary)
part, err := mpwriter.CreateFormFile(paramName, filepath.Base(path))
if err != nil {
log.Fatal(err)
return
}
_, err = io.Copy(part, buf)
if err != nil {
log.Fatal(err)
return
}
for key, val := range params {
_ = mpwriter.WriteField(key, val)
}
err = mpwriter.Close()
if err != nil {
log.Fatal(err)
return
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment