-
-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(module): support archiver module
feat: support 7z format
- Loading branch information
Showing
8 changed files
with
452 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Archiver Library | ||
|
||
`vfox` provides a decompression tool that supports `tar.gz`, `tgz`, `tar.xz`, `zip`, and `7z`. In Lua scripts, you can | ||
use `require("vfox.archiver")` to access it. | ||
|
||
**Usage** | ||
|
||
```shell | ||
local archiver = require("vfox.archiver") | ||
local err = archiver.decompress("testdata/test.zip", "testdata/test") | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Archiver 标准库 | ||
|
||
`vfox` 提供了解压工具, 支持`tar.gz`、`tgz`、`tar.xz`、`zip`、`7z`。在Lua脚本中,你可以使用`require("vfox.archiver")`来访问它。 | ||
例如: | ||
|
||
**Usage** | ||
```shell | ||
local archiver = require("vfox.archiver") | ||
local err = archiver.decompress("testdata/test.zip", "testdata/test") | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package string | ||
|
||
import ( | ||
"github.com/version-fox/vfox/internal/util" | ||
lua "github.com/yuin/gopher-lua" | ||
) | ||
|
||
// Preload adds strings to the given Lua state's package.preload table. After it | ||
// has been preloaded, it can be loaded using require: | ||
// | ||
// local strings = require("vfox.archiver") | ||
func Preload(L *lua.LState) { | ||
L.PreloadModule("vfox.archiver", Loader) | ||
} | ||
|
||
// Loader is the module loader function. | ||
func Loader(L *lua.LState) int { | ||
t := L.NewTable() | ||
L.SetFuncs(t, api) | ||
L.Push(t) | ||
return 1 | ||
} | ||
|
||
var api = map[string]lua.LGFunction{ | ||
"decompress": decompress, | ||
} | ||
|
||
// decompress lua archiver.decompress(sourceFile, targetPath): port of go string.decompress() returns error | ||
func decompress(L *lua.LState) int { | ||
archiverPath := L.CheckString(1) | ||
targetPath := L.CheckString(2) | ||
|
||
err := util.NewDecompressor(archiverPath).Decompress(targetPath) | ||
if err != nil { | ||
L.Push(lua.LString(err.Error())) | ||
return 1 | ||
} else { | ||
L.Push(lua.LNil) | ||
} | ||
return 1 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package string | ||
|
||
import ( | ||
lua "github.com/yuin/gopher-lua" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestDecompress(t *testing.T) { | ||
const str = ` | ||
local archiver = require("vfox.archiver") | ||
local err = archiver.decompress("testdata/test.zip", "testdata/test") | ||
assert(err == nil, "strings.decompress()") | ||
local f = io.open("testdata/test/test.txt", "r") | ||
if f then | ||
f:close() | ||
else | ||
error("file not found") | ||
end | ||
` | ||
defer func() { | ||
_ = os.RemoveAll("testdata/test") | ||
}() | ||
eval(str, t) | ||
} | ||
|
||
func eval(str string, t *testing.T) { | ||
s := lua.NewState() | ||
defer s.Close() | ||
|
||
Preload(s) | ||
if err := s.DoString(str); err != nil { | ||
t.Error(err) | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters