1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- package zip
- import (
- "archive/zip"
- "io"
- "os"
- "path/filepath"
- )
- type StreamArchive struct {
- *zip.Writer
- }
- func NewStreamArachive(w io.Writer) *StreamArchive {
- return &StreamArchive{zip.NewWriter(w)}
- }
- func (s *StreamArchive) StreamFile(relPath string, fi os.FileInfo, data []byte) error {
- if fi.IsDir() {
- fh, err := zip.FileInfoHeader(fi)
- if err != nil {
- return err
- }
- fh.Name = relPath + "/"
- if _, err = s.Writer.CreateHeader(fh); err != nil {
- return err
- }
- } else {
- fh, err := zip.FileInfoHeader(fi)
- if err != nil {
- return err
- }
- fh.Name = filepath.Join(relPath, fi.Name())
- fh.Method = zip.Deflate
- fw, err := s.Writer.CreateHeader(fh)
- if err != nil {
- return err
- } else if _, err = fw.Write(data); err != nil {
- return err
- }
- }
- return nil
- }
- func (s *StreamArchive) StreamReader(relPath string, fi os.FileInfo, r io.Reader) (err error) {
- fh, err := zip.FileInfoHeader(fi)
- if err != nil {
- return err
- }
- fh.Name = filepath.Join(relPath, fi.Name())
- fw, err := s.Writer.CreateHeader(fh)
- if err != nil {
- return err
- }
- _, err = io.Copy(fw, r)
- return err
- }
|