# Fileutil Package fileutil implements some basic functions for file operations.
## Source: - [https://github.com/duke-git/lancet/blob/main/fileutil/file.go](https://github.com/duke-git/lancet/blob/main/fileutil/file.go)
## Usage: ```go import ( "github.com/duke-git/lancet/v2/fileutil" ) ```
## Index - [ClearFile](#ClearFile) - [CreateFile](#CreateFile) - [CreateDir](#CreateDir) - [CopyFile](#CopyFile) - [CurrentPath](#CurrentPath) - [FileMode](#FileMode) - [MiMeType](#MiMeType) - [IsExist](#IsExist) - [IsLink](#IsLink) - [IsDir](#IsDir) - [ListFileNames](#ListFileNames) - [RemoveFile](#RemoveFile) - [ReadFileToString](#ReadFileToString) - [ReadFileByLine](#ReadFileByLine) - [Zip](#Zip) - [ZipAppendEntry](#ZipAppendEntry) - [UnZip](#UnZip) - [IsZipFile](#IsZipFile) - [FileSize](#FileSize) - [MTime](#MTime) - [Sha](#Sha) - [ReadCsvFile](#ReadCsvFile) - [WriteCsvFile](#WriteCsvFile) - [WriteStringToFile](#WriteStringToFile) - [WriteBytesToFile](#WriteBytesToFile)
## Documentation ### ClearFile

Clear the file content, write empty string to the file.

Signature: ```go func ClearFile(path string) error ``` Example: ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/fileutil" ) func main() { err := fileutil.ClearFile("./test.txt") if err != nil { fmt.Println(err) } } ``` ### CreateFile

Create file in path. return true if create succeed.

Signature: ```go func CreateFile(path string) bool ``` Example: ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/fileutil" ) func main() { isCreatedSucceed := fileutil.CreateFile("./test.txt") fmt.Println(isCreatedSucceed) } ``` ### CreateDir

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

Signature: ```go func CreateDir(absPath string) error ``` Example: ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/fileutil" ) func main() { err := fileutil.CreateDir("/a/") fmt.Println(err) } ``` ### CopyFile

Copy src file to dest file. If dest file exist will overwrite it.

Signature: ```go func CopyFile(srcPath string, dstPath string) error ``` Example: ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/fileutil" ) func main() { err := fileutil.CopyFile("./test.txt", "./test_copy.txt") if err != nil { fmt.Println(err) } } ``` ### CurrentPath

return current absolute path.

Signature: ```go func CurrentPath() string ``` Example: ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/fileutil" ) func main() { absPath := CurrentPath() fmt.Println(absPath) } ``` ### FileMode

Return file mode infomation.

Signature: ```go func FileMode(path string) (fs.FileMode, error) ``` Example: ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/fileutil" ) func main() { mode, err := fileutil.FileMode("./test.txt") if err != nil { fmt.Println(err) } fmt.Println(mode) } ``` ### MiMeType

Get file mime type, 'file' param's type should be string or *os.File.

Signature: ```go func MiMeType(file any) string ``` Example: ```go package main import ( "fmt" "os" "github.com/duke-git/lancet/v2/fileutil" ) func main() { type1 := fileutil.MiMeType("./test.txt") fmt.Println(type1) //text/plain; charset=utf-8 f, _ := os.Open("./file.go") type2 := fileutil.MiMeType(f) fmt.Println(type2) //text/plain; charset=utf-8 } ``` ### IsExist

Checks if a file or directory exists.

Signature: ```go func IsExist(path string) bool ``` Example: ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/fileutil" ) func main() { fileutil.CreateFile("./test.txt") isFileExist := fileutil.IsExist("./test.txt") fmt.Println(isFileExist) //true } ``` ### IsLink

Checks if a file is symbol link or not.

Signature: ```go func IsLink(path string) bool ``` Example: ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/fileutil" ) func main() { isLinkFile := fileutil.IsLink("./test.txt") fmt.Println(isLinkFile) //false } ``` ### IsDir

Checks if the path is directy or not.

Signature: ```go func IsDir(path string) bool ``` Example: ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/fileutil" ) func main() { isDir := fileutil.IsDir("./") fmt.Println(isDir) //true isDir = fileutil.IsDir("./test.txt") fmt.Println(isDir) //false } ``` ### ListFileNames

List all file names in given path.

Signature: ```go func ListFileNames(path string) ([]string, error) ``` Example: ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/fileutil" ) func main() { fileNames, _ := fileutil.ListFileNames("./") fmt.Println(fileNames) } ``` ### RemoveFile

Remove the file of path.

Signature: ```go func RemoveFile(path string) error ``` Example: ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/fileutil" ) func main() { err := fileutil.RemoveFile("./test.txt") if err != nil { fmt.Println(err) } } ``` ### ReadFileToString

Return string of file content.

Signature: ```go func ReadFileToString(path string) (string, error) ``` Example: ```go package main import ( "fmt" "os" "github.com/duke-git/lancet/v2/fileutil" ) func main() { path := "./test.txt" fileutil.CreateFile(path) f, _ := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0777) f.WriteString("hello world") content, _ := fileutil.ReadFileToString(path) fmt.Println(content) //hello world } ``` ### ReadFileByLine

Read file line by line, and return slice of lines

Signature: ```go func ReadFileByLine(path string)([]string, error) ``` Example: ```go package main import ( "fmt" "os" "github.com/duke-git/lancet/v2/fileutil" ) func main() { path := "./text.txt" fileutil.CreateFile(path) f, _ := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0777) defer f.Close() f.WriteString("hello\nworld") contents, _ := fileutil.ReadFileByLine(path) fmt.Println(contents) //[]string{"hello", "world"} } ``` ### Zip

Create a zip file of fpath, fpath could be a file or a directory.

Signature: ```go func Zip(fpath string, destPath string) error ``` Example: ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/fileutil" ) func main() { err := fileutil.Zip("./test.txt", "./test.zip") if err != nil { fmt.Println(err) } } ``` ### ZipAppendEntry

Append a single file or directory by fpath to an existing zip file.

Signature: ```go func ZipAppendEntry(fpath string, destPath string) error ``` Example: ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/fileutil" ) func main() { err := fileutil.ZipAppendEntry("./test.txt", "./test.zip") if err != nil { fmt.Println(err) } } ``` ### UnZip

Unzip the file and save it to dest path.

Signature: ```go func UnZip(zipFile string, destPath string) error ``` Example: ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/fileutil" ) func main() { err := fileutil.Zip("./test.zip", "./unzip/test.txt") if err != nil { fmt.Println(err) } } ``` ### IsZipFile

Checks if file is zip file or not.

Signature: ```go func IsZipFile(filepath string) bool ``` Example: ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/fileutil" ) func main() { isZip := fileutil.IsZipFile("./zipfile.zip") fmt.Println(isZip) } ``` ### FileSize

Returns file size in bytes.

Signature: ```go func FileSize(path string) (int64, error) ``` Example: ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/fileutil" ) func main() { size, err := fileutil.FileSize("./testdata/test.txt") fmt.Println(size) fmt.Println(err) // Output: // 20 // } ``` ### MTime

Returns file modified time(unix timestamp).

Signature: ```go func MTime(filepath string) (int64, error) ``` Example: ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/fileutil" ) func main() { mtime, err := fileutil.MTime("./testdata/test.txt") fmt.Println(mtime) fmt.Println(err) // Output: // 1682391110 // } ``` ### Sha

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

Signature: ```go func Sha(filepath string, shaType ...int) (string, error) ``` Example: ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/fileutil" ) func main() { sha1, err := fileutil.Sha("./testdata/test.txt", 1) sha256, _ := fileutil.Sha("./testdata/test.txt", 256) sha512, _ := fileutil.Sha("./testdata/test.txt", 512) fmt.Println(sha1) fmt.Println(sha256) fmt.Println(sha512) fmt.Println(err) // Output: // dda3cf10c5a6ff6c6659a497bf7261b287af2bc7 // aa6d0a3fbc3442c228d606da09e0c1dc98c69a1cac3da1909199e0266171df35 // d22aba2a1b7a2e2f512756255cc1c3708905646920cb1eb95e45b531ba74774dbbb89baebf1f716220eb9cf4908f1cfc5b2a01267704d9a59f59d77cab609870 // } ``` ### ReadCsvFile

Reads file content into slice.

Signature: ```go func ReadCsvFile(filepath string) ([][]string, error) ``` Example: ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/fileutil" ) func main() { content, err := fileutil.ReadCsvFile("./testdata/test.csv") fmt.Println(content) fmt.Println(err) // Output: // [[Bob 12 male] [Duke 14 male] [Lucy 16 female]] // } ``` ### WriteCsvFile

Write content to target csv file.

Signature: ```go func WriteCsvFile(filepath string, records [][]string, append bool) error ``` Example: ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/fileutil" ) func main() { fpath := "./test.csv" fileutil.CreateFile(fpath) f, _ := os.OpenFile(fpath, os.O_WRONLY|os.O_TRUNC, 0777) defer f.Close() data := [][]string{ {"Lili", "22", "female"}, {"Jim", "21", "male"}, } err := fileutil.WriteCsvFile(fpath, data, false) if err != nil { return } content, err := fileutil.ReadCsvFile(fpath) if err != nil { return } fmt.Println(content) // Output: // [[Lili 22 female] [Jim 21 male]] } ``` ### WriteBytesToFile

Writes bytes to target file.

Signature: ```go func WriteBytesToFile(filepath string, content []byte) error ``` Example: ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/fileutil" ) func main() { filepath := "./bytes.txt" file, err := os.Create(filepath) if err != nil { return } defer file.Close() err = fileutil.WriteBytesToFile(filepath, []byte("hello")) if err != nil { return } content, err := fileutil.ReadFileToString(filepath) if err != nil { return } os.Remove(filepath) fmt.Println(content) // Output: // hello } ``` ### WriteStringToFile

Writes string to target file.

Signature: ```go func WriteStringToFile(filepath string, content string, append bool) error ``` Example: ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/fileutil" ) func main() { filepath := "./test.txt" file, err := os.Create(filepath) if err != nil { return } defer file.Close() err = fileutil.WriteStringToFile(filepath, "hello", true) if err != nil { return } content, err := fileutil.ReadFileToString(filepath) if err != nil { return } os.Remove(filepath) fmt.Println(content) // Output: // hello } ```