xfile

package
v1.0.10 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2024 License: MIT Imports: 13 Imported by: 1

README

xfile

import "github.com/fufuok/utils/xfile"

Index

Constants

const (
    DefaultFlushSizeLimit = 1 << 20
    DefaultFlushInterval  = 1 * time.Second
    MinFlushSizeLimit     = 4096
    MinFlushInterval      = 100 * time.Millisecond
)

Variables

var ErrFilename = errors.New("wrong file name")

func CopyDir

func CopyDir(srcDir, dstDir string) error

CopyDir 目录拷贝

func CopyFile

func CopyFile(srcFile, dstFile string) error

CopyFile 文件拷贝

func IsDir

func IsDir(s string) bool

IsDir 目录是否存在

func IsExist

func IsExist(s string) bool

IsExist 文件或目录是否存在

func IsFile

func IsFile(s string) bool

IsFile 文件是否存在

func ModTime

func ModTime(filename string) time.Time

ModTime 文件最后修改时间

func ReadFile

func ReadFile(filename string) (string, error)

ReadFile reads contents from a file

func ReadLines

func ReadLines(filename string) ([]string, error)

ReadLines reads contents from a file and splits them by new lines. A convenience wrapper to ReadLinesOffsetN(filename, 0, -1).

func ReadLinesOffsetN

func ReadLinesOffsetN(filename string, offset uint, n int) ([]string, error)

ReadLinesOffsetN reads contents from file and splits them by new line. The offset tells at which line number to start. The count determines the number of lines to read (starting from offset): n >= 0: at most n lines n < 0: whole file Ref: gopsutil

func ResetDir

func ResetDir(dirPath string) error

ResetDir 清除并重建空目录

func UnzipDir

func UnzipDir(zipFile, dstDir string) error

UnzipDir 解压 zip 到目录

func UnzipFile

func UnzipFile(zipFile *zip.File, dstFile string) error

UnzipFile 解压单个文件

func ZipDir

func ZipDir(srcDir, zipFilePath string) error

ZipDir 目录打包为 zip 文件

type DefaultFilename

type DefaultFilename struct{}

func (*DefaultFilename) MakeFilename
func (d *DefaultFilename) MakeFilename(name string) string

type FilenameMaker

type FilenameMaker interface {
    MakeFilename(name string) string
}

type Logger

type Logger interface {
    Errorf(format string, v ...interface{})
}

type Options

type Options struct {
    // 文件名生成器, 每秒检查
    FilenameMaker FilenameMaker

    // 日志处理器
    Logger Logger

    // 每次(实例启动时除外)滚动的文件是否删除后重建
    Rebuild bool

    // 刷新到磁盘的大小和时间间隔, 默认 1MiB, 1秒
    // 注意: 由于写文件有缓冲, 如果按秒切割文件, 数据将有可能写入上一秒的文件名中
    FlushSizeLimit int
    FlushInterval  time.Duration
}

type Roller

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

func NewRoller
func NewRoller(filename string, opt *Options) (*Roller, error)

func (*Roller) Close
func (r *Roller) Close()

func (*Roller) Write
func (r *Roller) Write(p []byte) (int, error)

func (*Roller) WriteString
func (r *Roller) WriteString(s string) (int, error)

type TimeBasedFilename

type TimeBasedFilename struct {
    // 文件目录
    FilePath string

    // 文件名模板: run-%s.log
    FilenameTpl string

    // 日期时间模板: 060102
    TimeTpl string

    // 时间标识相同则不生成新文件名: 221130
    TimeTag string
}

func (*TimeBasedFilename) MakeFilename
func (t *TimeBasedFilename) MakeFilename(name string) string

Generated by gomarkdoc

Documentation

Index

Constants

View Source
const (
	DefaultFlushSizeLimit = 1 << 20
	DefaultFlushInterval  = 1 * time.Second
	MinFlushSizeLimit     = 4096
	MinFlushInterval      = 100 * time.Millisecond
)

Variables

View Source
var ErrFilename = errors.New("wrong file name")

Functions

func CopyDir added in v1.0.5

func CopyDir(srcDir, dstDir string) error

CopyDir 目录拷贝

func CopyFile added in v1.0.5

func CopyFile(srcFile, dstFile string) error

CopyFile 文件拷贝

func IsDir

func IsDir(s string) bool

IsDir 目录是否存在

func IsExist

func IsExist(s string) bool

IsExist 文件或目录是否存在

func IsFile

func IsFile(s string) bool

IsFile 文件是否存在

func ModTime added in v1.0.0

func ModTime(filename string) time.Time

ModTime 文件最后修改时间

func ReadFile added in v0.10.3

func ReadFile(filename string) (string, error)

ReadFile reads contents from a file

func ReadLines added in v0.10.3

func ReadLines(filename string) ([]string, error)

ReadLines reads contents from a file and splits them by new lines. A convenience wrapper to ReadLinesOffsetN(filename, 0, -1).

func ReadLinesOffsetN added in v0.10.3

func ReadLinesOffsetN(filename string, offset uint, n int) ([]string, error)

ReadLinesOffsetN reads contents from file and splits them by new line. The offset tells at which line number to start. The count determines the number of lines to read (starting from offset): n >= 0: at most n lines n < 0: whole file Ref: gopsutil

func ResetDir added in v1.0.5

func ResetDir(dirPath string) error

ResetDir 清除并重建空目录

func UnzipDir added in v1.0.5

func UnzipDir(zipFile, dstDir string) error

UnzipDir 解压 zip 到目录

func UnzipFile added in v1.0.5

func UnzipFile(zipFile *zip.File, dstFile string) error

UnzipFile 解压单个文件

func ZipDir added in v1.0.5

func ZipDir(srcDir, zipFilePath string) error

ZipDir 目录打包为 zip 文件

Types

type DefaultFilename

type DefaultFilename struct{}

func (*DefaultFilename) MakeFilename

func (d *DefaultFilename) MakeFilename(name string) string

type FilenameMaker

type FilenameMaker interface {
	MakeFilename(name string) string
}

type Logger

type Logger interface {
	Errorf(format string, v ...interface{})
}

type Options

type Options struct {
	// 文件名生成器, 每秒检查
	FilenameMaker FilenameMaker

	// 日志处理器
	Logger Logger

	// 每次(实例启动时除外)滚动的文件是否删除后重建
	Rebuild bool

	// 刷新到磁盘的大小和时间间隔, 默认 1MiB, 1秒
	// 注意: 由于写文件有缓冲, 如果按秒切割文件, 数据将有可能写入上一秒的文件名中
	FlushSizeLimit int
	FlushInterval  time.Duration
}

type Roller

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

func NewRoller

func NewRoller(filename string, opt *Options) (*Roller, error)

func (*Roller) Close

func (r *Roller) Close()

func (*Roller) Write

func (r *Roller) Write(p []byte) (int, error)

func (*Roller) WriteString

func (r *Roller) WriteString(s string) (int, error)

type TimeBasedFilename

type TimeBasedFilename struct {
	// 文件目录
	FilePath string

	// 文件名模板: run-%s.log
	FilenameTpl string

	// 日期时间模板: 060102
	TimeTpl string

	// 时间标识相同则不生成新文件名: 221130
	TimeTag string
}

func (*TimeBasedFilename) MakeFilename

func (t *TimeBasedFilename) MakeFilename(name string) string

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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