fileutil

package
v2.3.4 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2024 License: MIT Imports: 19 Imported by: 92

Documentation

Overview

Package fileutil implements some basic functions for file operations

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ChunkRead added in v2.3.0

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

ChunkRead reads a block from the file at the specified offset and returns all lines within the block Play: https://go.dev/play/p/r0hPmKWhsgf

Example
const mb = 1024 * 1024
const defaultChunkSizeMB = 100

filePath := "./testdata/test1.csv"
f, err := os.Open(filePath)
if err != nil {
	return
}

defer f.Close()

var bufPool = sync.Pool{
	New: func() interface{} {
		return make([]byte, 0, defaultChunkSizeMB*mb)
	},
}

lines, err := ChunkRead(f, 0, 100, &bufPool)
if err != nil {
	return
}

fmt.Println(lines[0])
fmt.Println(lines[1])
Output:

Lili,22,female
Jim,21,male

func ClearFile

func ClearFile(path string) error

ClearFile write empty string to path file. Play: https://go.dev/play/p/NRZ0ZT-G94H

Example
fname := "./test.txt"
CreateFile(fname)

f, _ := os.OpenFile(fname, os.O_WRONLY|os.O_TRUNC, 0777)
defer f.Close()

_, err := f.WriteString("hello world")
if err != nil {
	return
}

content1, _ := ReadFileToString(fname)

err = ClearFile(fname)
if err != nil {
	return
}
content2, _ := ReadFileToString(fname)

os.Remove(fname)

fmt.Println(content1)
fmt.Println(content2)
Output:

hello world

func CopyDir added in v2.3.0

func CopyDir(srcPath string, dstPath string) error

CopyDir copy src directory to dst directory, it will copy all files and directories recursively. the access permission will be the same as the source directory. if dstPath exists, it will return an error. Play: https://go.dev/play/p/YAyFTA_UuPb

func CopyFile

func CopyFile(srcPath string, dstPath string) error

CopyFile copy src file to dest file. Play: https://go.dev/play/p/Jg9AMJMLrJi

func CreateDir added in v2.0.9

func CreateDir(absPath string) error

CreateDir create directory in absolute path. param `absPath` like /a/, /a/b/. Play: https://go.dev/play/p/qUuCe1OGQnM

Example
pwd, _ := os.Getwd()
dirPath := pwd + "/createdir/a/b"

result1 := IsExist(dirPath)

err := CreateDir(dirPath)
if err != nil {
	return
}

result2 := IsExist(pwd + "/createdir/")
result3 := IsExist(pwd + "/createdir/a")
result4 := IsExist(pwd + "/createdir/a/b")

fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)

os.RemoveAll(pwd + "/createdir/")
Output:

false
true
true
true

func CreateFile

func CreateFile(path string) bool

CreateFile create a file in path. Play: https://go.dev/play/p/lDt8PEsTNKI

Example
fname := "./a.txt"

result1 := IsExist(fname)

CreateFile(fname)

result2 := IsExist(fname)

os.Remove(fname)

fmt.Println(result1)
fmt.Println(result2)
Output:

false
true

func CurrentPath added in v2.1.19

func CurrentPath() string

CurrentPath return current absolute path. Play: https://go.dev/play/p/s74a9iBGcSw

func DirSize added in v2.2.9

func DirSize(path string) (int64, error)

DirSize walks the folder recursively and returns folder size in bytes.

func FileMode

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

FileMode return file's mode and permission. Play: https://go.dev/play/p/2l2hI42fA3p

func FileSize added in v2.1.20

func FileSize(path string) (int64, error)

FileSize returns file size in bytes. Play: https://go.dev/play/p/H9Z05uD-Jjc

Example
size, err := FileSize("./testdata/test.txt")

fmt.Println(size)
fmt.Println(err)
Output:

20
<nil>

func IsDir

func IsDir(path string) bool

IsDir checks if the path is directory or not. Play: https://go.dev/play/p/WkVwEKqtOWk

Example
result1 := IsDir("./")
result2 := IsDir("./xxx.go")

fmt.Println(result1)
fmt.Println(result2)
Output:

true
false

func IsExist

func IsExist(path string) bool

IsExist checks if a file or directory exists. Play: https://go.dev/play/p/nKKXt8ZQbmh

Example
result1 := IsExist("./")
result2 := IsExist("./xxx.go")

fmt.Println(result1)
fmt.Println(result2)
Output:

true
false
func IsLink(path string) bool

IsLink checks if a file is symbol link or not. Play: https://go.dev/play/p/TL-b-Kzvf44

func IsZipFile added in v2.1.20

func IsZipFile(filepath string) bool

IsZipFile checks if file is zip or not. Play: https://go.dev/play/p/9M0g2j_uF_e

Example
result1 := IsZipFile("./file.go")
result2 := IsZipFile("./testdata/file.go.zip")

fmt.Println(result1)
fmt.Println(result2)
Output:

false
true

func ListFileNames

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

ListFileNames return all file names in the path. Play: https://go.dev/play/p/Tjd7Y07rejl

Example
fileList, _ := ListFileNames("../internal")
fmt.Println(fileList)
Output:

[assert.go assert_test.go error_join.go]

func MTime added in v2.1.20

func MTime(filepath string) (int64, error)

MTime returns file modified time. Play: https://go.dev/play/p/s_Tl7lZoAaY

func MiMeType

func MiMeType(file any) string

MiMeType return file mime type param `file` should be string(file path) or *os.File. Play: https://go.dev/play/p/bd5sevSUZNu

Example
fname := "./test.txt"
file, _ := os.Create(fname)

_, err := file.WriteString("hello\nworld")
if err != nil {
	return
}

f, _ := os.Open(fname)
defer f.Close()

mimeType := MiMeType(f)
fmt.Println(mimeType)

os.Remove(fname)
Output:

application/octet-stream

func ParallelChunkRead added in v2.3.0

func ParallelChunkRead(filePath string, linesCh chan<- []string, chunkSizeMB, maxGoroutine int) error

ParallelChunkRead reads the file in parallel and send each chunk of lines to the specified channel. filePath 文件路径 chunkSizeMB 分块的大小(单位MB,设置为0时使用默认100MB),设置过大反而不利,视情调整 maxGoroutine 并发读取分块的数量,设置为0时使用CPU核心数 linesCh用于接收返回结果的通道。 Play: https://go.dev/play/p/teMXnCsdSEw

Example
const mb = 1024 * 1024
const defaultChunkSizeMB = 100 // 默认值

numParsers := runtime.NumCPU()

linesCh := make(chan []string, numParsers)
filePath := "./testdata/test1.csv"

go ParallelChunkRead(filePath, linesCh, defaultChunkSizeMB, numParsers)

var totalLines int
for lines := range linesCh {
	totalLines += len(lines)

	for _, line := range lines {
		fmt.Println(line)
	}
}

fmt.Println(totalLines)
Output:

Lili,22,female
Jim,21,male
2

func ReadCsvFile added in v2.1.20

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

ReadCsvFile read file content into slice. Play: https://go.dev/play/p/OExTkhGEd3_u

Example
content, err := ReadCsvFile("./testdata/demo.csv")

fmt.Println(content)
fmt.Println(err)
Output:

[[Bob  12  male] [Duke  14  male] [Lucy  16  female]]
<nil>

func ReadFile added in v2.2.7

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

ReadFile get file reader by a url or a local file Play: https://go.dev/play/p/uNep3Tr8fqF

Example
reader, fn, err := ReadFile("https://httpbin.org/robots.txt")
if err != nil {
	return
}
defer fn()

dat, err := io.ReadAll(reader)
if err != nil {
	return
}

fmt.Println(string(dat))
Output:

User-agent: *
Disallow: /deny

func ReadFileByLine

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

ReadFileByLine read file line by line. Play: https://go.dev/play/p/svJP_7ZrBrD

Example
fname := "./test.txt"
CreateFile(fname)

f, _ := os.OpenFile(fname, os.O_WRONLY|os.O_TRUNC, 0777)
defer f.Close()

_, err := f.WriteString("hello\nworld")
if err != nil {
	return
}

content, _ := ReadFileByLine(fname)

os.Remove(fname)

fmt.Println(content)
Output:

[hello world]

func ReadFileToString

func ReadFileToString(path string) (string, error)

ReadFileToString return string of file content. Play: https://go.dev/play/p/cmfwp_5SQTp

Example
fname := "./test.txt"
CreateFile(fname)

f, _ := os.OpenFile(fname, os.O_WRONLY|os.O_TRUNC, 0777)
defer f.Close()

_, err := f.WriteString("hello world")
if err != nil {
	return
}

content, _ := ReadFileToString(fname)

os.Remove(fname)

fmt.Println(content)
Output:

hello world

func RemoveFile

func RemoveFile(path string) error

RemoveFile remove the path file. Play: https://go.dev/play/p/P2y0XW8a1SH

Example
srcFile := "./text.txt"
CreateFile(srcFile)

copyFile := "./text_copy.txt"
err := CopyFile(srcFile, copyFile)
if err != nil {
	return
}
file, err := os.Open(copyFile)
if err != nil {
	return
}
result1 := IsExist(copyFile)
result2 := file.Name()

os.Remove(srcFile)
os.Remove(copyFile)

fmt.Println(result1)
fmt.Println(result2)
Output:

true
./text_copy.txt

func Sha added in v2.1.20

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

Sha returns file sha value, param `shaType` should be 1, 256 or 512. Play: https://go.dev/play/p/VfEEcO2MJYf

Example
sha1, err := Sha("./testdata/test.txt", 1)
sha256, _ := Sha("./testdata/test.txt", 256)
sha512, _ := Sha("./testdata/test.txt", 512)

fmt.Println(sha1)
fmt.Println(sha256)
fmt.Println(sha512)
fmt.Println(err)
Output:

dda3cf10c5a6ff6c6659a497bf7261b287af2bc7
aa6d0a3fbc3442c228d606da09e0c1dc98c69a1cac3da1909199e0266171df35
d22aba2a1b7a2e2f512756255cc1c3708905646920cb1eb95e45b531ba74774dbbb89baebf1f716220eb9cf4908f1cfc5b2a01267704d9a59f59d77cab609870
<nil>

func UnZip

func UnZip(zipFile string, destPath string) error

UnZip unzip the file and save it to destPath. Play: https://go.dev/play/p/g0w34kS7B8m

Example
zipFile := "./testdata/file.go.zip"

err := UnZip(zipFile, "./testdata")
if err != nil {
	return
}

exist := IsExist("./testdata/file.go")
fmt.Println(exist)

os.Remove("./testdata/file.go")
Output:

true

func WriteBytesToFile added in v2.2.1

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

WriteBytesToFile write bytes to target file. Play: https://go.dev/play/p/s7QlDxMj3P8

Example
filepath := "./bytes.txt"

file, err := os.Create(filepath)
if err != nil {
	return
}

defer file.Close()

err = WriteBytesToFile(filepath, []byte("hello"))
if err != nil {
	return
}

content, err := ReadFileToString(filepath)
if err != nil {
	return
}

os.Remove(filepath)

fmt.Println(content)
Output:

hello

func WriteCsvFile added in v2.2.2

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

WriteCsvFile write content to target csv file. append: append to existing csv file delimiter: specifies csv delimiter Play: https://go.dev/play/p/dAXm58Q5U1o

Example
data := [][]string{
	{"Lili", "22", "female"},
	{"Jim", "21", "male"},
}
err := WriteCsvFile("./testdata/test2.csv", data, false)
fmt.Println(err)

content, _ := ReadCsvFile("./testdata/test2.csv")
fmt.Println(content)
Output:

<nil>
[[Lili 22 female] [Jim 21 male]]

func WriteMapsToCsv added in v2.2.9

func WriteMapsToCsv(filepath string, records []map[string]any, 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.

Example
csvFilePath := "./testdata/test3.csv"
records := []map[string]any{
	{"Name": "Lili", "Age": "22", "Gender": "female"},
	{"Name": "Jim", "Age": "21", "Gender": "male"},
}

headers := []string{"Name", "Age", "Gender"}
err := WriteMapsToCsv(csvFilePath, records, false, ';', headers)

if err != nil {
	log.Fatal(err)
}

content, err := ReadCsvFile(csvFilePath, ';')

fmt.Println(content)
Output:

[[Name Age Gender] [Lili 22 female] [Jim 21 male]]

func WriteStringToFile added in v2.2.1

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

WriteStringToFile write string to target file. Play: https://go.dev/play/p/GhLS6d8lH_g

Example
filepath := "./test.txt"

file, err := os.Create(filepath)
if err != nil {
	return
}

defer file.Close()

err = WriteStringToFile(filepath, "hello", true)
if err != nil {
	return
}

content, err := ReadFileToString(filepath)
if err != nil {
	return
}

os.Remove(filepath)

fmt.Println(content)
Output:

hello

func Zip

func Zip(path string, destPath string) error

Zip create zip file, fpath could be a single file or a directory. Play: https://go.dev/play/p/j-3sWBp8ik_P

Example
srcFile := "./test.txt"
CreateFile(srcFile)

zipFile := "./test.zip"
err := Zip(srcFile, zipFile)
if err != nil {
	return
}

result := IsExist(zipFile)

os.Remove(srcFile)
os.Remove(zipFile)

fmt.Println(result)
Output:

true

func ZipAppendEntry added in v2.2.2

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

Example
zipFile := "./test.zip"
CopyFile("./testdata/file.go.zip", zipFile)

ZipAppendEntry("./testdata", zipFile)

unZipPath := "./unzip"
UnZip(zipFile, unZipPath)

fmt.Println(IsExist("./unzip/file.go"))
fmt.Println(IsExist("./unzip/testdata/file.go.zip"))
fmt.Println(IsExist("./unzip/testdata/test.txt"))

os.Remove(zipFile)
os.RemoveAll(unZipPath)
Output:

true
true
true

Types

type FileReader added in v2.2.9

type FileReader struct {
	*bufio.Reader
	// contains filtered or unexported fields
}

FileReader is a reader supporting offset seeking and reading one line at a time, this is especially useful for large files

func NewFileReader added in v2.2.9

func NewFileReader(path string) (*FileReader, error)

NewFileReader creates the FileReader struct for reading

func (*FileReader) Close added in v2.2.9

func (f *FileReader) Close() error

Close takes care of the opened file

func (*FileReader) Offset added in v2.2.9

func (f *FileReader) Offset() int64

Offset returns the current offset of the file

func (*FileReader) ReadLine added in v2.2.9

func (f *FileReader) ReadLine() (string, error)

ReadLine reads and returns one line at a time excluding the trailing '\r' and '\n'

func (*FileReader) SeekOffset added in v2.3.0

func (f *FileReader) SeekOffset(offset int64) error

SeekOffset sets the current offset of the reading

Jump to

Keyboard shortcuts

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