gzip_embed

package
v1.4.4-alpha1202-loadi... Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2025 License: AGPL-3.0 Imports: 15 Imported by: 0

README

使用说明

0. 简介

此工具用于解决embed明文打包的问题,可以将静态资源文件压缩后打包到二进制文件中,运行时自动解压文件。

1. 安装

需要安装项目中的gzip-embed工具,可以使用以下命令安装:

go build -ldflags="-s -w" -o ~/.local/bin common/utils/gzip_embed/cmd/gzip-embed.go && chmod +x ~/.local/bin/gzip-embed

2. 使用

gzip-embed工具会自动读取工作目录下的static目录,将其中的文件进行压缩后生成static.tar.gz和embed.go文件。embed.go文件中定义了变量FS,可以读取并自动解压文件。

2.1 编写init.go

避免每次生成压缩文件时都要手动执行gzip-embed工具,可以在static的同级目录中编写init.go文件,添加generate指令,内容如下:

package xxx

//go:generate gzip-embed -cache

注意-cache参数表示生成的embed.go文件中会缓存文件内容,如果文件较大,可以不加此参数,每次读取文件时都会重新解压。

2.2 生成压缩文件

在static目录的同级目录下执行以下命令:

go generate .

也可以在项目根目录下执行以下命令:

go generate ./...

执行后会自动在init.go同级生成embed.go和static.tar.gz文件。

2.3 读取文件

在代码中使用embed.go中的FS变量读取文件,示例如下:

package main

import (
    "fmt"
    "log"
    "xxx"
)

func main() {
    data, err := xxx.FS.ReadFile("test.txt")
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(string(data))
}

具体案例可以参考测试文件:common/utils/gzip_embed/test/fs_test.go

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type PreprocessingEmbed

type PreprocessingEmbed struct {
	*embed.FS
	EnableCache bool
	// contains filtered or unexported fields
}

PreprocessingEmbed is a simple tools to read file from embed.FS and gzip compress file only support ReadFile method, not support Open method

func NewEmptyPreprocessingEmbed

func NewEmptyPreprocessingEmbed() *PreprocessingEmbed

func NewPreprocessingEmbed

func NewPreprocessingEmbed(fs *embed.FS, fileName string, cache bool) (*PreprocessingEmbed, error)

NewPreprocessingEmbed create a CompressFS instance fs is embed.FS instance, compressDirs is a map, key is virtual dir, value is compress file name

func (*PreprocessingEmbed) Base

func (c *PreprocessingEmbed) Base(name string) string

func (*PreprocessingEmbed) Delete

func (c *PreprocessingEmbed) Delete(name string) error

func (*PreprocessingEmbed) Exists

func (c *PreprocessingEmbed) Exists(name string) (bool, error)

func (*PreprocessingEmbed) Ext

func (c *PreprocessingEmbed) Ext(name string) string

func (*PreprocessingEmbed) ExtraInfo

func (c *PreprocessingEmbed) ExtraInfo(name string) map[string]any

ExtraInfo returns extra information about the fs

func (*PreprocessingEmbed) GetHash

func (c *PreprocessingEmbed) GetHash() (string, error)

GetHash 计算所有文件内容的哈希值,用于检测文件是否有变动 返回一个 SHA256 哈希字符串

func (*PreprocessingEmbed) GetSeparators

func (c *PreprocessingEmbed) GetSeparators() rune

PathFileSystem methods

func (*PreprocessingEmbed) Getwd

func (c *PreprocessingEmbed) Getwd() (string, error)

func (*PreprocessingEmbed) InvalidateHash

func (c *PreprocessingEmbed) InvalidateHash()

InvalidateHash 清除缓存的哈希值,在文件可能发生变化后调用

func (*PreprocessingEmbed) IsAbs

func (c *PreprocessingEmbed) IsAbs(name string) bool

func (*PreprocessingEmbed) Join

func (c *PreprocessingEmbed) Join(elem ...string) string

func (*PreprocessingEmbed) MkdirAll

func (c *PreprocessingEmbed) MkdirAll(path string, perm os.FileMode) error

func (*PreprocessingEmbed) Open

func (c *PreprocessingEmbed) Open(name string) (fs.File, error)

Open opens the named file

func (*PreprocessingEmbed) OpenFile

func (c *PreprocessingEmbed) OpenFile(name string, flag int, perm os.FileMode) (fs.File, error)

OpenFile opens the named file with specified flag (readonly for tar.gz)

func (*PreprocessingEmbed) PathSplit

func (c *PreprocessingEmbed) PathSplit(name string) (string, string)

func (*PreprocessingEmbed) ReadDir

func (c *PreprocessingEmbed) ReadDir(dirname string) ([]fs.DirEntry, error)

ReadDir reads the directory and returns directory entries

func (*PreprocessingEmbed) ReadFile

func (c *PreprocessingEmbed) ReadFile(name string) ([]byte, error)

ReadFile override embed.FS.ReadFile, if file is compress file, return decompress data

func (*PreprocessingEmbed) Rel

func (c *PreprocessingEmbed) Rel(basepath, targpath string) (string, error)

func (*PreprocessingEmbed) Rename

func (c *PreprocessingEmbed) Rename(oldname, newname string) error

WriteFileSystem methods (read-only, return errors)

func (*PreprocessingEmbed) Stat

func (c *PreprocessingEmbed) Stat(name string) (fs.FileInfo, error)

Stat returns file info for the named file or directory

func (*PreprocessingEmbed) WriteFile

func (c *PreprocessingEmbed) WriteFile(name string, data []byte, perm os.FileMode) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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