# Fileutil

Package fileutil implements some basic functions for file operations.

# Source:

# Usage:

import (
    "github.com/duke-git/lancet/v2/fileutil"
)

# Documentation

# ClearFile

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

Signature:

func ClearFile(path string) error

Example:

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:

func CreateFile(path string) bool

Example:

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:

func CreateDir(absPath string) error

Example:

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:

func CopyFile(srcFilePath string, dstFilePath string) error

Example:

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)
    }
}

# FileMode

Return file mode infomation.

Signature:

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

Example:

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:

func MiMeType(file any) string

Example:

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:

func IsExist(path string) bool

Example:

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
}

Checks if a file is symbol link or not.

Signature:

func IsLink(path string) bool

Example:

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:

func IsDir(path string) bool

Example:

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:

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

Example:

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:

func RemoveFile(path string) error

Example:

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:

func ReadFileToString(path string) (string, error)

Example:

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:

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

Example:

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:

func Zip(fpath string, destPath string) error

Example:

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)
    }
}

# UnZip

Unzip the file and save it to dest path.

Signature:

func UnZip(zipFile string, destPath string) error

Example:

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)
    }
}
Last Updated: 9/29/2022, 1:53:14 PM