Skip to content

Commit 35e3618

Browse files
author
武斌
committed
rollback github.com/mholt/archives to github.com/mholt/archiver/v3
1 parent d78adea commit 35e3618

File tree

4 files changed

+18
-289
lines changed

4 files changed

+18
-289
lines changed

fileutils/fileutils.go

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ package fileutils
22

33
import (
44
"bufio"
5-
"context"
65
"io"
76
"os"
87

9-
"github.com/mholt/archives"
8+
"github.com/mholt/archiver/v3"
109
"github.com/pkg/errors"
1110
)
1211

@@ -50,37 +49,5 @@ func LinesFromReader(r io.Reader) ([]string, error) {
5049
}
5150

5251
func Archive(output string, sources ...string) error {
53-
ctx := context.Background()
54-
55-
// 通过文件扩展名自动识别格式
56-
format, _, err := archives.Identify(ctx, output, nil)
57-
if err != nil {
58-
return err
59-
}
60-
61-
archiver, ok := format.(archives.Archiver)
62-
if !ok {
63-
return errors.New("不支持的归档格式")
64-
}
65-
66-
// 创建输出文件
67-
out, err := os.Create(output)
68-
if err != nil {
69-
return err
70-
}
71-
defer out.Close()
72-
73-
// 准备源文件映射和文件列表
74-
filesMap := make(map[string]string)
75-
for _, source := range sources {
76-
filesMap[source] = ""
77-
}
78-
79-
files, err := archives.FilesFromDisk(ctx, nil, filesMap)
80-
if err != nil {
81-
return err
82-
}
83-
84-
// 创建归档
85-
return archiver.Archive(ctx, out, files)
52+
return archiver.Archive(sources, output)
8653
}

fileutils/fileutils_test.go

Lines changed: 0 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
11
package fileutils
22

33
import (
4-
"context"
5-
"os"
6-
"path/filepath"
74
"testing"
85

9-
"github.com/mholt/archives"
10-
"github.com/stretchr/testify/assert"
11-
"github.com/stretchr/testify/require"
12-
136
"github.com/unionj-cloud/toolkit/pathutils"
147
)
158

@@ -47,90 +40,3 @@ func TestCreateDirectory(t *testing.T) {
4740
})
4841
}
4942
}
50-
51-
func TestArchive(t *testing.T) {
52-
// 创建临时目录
53-
tempDir, err := os.MkdirTemp("", "archive-test")
54-
require.NoError(t, err)
55-
defer os.RemoveAll(tempDir)
56-
57-
// 创建测试文件
58-
testFile1 := filepath.Join(tempDir, "test1.txt")
59-
err = os.WriteFile(testFile1, []byte("测试内容1"), 0644)
60-
require.NoError(t, err)
61-
62-
// 创建测试目录及其中的文件
63-
testSubDir := filepath.Join(tempDir, "subdir")
64-
err = os.Mkdir(testSubDir, 0755)
65-
require.NoError(t, err)
66-
67-
testFile2 := filepath.Join(testSubDir, "test2.txt")
68-
err = os.WriteFile(testFile2, []byte("测试内容2"), 0644)
69-
require.NoError(t, err)
70-
71-
tests := []struct {
72-
name string
73-
outputName string
74-
}{
75-
{
76-
name: "Zip归档",
77-
outputName: "archive.zip",
78-
},
79-
{
80-
name: "Tar归档",
81-
outputName: "archive.tar",
82-
},
83-
{
84-
name: "TarGz归档",
85-
outputName: "archive.tar.gz",
86-
},
87-
}
88-
89-
for _, tt := range tests {
90-
t.Run(tt.name, func(t *testing.T) {
91-
// 设置输出文件路径
92-
outputPath := filepath.Join(tempDir, tt.outputName)
93-
94-
// 调用Archive函数
95-
err := Archive(outputPath, testFile1, testSubDir)
96-
require.NoError(t, err)
97-
98-
// 验证归档文件存在
99-
_, err = os.Stat(outputPath)
100-
assert.NoError(t, err)
101-
102-
// 验证归档文件内容
103-
validateArchive(t, outputPath, []string{"test1.txt", "subdir/test2.txt"})
104-
})
105-
}
106-
}
107-
108-
// 验证归档文件内容
109-
func validateArchive(t *testing.T, archivePath string, expectedPaths []string) {
110-
// 打开归档文件
111-
f, err := os.Open(archivePath)
112-
require.NoError(t, err)
113-
defer f.Close()
114-
115-
// 识别归档格式
116-
format, reader, err := archives.Identify(context.Background(), archivePath, f)
117-
require.NoError(t, err)
118-
119-
extractor, ok := format.(archives.Extractor)
120-
require.True(t, ok, "格式无法用于提取")
121-
122-
// 提取文件并验证
123-
foundPaths := make(map[string]bool)
124-
err = extractor.Extract(context.Background(), reader, func(ctx context.Context, file archives.FileInfo) error {
125-
if !file.IsDir() {
126-
foundPaths[file.NameInArchive] = true
127-
}
128-
return nil
129-
})
130-
require.NoError(t, err)
131-
132-
// 检查是否找到所有期望的文件
133-
for _, expectedPath := range expectedPaths {
134-
assert.True(t, foundPaths[expectedPath], "在归档中未找到文件: %s", expectedPath)
135-
}
136-
}

go.mod

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ require (
3939
github.com/goccy/go-reflect v1.2.0
4040
github.com/hashicorp/go-sockaddr v1.0.2
4141
github.com/hazelcast/hazelcast-go-client v1.4.1
42-
github.com/mholt/archives v0.1.2
42+
github.com/mholt/archiver/v3 v3.5.1
4343
github.com/morkid/gocache v1.0.0
4444
github.com/patrickmn/go-cache v2.1.0+incompatible
4545
github.com/proullon/ramsql v0.1.4
@@ -58,11 +58,7 @@ require (
5858
require (
5959
filippo.io/edwards25519 v1.1.0 // indirect
6060
github.com/HdrHistogram/hdrhistogram-go v1.1.2 // indirect
61-
github.com/STARRY-S/zip v0.2.1 // indirect
6261
github.com/apache/thrift v0.16.0 // indirect
63-
github.com/bodgit/plumbing v1.3.0 // indirect
64-
github.com/bodgit/sevenzip v1.6.0 // indirect
65-
github.com/bodgit/windows v1.0.1 // indirect
6662
github.com/bytedance/sonic/loader v0.2.4 // indirect
6763
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
6864
github.com/certifi/gocertifi v0.0.0-20200922220541-2c3bb06c6054 // indirect
@@ -80,25 +76,21 @@ require (
8076
github.com/grpc-ecosystem/grpc-gateway v1.16.0 // indirect
8177
github.com/hashicorp/go-uuid v1.0.3 // indirect
8278
github.com/hashicorp/golang-lru v0.5.4 // indirect
83-
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
8479
github.com/jackc/puddle/v2 v2.2.1 // indirect
8580
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
8681
github.com/kr/pretty v0.3.1 // indirect
8782
github.com/kr/text v0.2.0 // indirect
8883
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
89-
github.com/minio/minlz v1.0.0 // indirect
90-
github.com/nwaples/rardecode/v2 v2.1.0 // indirect
84+
github.com/nwaples/rardecode v1.1.0 // indirect
9185
github.com/nxadm/tail v1.4.8 // indirect
9286
github.com/pegasus-kv/thrift v0.13.0 // indirect
9387
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
9488
github.com/rogpeppe/go-internal v1.11.0 // indirect
9589
github.com/shirou/gopsutil/v3 v3.23.12 // indirect
9690
github.com/shoenig/go-m1cpu v0.1.6 // indirect
97-
github.com/sorairolake/lzip-go v0.3.5 // indirect
98-
github.com/therootcompany/xz v1.0.1 // indirect
9991
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
92+
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
10093
go.uber.org/goleak v1.3.0 // indirect
101-
go4.org v0.0.0-20230225012048-214862532bf5 // indirect
10294
golang.org/x/arch v0.1.0 // indirect
10395
google.golang.org/genproto/googleapis/api v0.0.0-20240318140521-94a12d6c2237 // indirect
10496
google.golang.org/genproto/googleapis/rpc v0.0.0-20240318140521-94a12d6c2237 // indirect

0 commit comments

Comments
 (0)