Documentation
¶
Overview ¶
Package httpzip provides a HTTP handler to serve multiple files in a ZIP stream.
Index ¶
Examples ¶
Constants ¶
const ( Zip64ExtraID = 0x0001 // Zip64 extended information. NtfsExtraID = 0x000a // NTFS. UnixExtraID = 0x000d // UNIX. ExtTimeExtraID = 0x5455 // Extended timestamp. InfoZipUnixExtraID = 0x5855 // Info-ZIP Unix extension. )
Extra header IDs. See http://mdfs.net/Docs/Comp/Archiving/Zip/ExtraField.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Entry ¶ added in v0.1.1
type Entry struct {
zip.FileHeader
// contains filtered or unexported fields
}
Entry represents a file or directory in ZIP archive.
type FileSource ¶
type FileSource struct {
Path string
Modified time.Time
Size int64
CRC32 uint32 // CRC32 checksum of the file content, optional.
Data func(w io.Writer) error
}
FileSource describes archive entry.
func (*FileSource) FillCRC32 ¶ added in v0.1.2
func (fs *FileSource) FillCRC32() error
FillCRC32 counts CRC32 if it is empty.
type Handler ¶
type Handler struct {
OnError func(err error)
Streamable bool // Use inlined raw file headers instead of final directory to allow streaming decoding.
IgnoreCRC32 bool // Allow streamable ZIP with empty CRC32.
// contains filtered or unexported fields
}
Handler serves multiple files in uncompressed ZIP.
func NewHandler ¶
NewHandler creates an instance of Handler.
Example ¶
package main
import (
"fmt"
"io"
"log"
"net/http/httptest"
"time"
"github.com/vearutop/httpzip"
)
func main() {
rw := httptest.NewRecorder()
h := httpzip.NewHandler("archive")
h.Streamable = true
h.OnError = func(err error) {
log.Println(err)
}
c := "hello world"
for i := 0; i < 10; i++ {
if err := h.AddFile(httpzip.FileSource{
Path: fmt.Sprintf("file_%d.txt", i),
Modified: time.Now(),
Size: int64(len(c)),
Data: func(w io.Writer) error {
_, err := w.Write([]byte(c)) // Mimicking actual file source.
return err
},
}); err != nil {
log.Println(err)
}
}
h.ServeHTTP(rw, nil)
fmt.Println("Status:", rw.Code)
fmt.Println("Content-Length:", rw.Header().Get("Content-Length"))
}
Output: Status: 200 Content-Length: 1092
func (*Handler) AddFile ¶
func (h *Handler) AddFile(fs FileSource) error
AddFile add a file to the archive.
type StreamReader ¶ added in v0.1.1
type StreamReader struct {
// contains filtered or unexported fields
}
StreamReader can read ZIP contents from a io.Reader.
func NewStreamReader ¶ added in v0.1.1
func NewStreamReader(r io.Reader) *StreamReader
NewStreamReader returns streaming ZIP reader.
Example ¶
package main
import (
"io"
"log"
"net/http"
"github.com/vearutop/httpzip"
)
func main() {
resp, err := http.Get("https://www.example.com/archive.zip")
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
zr := httpzip.NewStreamReader(resp.Body)
for {
e, err := zr.Next()
if err == io.EOF {
break
}
if err != nil {
log.Fatalf("failed to find next file in zip: %s", err)
}
log.Println("file path:", e.Name)
if !e.IsDir() {
rc, err := e.Open()
if err != nil {
log.Fatalf("unable to open zip file entry: %s", err)
}
w, err := io.Copy(io.Discard, rc)
if err != nil {
log.Fatalf("unable to stream zip file: %s (%d)", err, w)
}
log.Println("file length:", w)
if err := rc.Close(); err != nil {
log.Fatalf("failed to close zip file entry: %s", err)
}
}
}
}
Output:
func (*StreamReader) Next ¶ added in v0.1.1
func (z *StreamReader) Next() (*Entry, error)
Next returns the next Entry.