goframe gfile用法

        GoFrame 的  gfile  模块提供了一套丰富的文件和目录操作方法,使得文件系统的操作变得更加简单易用。以下是一些  gfile  的常用方法及其用法:

1. 获取绝对路径:使用  Abs  函数可以获取给定路径的绝对表示形式,如果路径不是绝对路径,它会与当前工作目录联接以转换为绝对路径 。


2. 获取文件名: Basename  函数可以返回路径的最后一个元素,即文件名,包括文件扩展名 。

3. 复制文件或目录: Copy  函数用于复制文件或目录,它支持不同情况下的复制操作,例如复制单个文件或整个目录 。

4. 获取文件扩展名: Ext  函数返回文件的扩展名,包括点( . ),而  ExtName  函数返回的扩展名则不包含点 。

5. 格式化文件大小: FormatSize  函数可以将文件大小转换为更易于阅读的格式 。

6. 读取文件内容: GetBytes  函数可以读取文件的全部内容,并以字节切片的形式返回 。

7. 读取文件的一部分内容: GetBytesByTwoOffsets  函数可以根据给定的起始和结束位置读取文件的一部分内容 。

8. 打开文件: Open  函数以只读模式打开文件或目录,而  OpenFile  和  OpenWithFlag  函数允许使用自定义的标志和权限来打开文件或目录 。

9. 获取特定字符的文件偏移量: GetNextCharOffsetByPath  函数可以根据给定的字符和起始位置返回文件中的偏移量 。

        这些方法提供了对文件系统进行操作的便利性,隐藏了底层实现的复杂性,使得开发者可以更专注于业务逻辑的实现。更多详细的使用示例和高级用法,可以查阅 GoFrame 的官方文档和接口文档 。

// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.

package gfile_test

import (
    "os"
    "path/filepath"
    "strings"
    "testing"

    "github.com/gogf/gf/v2/os/gfile"
    "github.com/gogf/gf/v2/os/gtime"
    "github.com/gogf/gf/v2/test/gtest"
    "github.com/gogf/gf/v2/util/gconv"
)

func Test_IsDir(t *testing.T) {
    gtest.C(t, func(t *gtest.T) {
       paths := "/testfile"
       createDir(paths)
       defer delTestFiles(paths)

       t.Assert(gfile.IsDir(testpath()+paths), true)
       t.Assert(gfile.IsDir("./testfile2"), false)
       t.Assert(gfile.IsDir("./testfile/tt.txt"), false)
       t.Assert(gfile.IsDir(""), false)
    })
}

func Test_IsEmpty(t *testing.T) {
    gtest.C(t, func(t *gtest.T) {
       path := "/testdir_" + gconv.String(gtime.TimestampNano())
       createDir(path)
       defer delTestFiles(path)

       t.Assert(gfile.IsEmpty(testpath()+path), true)
       t.Assert(gfile.IsEmpty(testpath()+path+gfile.Separator+"test.txt"), true)
    })
    gtest.C(t, func(t *gtest.T) {
       path := "/testfile_" + gconv.String(gtime.TimestampNano())
       createTestFile(path, "")
       defer delTestFiles(path)

       t.Assert(gfile.IsEmpty(testpath()+path), true)
    })
    gtest.C(t, func(t *gtest.T) {
       path := "/testfile_" + gconv.String(gtime.TimestampNano())
       createTestFile(path, "1")
       defer delTestFiles(path)

       t.Assert(gfile.IsEmpty(testpath()+path), false)
    })
}

func Test_Create(t *testing.T) {
    gtest.C(t, func(t *gtest.T) {
       var (
          err       error
          filepaths []string
          fileobj   *os.File
       )
       filepaths = append(filepaths, "/testfile_cc1.txt")
       filepaths = append(filepaths, "/testfile_cc2.txt")
       for _, v := range filepaths {
          fileobj, err = gfile.Create(testpath() + v)
          defer delTestFiles(v)
          fileobj.Close()
          t.AssertNil(err)
       }
    })

    gtest.C(t, func(t *gtest.T) {
       tmpPath := gfile.Join(gfile.Temp(), "test/testfile_cc1.txt")
       fileobj, err := gfile.Create(tmpPath)
       defer gfile.Remove(tmpPath)
       t.AssertNE(fileobj, nil)
       t.AssertNil(err)
       fileobj.Close()
    })
}

func Test_Open(t *testing.T) {
    gtest.C(t, func(t *gtest.T) {
       var (
          err     error
          files   []string
          flags   []bool
          fileobj *os.File
       )

       file1 := "/testfile_nc1.txt"
       createTestFile(file1, "")
       defer delTestFiles(file1)

       files = append(files, file1)
       flags = append(flags, true)

       files = append(files, "./testfile/file1/c1.txt")
       flags = append(flags, false)

       for k, v := range files {
          fileobj, err = gfile.Open(testpath() + v)
          fileobj.Close()
          if flags[k] {
             t.AssertNil(err)
          } else {
             t.AssertNE(err, nil)
          }

       }

    })
}

func Test_OpenFile(t *testing.T) {
    gtest.C(t, func(t *gtest.T) {
       var (
          err     error
          files   []string
          flags   []bool
          fileobj *os.File
       )

       files = append(files, "./testfile/file1/nc1.txt")
       flags = append(flags, false)

       f1 := "/testfile_tt.txt"
       createTestFile(f1, "")
       defer delTestFiles(f1)

       files = append(files, f1)
       flags = append(flags, true)

       for k, v := range files {
          fileobj, err = gfile.OpenFile(testpath()+v, os.O_RDWR, 0666)
          fileobj.Close()
          if flags[k] {
             t.AssertNil(err)
          } else {
             t.AssertNE(err, nil)
          }

       }

    })
}

func Test_OpenWithFlag(t *testing.T) {
    gtest.C(t, func(t *gtest.T) {
       var (
          err     error
          files   []string
          flags   []bool
          fileobj *os.File
       )

       file1 := "/testfile_t1.txt"
       createTestFile(file1, "")
       defer delTestFiles(file1)
       files = append(files, file1)
       flags = append(flags, true)

       files = append(files, "/testfiless/dirfiles/t1_no.txt")
       flags = append(flags, false)

       for k, v := range files {
          fileobj, err = gfile.OpenWithFlag(testpath()+v, os.O_RDWR)
          fileobj.Close()
          if flags[k] {
             t.AssertNil(err)
          } else {
             t.AssertNE(err, nil)
          }

       }

    })
}

func Test_OpenWithFlagPerm(t *testing.T) {
    gtest.C(t, func(t *gtest.T) {
       var (
          err     error
          files   []string
          flags   []bool
          fileobj *os.File
       )
       file1 := "/testfile_nc1.txt"
       createTestFile(file1, "")
       defer delTestFiles(file1)
       files = append(files, file1)
       flags = append(flags, true)

       files = append(files, "/testfileyy/tt.txt")
       flags = append(flags, false)

       for k, v := range files {
          fileobj, err = gfile.OpenWithFlagPerm(testpath()+v, os.O_RDWR, 0666)
          fileobj.Close()
          if flags[k] {
             t.AssertNil(err)
          } else {
             t.AssertNE(err, nil)
          }

       }

    })
}

func Test_Exists(t *testing.T) {

    gtest.C(t, func(t *gtest.T) {
       var (
          flag  bool
          files []string
          flags []bool
       )

       file1 := "/testfile_GetContents.txt"
       createTestFile(file1, "")
       defer delTestFiles(file1)

       files = append(files, file1)
       flags = append(flags, true)

       files = append(files, "./testfile/havefile1/tt_no.txt")
       flags = append(flags, false)

       for k, v := range files {
          flag = gfile.Exists(testpath() + v)
          if flags[k] {
             t.Assert(flag, true)
          } else {
             t.Assert(flag, false)
          }

       }

    })
}

func Test_Pwd(t *testing.T) {
    gtest.C(t, func(t *gtest.T) {
       paths, err := os.Getwd()
       t.AssertNil(err)
       t.Assert(gfile.Pwd(), paths)

    })
}

func Test_IsFile(t *testing.T) {
    gtest.C(t, func(t *gtest.T) {
       var (
          flag  bool
          files []string
          flags []bool
       )

       file1 := "/testfile_tt.txt"
       createTestFile(file1, "")
       defer delTestFiles(file1)
       files = append(files, file1)
       flags = append(flags, true)

       dir1 := "/testfiless"
       createDir(dir1)
       defer delTestFiles(dir1)
       files = append(files, dir1)
       flags = append(flags, false)

       files = append(files, "./testfiledd/tt1.txt")
       flags = append(flags, false)

       for k, v := range files {
          flag = gfile.IsFile(testpath() + v)
          if flags[k] {
             t.Assert(flag, true)
          } else {
             t.Assert(flag, false)
          }

       }

    })
}

func Test_Info(t *testing.T) {
    gtest.C(t, func(t *gtest.T) {
       var (
          err    error
          paths  string = "/testfile_t1.txt"
          files  os.FileInfo
          files2 os.FileInfo
       )

       createTestFile(paths, "")
       defer delTestFiles(paths)
       files, err = gfile.Stat(testpath() + paths)
       t.AssertNil(err)

       files2, err = os.Stat(testpath() + paths)
       t.AssertNil(err)

       t.Assert(files, files2)

    })
}

func Test_Move(t *testing.T) {
    gtest.C(t, func(t *gtest.T) {
       var (
          paths     string = "/ovetest"
          filepaths string = "/testfile_ttn1.txt"
          topath    string = "/testfile_ttn2.txt"
       )
       createDir("/ovetest")
       createTestFile(paths+filepaths, "a")

       defer delTestFiles(paths)

       yfile := testpath() + paths + filepaths
       tofile := testpath() + paths + topath

       t.Assert(gfile.Move(yfile, tofile), nil)

       // 检查移动后的文件是否真实存在
       _, err := os.Stat(tofile)
       t.Assert(os.IsNotExist(err), false)

    })
}

func Test_Rename(t *testing.T) {
    gtest.C(t, func(t *gtest.T) {
       var (
          paths  string = "/testfiles"
          ypath  string = "/testfilettm1.txt"
          topath string = "/testfilettm2.txt"
       )
       createDir(paths)
       createTestFile(paths+ypath, "a")
       defer delTestFiles(paths)

       ypath = testpath() + paths + ypath
       topath = testpath() + paths + topath

       t.Assert(gfile.Rename(ypath, topath), nil)
       t.Assert(gfile.IsFile(topath), true)

       t.AssertNE(gfile.Rename("", ""), nil)

    })

}

func Test_DirNames(t *testing.T) {
    gtest.C(t, func(t *gtest.T) {
       var (
          paths    string = "/testdirs"
          err      error
          readlist []string
       )
       havelist := []string{
          "t1.txt",
          "t2.txt",
       }

       // 创建测试文件
       createDir(paths)
       for _, v := range havelist {
          createTestFile(paths+"/"+v, "")
       }
       defer delTestFiles(paths)

       readlist, err = gfile.DirNames(testpath() + paths)

       t.AssertNil(err)
       t.AssertIN(readlist, havelist)

       _, err = gfile.DirNames("")
       t.AssertNE(err, nil)

    })
}

func Test_Glob(t *testing.T) {
    gtest.C(t, func(t *gtest.T) {
       var (
          paths      string = "/testfiles/*.txt"
          dirpath    string = "/testfiles"
          err        error
          resultlist []string
       )

       havelist1 := []string{
          "t1.txt",
          "t2.txt",
       }

       havelist2 := []string{
          testpath() + "/testfiles/t1.txt",
          testpath() + "/testfiles/t2.txt",
       }

       // ===============================构建测试文件
       createDir(dirpath)
       for _, v := range havelist1 {
          createTestFile(dirpath+"/"+v, "")
       }
       defer delTestFiles(dirpath)

       resultlist, err = gfile.Glob(testpath()+paths, true)
       t.AssertNil(err)
       t.Assert(resultlist, havelist1)

       resultlist, err = gfile.Glob(testpath()+paths, false)

       t.AssertNil(err)
       t.Assert(formatpaths(resultlist), formatpaths(havelist2))

       _, err = gfile.Glob("", true)
       t.AssertNil(err)

    })
}

func Test_Remove(t *testing.T) {
    gtest.C(t, func(t *gtest.T) {
       var (
          paths string = "/testfile_t1.txt"
       )
       createTestFile(paths, "")
       t.Assert(gfile.Remove(testpath()+paths), nil)

       t.Assert(gfile.Remove(""), nil)

       defer delTestFiles(paths)

    })
}

func Test_IsReadable(t *testing.T) {
    gtest.C(t, func(t *gtest.T) {
       var (
          paths1 string = "/testfile_GetContents.txt"
          paths2 string = "./testfile_GetContents_no.txt"
       )

       createTestFile(paths1, "")
       defer delTestFiles(paths1)

       t.Assert(gfile.IsReadable(testpath()+paths1), true)
       t.Assert(gfile.IsReadable(paths2), false)

    })
}

func Test_IsWritable(t *testing.T) {
    gtest.C(t, func(t *gtest.T) {
       var (
          paths1 string = "/testfile_GetContents.txt"
          paths2 string = "./testfile_GetContents_no.txt"
       )

       createTestFile(paths1, "")
       defer delTestFiles(paths1)
       t.Assert(gfile.IsWritable(testpath()+paths1), true)
       t.Assert(gfile.IsWritable(paths2), false)

    })
}

func Test_Chmod(t *testing.T) {
    gtest.C(t, func(t *gtest.T) {
       var (
          paths1 string = "/testfile_GetContents.txt"
          paths2 string = "./testfile_GetContents_no.txt"
       )
       createTestFile(paths1, "")
       defer delTestFiles(paths1)

       t.Assert(gfile.Chmod(testpath()+paths1, 0777), nil)
       t.AssertNE(gfile.Chmod(paths2, 0777), nil)

    })
}

// 获取绝对目录地址
func Test_RealPath(t *testing.T) {
    gtest.C(t, func(t *gtest.T) {
       var (
          paths1    string = "/testfile_files"
          readlPath string

          tempstr string
       )

       createDir(paths1)
       defer delTestFiles(paths1)

       readlPath = gfile.RealPath("./")

       tempstr, _ = filepath.Abs("./")

       t.Assert(readlPath, tempstr)

       t.Assert(gfile.RealPath("./nodirs"), "")

    })
}

// 获取当前执行文件的目录
func Test_SelfPath(t *testing.T) {
    gtest.C(t, func(t *gtest.T) {
       var (
          paths1    string
          readlPath string
          tempstr   string
       )
       readlPath = gfile.SelfPath()
       readlPath = filepath.ToSlash(readlPath)

       tempstr, _ = filepath.Abs(os.Args[0])
       paths1 = filepath.ToSlash(tempstr)
       paths1 = strings.Replace(paths1, "./", "/", 1)

       t.Assert(readlPath, paths1)

    })
}

func Test_SelfDir(t *testing.T) {
    gtest.C(t, func(t *gtest.T) {
       var (
          paths1    string
          readlPath string
          tempstr   string
       )
       readlPath = gfile.SelfDir()

       tempstr, _ = filepath.Abs(os.Args[0])
       paths1 = filepath.Dir(tempstr)

       t.Assert(readlPath, paths1)

    })
}

func Test_Basename(t *testing.T) {
    gtest.C(t, func(t *gtest.T) {
       var (
          paths1    string = "/testfilerr_GetContents.txt"
          readlPath string
       )

       createTestFile(paths1, "")
       defer delTestFiles(paths1)

       readlPath = gfile.Basename(testpath() + paths1)
       t.Assert(readlPath, "testfilerr_GetContents.txt")

    })
}

func Test_Dir(t *testing.T) {
    gtest.C(t, func(t *gtest.T) {
       var (
          paths1    string = "/testfiless"
          readlPath string
       )
       createDir(paths1)
       defer delTestFiles(paths1)

       readlPath = gfile.Dir(testpath() + paths1)

       t.Assert(readlPath, testpath())

       t.Assert(len(gfile.Dir(".")) > 0, true)
    })
}

func Test_Ext(t *testing.T) {
    gtest.C(t, func(t *gtest.T) {
       var (
          paths1   string = "/testfile_GetContents.txt"
          dirpath1        = "/testdirs"
       )
       createTestFile(paths1, "")
       defer delTestFiles(paths1)

       createDir(dirpath1)
       defer delTestFiles(dirpath1)

       t.Assert(gfile.Ext(testpath()+paths1), ".txt")
       t.Assert(gfile.Ext(testpath()+dirpath1), "")
    })

    gtest.C(t, func(t *gtest.T) {
       t.Assert(gfile.Ext("/var/www/test.js"), ".js")
       t.Assert(gfile.Ext("/var/www/test.min.js"), ".js")
       t.Assert(gfile.Ext("/var/www/test.js?1"), ".js")
       t.Assert(gfile.Ext("/var/www/test.min.js?v1"), ".js")
    })
}

func Test_ExtName(t *testing.T) {
    gtest.C(t, func(t *gtest.T) {
       t.Assert(gfile.ExtName("/var/www/test.js"), "js")
       t.Assert(gfile.ExtName("/var/www/test.min.js"), "js")
       t.Assert(gfile.ExtName("/var/www/test.js?v=1"), "js")
       t.Assert(gfile.ExtName("/var/www/test.min.js?v=1"), "js")
    })
}

func Test_TempDir(t *testing.T) {
    gtest.C(t, func(t *gtest.T) {
       t.Assert(gfile.Temp(), os.TempDir())
    })
}

func Test_Mkdir(t *testing.T) {
    gtest.C(t, func(t *gtest.T) {
       var (
          tpath string = "/testfile/createdir"
          err   error
       )

       defer delTestFiles("/testfile")

       err = gfile.Mkdir(testpath() + tpath)
       t.AssertNil(err)

       err = gfile.Mkdir("")
       t.AssertNE(err, nil)

       err = gfile.Mkdir(testpath() + tpath + "2/t1")
       t.AssertNil(err)

    })
}

func Test_Stat(t *testing.T) {
    gtest.C(t, func(t *gtest.T) {
       var (
          tpath1   = "/testfile_t1.txt"
          tpath2   = "./testfile_t1_no.txt"
          err      error
          fileiofo os.FileInfo
       )

       createTestFile(tpath1, "a")
       defer delTestFiles(tpath1)

       fileiofo, err = gfile.Stat(testpath() + tpath1)
       t.AssertNil(err)

       t.Assert(fileiofo.Size(), 1)

       _, err = gfile.Stat(tpath2)
       t.AssertNE(err, nil)

    })
}

func Test_MainPkgPath(t *testing.T) {
    gtest.C(t, func(t *gtest.T) {
       reads := gfile.MainPkgPath()
       t.Assert(reads, "")
    })
}

func Test_SelfName(t *testing.T) {
    gtest.C(t, func(t *gtest.T) {
       t.Assert(len(gfile.SelfName()) > 0, true)
    })
}

func Test_MTimestamp(t *testing.T) {
    gtest.C(t, func(t *gtest.T) {
       t.Assert(gfile.MTimestamp(gfile.Temp()) > 0, true)
    })
}
  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

leijmdas

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值