fileutil

package
v1.4.3 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2024 License: MIT Imports: 21 Imported by: 18

Documentation

Overview

Package fileutil implements some basic functions for file operations

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ChunkRead added in v1.4.3

func ChunkRead(f *os.File, offset int64, size int, bufPool *sync.Pool) []string

ChunkRead 从文件的指定偏移读取块并返回块内所有行

func ClearFile added in v1.0.6

func ClearFile(path string) error

ClearFile write empty string to path file

func CopyFile

func CopyFile(srcFilePath string, dstFilePath string) error

CopyFile copy src file to dest file

func CreateDir added in v1.3.0

func CreateDir(absPath string) error

CreateDir create directory in absolute path. param `absPath` like /a/, /a/b/

func CreateFile

func CreateFile(path string) bool

CreateFile create a file in path

func CurrentPath added in v1.3.8

func CurrentPath() string

CurrentPath return current absolute path.

func FileMode added in v1.1.8

func FileMode(path string) (fs.FileMode, error)

FileMode return file's mode and permission

func FileSize added in v1.3.8

func FileSize(path string) (int64, error)

FileSize returns file size in bytes.

func IsDir

func IsDir(path string) bool

IsDir checks if the path is directory or not

func IsExist

func IsExist(path string) bool

IsExist checks if a file or directory exists

func IsLink(path string) bool

IsLink checks if a file is symbol link or not

func IsZipFile added in v1.3.8

func IsZipFile(filepath string) bool

IsZipFile checks if file is zip or not.

func ListFileNames

func ListFileNames(path string) ([]string, error)

ListFileNames return all file names in the path

func MTime added in v1.3.8

func MTime(filepath string) (int64, error)

MTime returns file modified time.

func MiMeType added in v1.1.8

func MiMeType(file interface{}) string

MiMeType return file mime type param `file` should be string(file path) or *os.File

func ParallelChunkRead added in v1.4.3

func ParallelChunkRead(filePath string, linesCh chan<- []string, ChunkSizeMB, MaxGoroutine int)

并行读取文件并将每个块的行发送到指定通道 filePath 文件路径 ChunkSizeMB 分块的大小(单位MB,设置为0时使用默认100MB),设置过大反而不利,视情调整 MaxGoroutine 并发读取分块的数量,设置为0时使用CPU核心数 linesCh用于接收返回结果的通道。

func ReadCsvFile added in v1.3.8

func ReadCsvFile(filepath string, delimiter ...rune) ([][]string, error)

ReadCsvFile read file content into slice.

func ReadFile added in v1.4.2

func ReadFile(path string) (reader io.ReadCloser, closeFn func(), err error)

ReadFile get file reader by a url or a local file.

func ReadFileByLine added in v1.0.6

func ReadFileByLine(path string) ([]string, error)

ReadFileByLine read file line by line

func ReadFileToString added in v1.0.6

func ReadFileToString(path string) (string, error)

ReadFileToString return string of file content

func RemoveFile

func RemoveFile(path string) error

RemoveFile remove the path file

func Sha added in v1.3.8

func Sha(filepath string, shaType ...int) (string, error)

Sha returns file sha value, param `shaType` should be 1, 256 or 512.

func UnZip added in v1.1.8

func UnZip(zipFile string, destPath string) error

UnZip unzip the file and save it to destPath

func WriteBytesToFile added in v1.3.9

func WriteBytesToFile(filepath string, content []byte) error

WriteBytesToFile write bytes to target file.

func WriteCsvFile added in v1.4.0

func WriteCsvFile(filepath string, records [][]string, append bool, delimiter ...rune) error

WriteCsvFile write content to target csv file.

func WriteMapsToCsv added in v1.4.3

func WriteMapsToCsv(filepath string, records []map[string]interface{}, appendToExistingFile bool, delimiter rune, headers ...[]string) error

WriteMapsToCsv write slice of map to csv file. Play: https://go.dev/play/p/umAIomZFV1c filepath: Path to the CSV file. records: Slice of maps to be written. the value of map should be basic type. the maps will be sorted by key in alphabeta order, then be written into csv file. appendToExistingFile: If true, data will be appended to the file if it exists. delimiter: Delimiter to use in the CSV file. headers: order of the csv column headers, needs to be consistent with the key of the map.

func WriteStringToFile added in v1.3.9

func WriteStringToFile(filepath string, content string, append bool) error

WriteStringToFile write string to target file.

func Zip added in v1.1.8

func Zip(path string, destPath string) error

Zip create zip file, fpath could be a single file or a directory

func ZipAppendEntry added in v1.4.0

func ZipAppendEntry(fpath string, destPath string) error

ZipAppendEntry append a single file or directory by fpath to an existing zip file. Play: https://go.dev/play/p/cxvaT8TRNQp

Types

This section is empty.

Jump to

Keyboard shortcuts

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