Skip to content

Commit

Permalink
Feat: Finished test
Browse files Browse the repository at this point in the history
  • Loading branch information
YuMao233 committed Mar 30, 2024
1 parent a36a484 commit 51803e2
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 78 deletions.
70 changes: 70 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
name: Build

on:
push:
pull_request:

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.19

- name: Build
run: |
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -trimpath -ldflags '-s -w --extldflags "-static -fpic"' -o pty_windows_x64.exe
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -trimpath -ldflags '-s -w --extldflags "-static -fpic"' -o pty_linux_x64
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -trimpath -ldflags '-s -w --extldflags "-static -fpic"' -o pty_linux_arm64
CGO_ENABLED=0 GOOS=linux GOARCH=arm go build -trimpath -ldflags '-s -w --extldflags "-static -fpic"' -o pty_linux_arm
CGO_ENABLED=0 GOOS=linux GOARCH=386 go build -trimpath -ldflags '-s -w --extldflags "-static -fpic"' -o pty_linux_386
CGO_ENABLED=0 GOOS=linux GOARCH=mips go build -trimpath -ldflags '-s -w --extldflags "-static -fpic"' -o pty_linux_mips
CGO_ENABLED=0 GOOS=linux GOARCH=mipsle go build -trimpath -ldflags '-s -w --extldflags "-static -fpic"' -o pty_linux_mipsle
CGO_ENABLED=0 GOOS=linux GOARCH=ppc64 go build -trimpath -ldflags '-s -w --extldflags "-static -fpic"' -o pty_linux_ppc64
CGO_ENABLED=0 GOOS=linux GOARCH=riscv64 go build -trimpath -ldflags '-s -w --extldflags "-static -fpic"' -o pty_linux_riscv64
CGO_ENABLED=0 GOOS=linux GOARCH=s390x go build -trimpath -ldflags '-s -w --extldflags "-static -fpic"' -o pty_linux_s390x
CGO_ENABLED=0 GOOS=netbsd GOARCH=amd64 go build -trimpath -ldflags '-s -w --extldflags "-static -fpic"' -o pty_netbsd_x64
CGO_ENABLED=0 GOOS=netbsd GOARCH=arm go build -trimpath -ldflags '-s -w --extldflags "-static -fpic"' -o pty_netbsd_arm
CGO_ENABLED=0 GOOS=netbsd GOARCH=arm64 go build -trimpath -ldflags '-s -w --extldflags "-static -fpic"' -o pty_netbsd_arm64
CGO_ENABLED=0 GOOS=openbsd GOARCH=386 go build -trimpath -ldflags '-s -w --extldflags "-static -fpic"' -o pty_openbsd_386
CGO_ENABLED=0 GOOS=openbsd GOARCH=amd64 go build -trimpath -ldflags '-s -w --extldflags "-static -fpic"' -o pty_openbsd_x64
CGO_ENABLED=0 GOOS=openbsd GOARCH=arm go build -trimpath -ldflags '-s -w --extldflags "-static -fpic"' -o pty_openbsd_arm
CGO_ENABLED=0 GOOS=openbsd GOARCH=arm64 go build -trimpath -ldflags '-s -w --extldflags "-static -fpic"' -o pty_openbsd_arm64
CGO_ENABLED=0 GOOS=freebsd GOARCH=386 go build -trimpath -ldflags '-s -w --extldflags "-static -fpic"' -o pty_freebsd_386
CGO_ENABLED=0 GOOS=freebsd GOARCH=amd64 go build -trimpath -ldflags '-s -w --extldflags "-static -fpic"' -o pty_freebsd_x64
CGO_ENABLED=0 GOOS=freebsd GOARCH=arm go build -trimpath -ldflags '-s -w --extldflags "-static -fpic"' -o pty_freebsd_arm
CGO_ENABLED=0 GOOS=freebsd GOARCH=arm64 go build -trimpath -ldflags '-s -w --extldflags "-static -fpic"' -o pty_freebsd_arm64
- uses: "marvinpinto/action-automatic-releases@latest"
with:
repo_token: "${{ secrets.GITHUB_TOKEN }}"
automatic_release_tag: "latest"
title: Development Build
prerelease: true
files: |
pty_windows_x64.exe
pty_linux_x64
pty_linux_arm64
pty_linux_arm
pty_linux_386
pty_linux_mips
pty_linux_mipsle
pty_linux_ppc64
pty_linux_riscv64
pty_linux_s390x
pty_netbsd_x64
pty_netbsd_arm
pty_netbsd_arm64
pty_openbsd_386
pty_openbsd_x64
pty_openbsd_arm
pty_openbsd_arm64
pty_freebsd_386
pty_freebsd_x64
pty_freebsd_arm
pty_freebsd_arm64
104 changes: 26 additions & 78 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"os"
"path/filepath"

"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"
)

Expand Down Expand Up @@ -47,35 +46,36 @@ func init() {
}

func main() {
flag.Parse() // 解析参数

fmt.Println("PPPP:", zipPath, distDirPath, mode, srcFiles)

flag.Parse()
if mode == UNZIP_MODE {
fmt.Println("UNZIP:", zipPath, "-->", distDirPath)
UnZip(distDirPath, zipPath)
fmt.Println("UNZIP:", zipPath, "-->", distDirPath, "code:", encode)
err := UnZip(distDirPath, zipPath)
if err != nil {
fmt.Println(err)
os.Exit(-3)
}
return
}
if mode == ZIP_MODE {
fmt.Println("ZIP:", srcFiles, "-->"+zipPath)
Zip(srcFiles, zipPath)
fmt.Println("ZIP:", srcFiles, "-->", zipPath, "code:", encode)
err := Zip(srcFiles, zipPath)
if err != nil {
fmt.Println(err)
os.Exit(-2)
}
return
}

fmt.Println("ERR Params!")
fmt.Println("Err Mode Params!")
os.Exit(-1)
}

// 压缩文件
// files 文件数组,可以是不同dir下的文件或者文件夹
// dest 压缩文件存放地址
func Zip(srcFiles []string, dest string) error {
var files []*os.File
for _, fileName := range srcFiles {
file, err := os.Open(fileName)
if err != nil {
log.Fatalln(err)
os.Exit(-4)
return err
}
files = append(files, file)
}
Expand All @@ -92,96 +92,44 @@ func Zip(srcFiles []string, dest string) error {
return nil
}

func fileToZipWriter(file *os.File, prefix string, zw *zip.Writer) error {
info, err := file.Stat()
func UnZip(distDirPath, zipPath string) error {
zipFile, err := zip.OpenReader(zipPath)
if err != nil {
return err
}
if info.IsDir() {
if prefix == "" {
prefix = info.Name()
} else {
prefix = prefix + "/" + info.Name()
}

fileInfos, err := file.Readdir(-1)
if err != nil {
return err
}
for _, fi := range fileInfos {
f, err := os.Open(file.Name() + "/" + fi.Name())
if err != nil {
return err
}
err = fileToZipWriter(f, prefix, zw)
if err != nil {
return err
}
}
} else {
header, err := zip.FileInfoHeader(info)
if prefix != "" {
header.Name = prefix + "/" + header.Name
}
if err != nil {
return err
}
writer, err := zw.CreateHeader(header)
if err != nil {
return err
}
_, err = io.Copy(writer, file)
file.Close()
if err != nil {
return err
}
}
return nil
}

func UnZip(distDirPath, zipPath string) {
zipFile, err := zip.OpenReader(zipPath)
if err != nil {
panic(err)
}
defer zipFile.Close()

prefix := ""
if distDirPath != "" && distDirPath != "." {
prefix = distDirPath + "/"
}

// 第二步,遍历 zip 中的文件
for _, f := range zipFile.File {
filePath := f.Name
if encode == "gbk" {
i := bytes.NewReader([]byte(f.Name))
decoder := transform.NewReader(i, simplifiedchinese.GB18030.NewDecoder())
content, _ := ioutil.ReadAll(decoder)
filePath = string(content)
}
nameReader := bytes.NewReader([]byte(f.Name))
decoder := transform.NewReader(nameReader, getDecoderByCoder(encode))
content, _ := ioutil.ReadAll(decoder)
filePath := string(content)
if f.FileInfo().IsDir() {
_ = os.MkdirAll(prefix+filePath, os.ModePerm)
continue
}
// 创建对应文件夹
if err := os.MkdirAll(prefix+filepath.Dir(filePath), os.ModePerm); err != nil {
panic(err)
return err
}
// 解压到的目标文件
dstFile, err := os.OpenFile(prefix+filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
panic(err)
return err
}
file, err := f.Open()
if err != nil {
panic(err)
return err
}
// 写入到解压到的目标文件
if _, err := io.Copy(dstFile, file); err != nil {
panic(err)
return err
}
dstFile.Close()
file.Close()
}
return nil
}
80 changes: 80 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package main

import (
"archive/zip"
"io"
"os"
"strings"

"golang.org/x/text/encoding"
"golang.org/x/text/encoding/japanese"
"golang.org/x/text/encoding/korean"
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/encoding/traditionalchinese"
"golang.org/x/text/encoding/unicode"
)

func getDecoderByCoder(code string) *encoding.Decoder {
code = strings.ToLower(code)
var decoder *encoding.Decoder
switch code {
case "gbk":
decoder = simplifiedchinese.GBK.NewDecoder()
case "big5":
decoder = traditionalchinese.Big5.NewDecoder()
case "shift_jis":
decoder = japanese.ShiftJIS.NewDecoder()
case "euckr":
decoder = korean.EUCKR.NewDecoder()
default:
decoder = unicode.UTF8.NewDecoder()
}
return decoder
}

func fileToZipWriter(file *os.File, prefix string, zw *zip.Writer) error {
info, err := file.Stat()
if err != nil {
return err
}
if info.IsDir() {
if prefix == "" {
prefix = info.Name()
} else {
prefix = prefix + "/" + info.Name()
}

fileInfos, err := file.Readdir(-1)
if err != nil {
return err
}
for _, fi := range fileInfos {
f, err := os.Open(file.Name() + "/" + fi.Name())
if err != nil {
return err
}
err = fileToZipWriter(f, prefix, zw)
if err != nil {
return err
}
}
} else {
header, err := zip.FileInfoHeader(info)
if prefix != "" {
header.Name = prefix + "/" + header.Name
}
if err != nil {
return err
}
writer, err := zw.CreateHeader(header)
if err != nil {
return err
}
_, err = io.Copy(writer, file)
file.Close()
if err != nil {
return err
}
}
return nil
}

0 comments on commit 51803e2

Please sign in to comment.