Documentation
¶
Overview ¶
Package zipfs provides an implementation of an http.FileSystem backed by the contents of a zip archive.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewEmbeddedZipFileSystem ¶
func NewEmbeddedZipFileSystem(setters ...Option) (http.FileSystem, error)
NewEmbeddedZipFileSystem creates an http.FileSystem backed by the contents of a zip archive. It reads the zip archive content from the currently-running binary file itself. It also accepts an optional list of options.
Example ¶
package main
import (
"log"
"net/http"
"gerace.dev/zipfs"
)
func main() {
fs, err := zipfs.NewEmbeddedZipFileSystem(zipfs.ServeIndexForMissing())
if err != nil {
log.Fatalf("Error setting up zipfs: %v", err)
}
log.Fatal(http.ListenAndServe("127.0.0.1:8080", http.FileServer(fs)))
}
func NewZipFileSystem ¶
NewZipFileSystem creates an http.FileSystem backed by the contents of a zip archive (using from the provided *zip.Reader). It also accepts an optional list of options.
Example ¶
package main
import (
"archive/zip"
"log"
"net/http"
"os"
"gerace.dev/zipfs"
)
func main() {
f, err := os.Open("webassets.zip")
if err != nil {
log.Fatalf("Error opening webassets.zip: %v", err)
}
fi, err := f.Stat()
if err != nil {
log.Fatalf("Error getting file information about webassets.zip: %v", err)
}
zr, err := zip.NewReader(f, fi.Size())
if err != nil {
log.Fatalf("Error reading zip data from webassets.zip: %v", err)
}
fs, err := zipfs.NewZipFileSystem(zr)
if err != nil {
log.Fatalf("Error creating zip filesystem: %v", err)
}
log.Fatal(http.ListenAndServe("127.0.0.1:8080", http.FileServer(fs)))
}
Types ¶
type Option ¶
type Option func(*zipFileSystem)
Option represents a single option that can be set when instantiating a new filesystem.
func ServeIndexForMissing ¶
func ServeIndexForMissing() Option
ServeIndexForMissing configures the filesystem to always serve the content of index.html (at the root of the zip archive) when a nonexistent path is requested. This is useful for single-page applications.
Example ¶
package main
import (
"log"
"net/http"
"gerace.dev/zipfs"
)
func main() {
fs, err := zipfs.NewEmbeddedZipFileSystem(zipfs.ServeIndexForMissing())
if err != nil {
log.Fatalf("Error setting up zipfs: %v", err)
}
log.Fatal(http.ListenAndServe("127.0.0.1:8080", http.FileServer(fs)))
}