/go-zim

A Golang library to read and serve ZIM archives.

Primary LanguageGoMIT LicenseMIT

go-zim

Go Reference

A Golang library to read and serve ZIM archives.

Inspired by github.com/tim-st/go-zim.

Usage

Reading a ZIM file

package main

import (
	"flag"
	"net/http"

	"github.com/Bornholm/go-zim"
)

func main() {
	reader, err := zim.Open("my-archive.zim")
	if err != nil {
		panic(err)
	}

	defer func() {
		if err := reader.Close(); err != nil {
			panic(err)
		}
	}()

	fmt.Println("Entries count:", reader.EntryCount())

	mainPage, err := reader.MainPage()
	if err != nil {
		panic(err)
	}

	fmt.Println("Main Page Title:", mainPage.Title())
	fmt.Println("Main Page Full URL:", mainPage.FullURL())
}

Serving a ZIM file with a HTTP server

package main

import (
	"flag"
	"net/http"

	"github.com/Bornholm/go-zim"
	zimFS "github.com/Bornholm/go-zim/fs"
)

func main() {
	reader, err := zim.Open(zimPath)
	if err != nil {
		panic(err)
	}

	defer func() {
		if err := reader.Close(); err != nil {
			panic(err)
		}
	}()

	fs := zimFS.New(reader)
	fileServer := http.FileServer(http.FS(fs))

	if err := http.ListenAndServe(":8080", fileServer); err != nil {
		panic(err)
	}
}

See examples/zim-server for an runnable example.

License

MIT