Skip to content

ZIP

The zip driver exposes a ZIP archive on disk as a FileSystem. You can read an existing archive, build a new one, or open one for in-place editing.

  • Bundling generated assets into a downloadable export.
  • Reading uploaded archives without extracting them to disk.
  • Treating a content pack as a virtual filesystem at runtime.
Terminal window
go get github.com/gobeaver/filekit/driver/zip

There are three constructors corresponding to three modes:

import (
"context"
"strings"
"github.com/gobeaver/filekit/driver/zip"
)
ctx := context.Background()
// Read-only — opens an existing archive
ro, err := zip.Open("/path/to/archive.zip")
if err != nil { panic(err) }
defer ro.Close()
files, _ := ro.ListContents(ctx, "/", true)
_ = files
// Write-only — creates a fresh archive
w, err := zip.Create("/path/to/new.zip")
if err != nil { panic(err) }
_, _ = w.Write(ctx, "readme.txt", strings.NewReader("hello"))
_ = w.CreateDir(ctx, "images")
_ = w.Close() // finalises the central directory
// Read-write — load existing then mutate
rw, err := zip.OpenOrCreate("/path/to/archive.zip")
if err != nil { panic(err) }
_, _ = rw.Write(ctx, "new.txt", strings.NewReader("..."))
_ = rw.Delete(ctx, "old.txt")
_ = rw.Close() // rewrites the file with the changes

Close() is mandatory in write and read-write modes — that’s when the ZIP central directory is actually written to disk. Failing to call Close() leaves a truncated archive.

const (
ModeRead Mode = iota // archive opened for reading
ModeWrite // new archive being constructed
ModeReadWrite // existing archive loaded into memory
)

OpenOrCreate reads the entire archive into memory on open so it can later rewrite it. For very large archives this is expensive — prefer Open (read) or Create (write) when you don’t need both.

InterfaceImplementedNotes
FileSystemyesmode determines which methods succeed
CanCopyyesin-memory entry duplication
CanMoveyescopy + delete
CanChecksumyeshashes the entry’s stored bytes
CanWatchyes (sort of)returns a NeverChangeToken — archives are static
CanSignURLno
CanReadRangeno
ChunkedUploaderno
  • Writing to a ModeRead adapter or reading from a ModeWrite adapter before Close() returns an error.
  • The watcher’s ChangeToken is NeverChangeTokenHasChanged() will always return false. This is deliberate: a static archive cannot change.
  • Stat() returns the ZIP entry’s Modified time as ModTime; CreatedAt is nil.