add zip unzip extract archive

This commit is contained in:
Star 2026-03-22 22:15:42 +08:00
parent 0b250f2c4b
commit e49187a1fa
3 changed files with 165 additions and 113 deletions

20
go.mod
View File

@ -1,14 +1,14 @@
module apigo.cc/gojs/util
go 1.24.0
go 1.25.0
require (
apigo.cc/gojs v0.0.32
apigo.cc/gojs v0.0.34
apigo.cc/gojs/console v0.0.4
github.com/ZZMarquis/gm v1.3.2
github.com/emmansun/gmsm v0.40.0
github.com/emmansun/gmsm v0.41.1
github.com/obscuren/ecies v0.0.0-20150213224233-7c0f4a9b18d9
github.com/ssgo/u v1.7.23
github.com/ssgo/u v1.7.24
gopkg.in/yaml.v3 v3.0.1
)
@ -16,12 +16,12 @@ require (
github.com/dlclark/regexp2 v1.11.5 // indirect
github.com/fsnotify/fsnotify v1.9.0 // indirect
github.com/go-sourcemap/sourcemap v2.1.4+incompatible // indirect
github.com/google/pprof v0.0.0-20250903194437-c28834ac2320 // indirect
github.com/google/pprof v0.0.0-20260302011040-a15ffb7f9dcc // indirect
github.com/ssgo/config v1.7.10 // indirect
github.com/ssgo/log v1.7.10 // indirect
github.com/ssgo/standard v1.7.7 // indirect
github.com/ssgo/log v1.7.11 // indirect
github.com/ssgo/standard v1.7.8 // indirect
github.com/ssgo/tool v0.4.29 // indirect
golang.org/x/crypto v0.46.0 // indirect
golang.org/x/sys v0.39.0 // indirect
golang.org/x/text v0.32.0 // indirect
golang.org/x/crypto v0.48.0 // indirect
golang.org/x/sys v0.42.0 // indirect
golang.org/x/text v0.35.0 // indirect
)

44
util.go
View File

@ -701,6 +701,50 @@ func init() {
return nil
}
},
"zip": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm).Check(1)
if r, err := u.Zip(u.Bytes(args.Arguments[0].Export())); err == nil {
return vm.ToValue(r)
} else {
// panic(vm.NewGoError(err))
vm.SetData("_lastError", err)
gojs.GetLogger(vm).Error(err.Error())
return nil
}
},
"unzip": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm).Check(1)
if r, err := u.Unzip(u.Bytes(args.Arguments[0].Export())); err == nil {
return vm.ToValue(r)
} else {
// panic(vm.NewGoError(err))
vm.SetData("_lastError", err)
gojs.GetLogger(vm).Error(err.Error())
return nil
}
},
"extract": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm).Check(2)
if err := u.Extract(args.Str(0), args.Str(1), args.Bool(2)); err == nil {
return vm.ToValue(true)
} else {
// panic(vm.NewGoError(err))
vm.SetData("_lastError", err)
gojs.GetLogger(vm).Error(err.Error())
return vm.ToValue(false)
}
},
"archive": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm).Check(2)
if err := u.Archive(args.Str(0), args.Str(1)); err == nil {
return vm.ToValue(true)
} else {
// panic(vm.NewGoError(err))
vm.SetData("_lastError", err)
gojs.GetLogger(vm).Error(err.Error())
return vm.ToValue(false)
}
},
"uniqueId": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm)
size := args.Int(0)

View File

@ -42,6 +42,10 @@ export default {
decryptRSA,
gzip,
gunzip,
zip,
unzip,
extract,
archive,
id,
idL,
uniqueId,
@ -147,6 +151,10 @@ function encryptRSA(data: any, pubKey: any): any { return '' }
function decryptRSA(data: any, priKey: any): any { return '' }
function gzip(data: any): string { return '' }
function gunzip(data: string): any { return null }
function zip(data: any): string { return '' }
function unzip(data: string): any { return null }
function extract(from: string, to: string, stripRoot?: boolean): boolean { return false }
function archive(from: string, to: string): boolean { return false }
function id(size?: number): string { return '' }
function idL(size?: number): string { return '' }
function uniqueId(size?: number): string { return '' }