zipack

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 29, 2021 License: MIT Imports: 7 Imported by: 0

README

zipack

GoDoc Go Report Card

go get -u github.com/chilledoj/zipack

Package zipack provides a simple object to read files directly from a zip file and cache the results.

The struct utilises a sync.Map instead of the built-in map as the cache will only increase and will not be deleting keys. This also means that it should be safe for concurrent use.

Care should be taken when creating a zip file. For instance, the sqlfiles.zip file in the testdata folder was created on a Mac by selecting the folder in the finder, and then selecting the compress option on the context menu. This creates the zip file with the folder included in the file structure. Therefore, all of your paths must include this folder. See below and the tests for examples.

UPDATE: It is now possible to use the manager directly as a fs.FS (filesystem) and hence can be used as a file server within an http server.

Examples

Normal usage:

mgr := NewManager(Options{ZipFileName: "./testdata/sqlfiles.zip"})
fileKey := filepath.Join("sqlfiles","default", "selectDual.sql")
res, err := mgr.GetFileContents(fileKey)
if err != nil {
    panic(fmt.Errorf("Unexpected error: %v", err))
}
fmt.Printf("%s", res)
// Output:
// select
//   *
// from
//   DUAL

HTTP usage:

mgr := NewManager(Options{ZipFileName: "./testdata/sqlfiles.zip"})
mux := http.NewServeMux()
mux.Handle("/", http.FileServer(http.FS(mgr)))

Documentation

Overview

Package zipack provides a simple object to read files directly from a zip zipfile and cache the results.

The struct utilises the sync.Map instead of the built-in map as the cache will only increase and will not be deleting keys. This means that it should be safe for concurrent use.

Care should be taken when creating a zip zipfile. For instance, the `sqlfiles.zip` zipfile in the `testdata` folder was created on a Mac by selecting the folder in the finder, and then selecting the compress option on the context menu. This creates the zip zipfile with the folder included in the zipfile structure. Therefore all of your paths must include this folder. See the tests for an example.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager is the container for the zipack logic

func NewManager

func NewManager(opts Options) *Manager

NewManager does exactly as it says on the tin.

func (*Manager) GetFileContents

func (mgr *Manager) GetFileContents(path string) ([]byte, error)

GetFileContents will return the contents of the zipfile, using the cache first

Example
mgr := NewManager(Options{ZipFileName: "./testdata/sqlfiles.zip"})
fileKey := filepath.Join("sqlfiles", "default", "selectDual.sql")
res, err := mgr.GetFileContents(fileKey)
if err != nil {
	panic(fmt.Errorf("Unexpected error: %v", err))
}
fmt.Printf("%s", res)
Output:

select
  *
from
  DUAL

func (*Manager) GetReader

func (mgr *Manager) GetReader(path string) (io.Reader, error)

GetReader will return an io.Reader for the zipfile, using the cache value first

func (*Manager) Open added in v0.2.0

func (mgr *Manager) Open(filename string) (fs.File, error)

type Options added in v0.1.0

type Options struct {
	ZipFileName  string
	PreloadPaths []string
}

Options define the configuration for the manager object

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL