-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzip.go
More file actions
125 lines (106 loc) · 2.68 KB
/
zip.go
File metadata and controls
125 lines (106 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package archives
import (
"archive/zip"
"bytes"
"fmt"
"io"
"strings"
)
type zipReader struct {
data []byte
reader *zip.Reader
}
func openZip(content io.Reader) (*zipReader, error) {
// Read entire content into memory
data, err := io.ReadAll(content)
if err != nil {
return nil, fmt.Errorf("reading zip content: %w", err)
}
// Create zip reader from bytes
reader, err := zip.NewReader(bytes.NewReader(data), int64(len(data)))
if err != nil {
return nil, fmt.Errorf("opening zip: %w", err)
}
return &zipReader{
data: data,
reader: reader,
}, nil
}
func (z *zipReader) List() ([]FileInfo, error) {
files := make([]FileInfo, 0, len(z.reader.File))
for _, f := range z.reader.File {
files = append(files, fileInfoFromZip(f))
}
return files, nil
}
func (z *zipReader) ListDir(dirPath string) ([]FileInfo, error) {
dirPath = normalizeDir(dirPath)
var files []FileInfo
// Track directories we've seen to avoid duplicates
seenDirs := make(map[string]bool)
for _, f := range z.reader.File {
path := f.Name
// Check if this file/dir is directly in the requested directory
if isInDir(path, dirPath) {
if f.FileInfo().IsDir() {
seenDirs[path] = true
}
files = append(files, fileInfoFromZip(f))
continue
}
// Check if we should add a subdirectory entry
if dirPath == "" || strings.HasPrefix(path, dirPath) {
rel := strings.TrimPrefix(path, dirPath)
parts := strings.Split(strings.TrimSuffix(rel, "/"), "/")
if len(parts) > 1 {
// This file is in a subdirectory
subdir := dirPath + parts[0] + "/"
if !seenDirs[subdir] {
seenDirs[subdir] = true
files = append(files, FileInfo{
Path: subdir,
Name: parts[0],
IsDir: true,
})
}
}
}
}
return files, nil
}
func (z *zipReader) Extract(filePath string) (io.ReadCloser, error) {
// Find the file
for _, f := range z.reader.File {
if f.Name == filePath {
if f.FileInfo().IsDir() {
return nil, fmt.Errorf("path is a directory: %s", filePath)
}
return f.Open()
}
}
return nil, fmt.Errorf("file not found: %s", filePath)
}
func (z *zipReader) Close() error {
// No resources to clean up for zip reader
z.data = nil
z.reader = nil
return nil
}
func fileInfoFromZip(f *zip.File) FileInfo {
return FileInfo{
Path: f.Name,
Name: extractName(f.Name),
Size: int64(f.UncompressedSize64),
CompressedSize: int64(f.CompressedSize64),
ModTime: f.Modified,
IsDir: f.FileInfo().IsDir(),
Mode: uint32(f.Mode()),
}
}
func extractName(path string) string {
path = strings.TrimSuffix(path, "/")
if idx := strings.LastIndex(path, "/"); idx >= 0 {
return path[idx+1:]
}
return path
}