fork/gdu

git clone https://git.julia.blue/fork/gdu

ShivamB25feat: customize JSON export attributes (#605)d062e88

ignore
4.6 KiB196 linesraw
1package analyze
2
3import (
4	"archive/zip"
5	"io"
6	"os"
7	"path/filepath"
8	"strings"
9	"time"
10
11	"github.com/dundee/gdu/v5/pkg/fs"
12)
13
14// ZipDir represents a directory structure inside a zip file
15type ZipDir struct {
16	*Dir
17	zipPath string // path to the original zip file
18}
19
20// ZipFile represents a file inside a zip archive
21type ZipFile struct {
22	*File
23	zipPath   string
24	inZipPath string // path inside the zip file
25}
26
27// GetPath returns the virtual path for zip file
28func (zf *ZipFile) GetPath() string {
29	return zf.zipPath + "/" + zf.inZipPath
30}
31
32// GetType returns type of zip file
33func (zf *ZipFile) GetType() string {
34	return "ZipFile"
35}
36
37// EncodeJSON encodes zip file to JSON
38func (zf *ZipFile) EncodeJSON(writer io.Writer, topLevel bool, attributes fs.JSONAttributes) error {
39	// Use the embedded File's EncodeJSON method
40	return zf.File.EncodeJSON(writer, topLevel, attributes)
41}
42
43// GetType returns type of zip directory
44func (zd *ZipDir) GetType() string {
45	return "ZipDirectory"
46}
47
48// IsDir returns true for ZipDir
49func (zd *ZipDir) IsDir() bool {
50	return true
51}
52
53// EncodeJSON encodes zip directory to JSON
54func (zd *ZipDir) EncodeJSON(writer io.Writer, topLevel bool, attributes fs.JSONAttributes) error {
55	// Use the embedded Dir's EncodeJSON method
56	return zd.Dir.EncodeJSON(writer, topLevel, attributes)
57}
58
59// GetPath returns the virtual path for zip directory
60func (zd *ZipDir) GetPath() string {
61	if zd.Parent != nil {
62		return filepath.Join(zd.Parent.GetPath(), zd.Name)
63	}
64	return zd.zipPath
65}
66
67// isZipFile checks if a file is a zip or jar file
68func isZipFile(filename string) bool {
69	ext := strings.ToLower(filepath.Ext(filename))
70	return ext == ".zip" || ext == ".jar"
71}
72
73// processZipFile processes a zip file and returns a ZipDir representing its contents
74func processZipFile(zipPath string, info os.FileInfo) (zipDir *ZipDir, err error) {
75	reader, err := zip.OpenReader(zipPath)
76	if err != nil {
77		return nil, err
78	}
79	defer reader.Close()
80
81	// Create root directory
82	zipDir = &ZipDir{
83		Dir: &Dir{
84			File: &File{
85				Name:  filepath.Base(zipPath),
86				Flag:  'Z', // Use 'Z' to identify zip files
87				Size:  info.Size(),
88				Usage: info.Size(),
89				Mtime: info.ModTime(),
90			},
91			ItemCount: 1,
92			Files:     make(fs.Files, 0),
93		},
94		zipPath: zipPath,
95	}
96
97	// Use map to store directory structure
98	dirMap := make(map[string]*ZipDir)
99	dirMap[""] = zipDir // root directory
100
101	for _, f := range reader.File {
102		if f.FileInfo().IsDir() {
103			continue // Skip directory entries, we'll create them automatically based on file paths
104		}
105
106		// Parse file path and ensure all parent directories exist
107		dirPath := filepath.Dir(f.Name)
108		if dirPath == "." {
109			dirPath = "" // root directory
110		}
111		ensureZipDirExists(dirMap, dirPath, zipPath, zipDir)
112
113		// Create file item
114		parentDir := dirMap[dirPath]
115		zipFile := &ZipFile{
116			File: &File{
117				Name:   filepath.Base(f.Name),
118				Flag:   ' ',
119				Size:   int64(f.UncompressedSize64),
120				Usage:  int64(f.CompressedSize64),
121				Mtime:  f.FileInfo().ModTime(),
122				Parent: parentDir,
123			},
124			zipPath:   zipPath,
125			inZipPath: f.Name,
126		}
127
128		parentDir.AddFile(zipFile)
129	}
130
131	return zipDir, nil
132}
133
134// ensureZipDirExists ensures all directories in the specified path exist
135func ensureZipDirExists(dirMap map[string]*ZipDir, path, zipPath string, rootDir *ZipDir) {
136	if path == "" || path == "." {
137		return
138	}
139
140	// If directory already exists, return directly
141	if _, exists := dirMap[path]; exists {
142		return
143	}
144
145	// Ensure parent directory exists
146	parentPath := filepath.Dir(path)
147	if parentPath != "." && parentPath != "" && parentPath != path {
148		ensureZipDirExists(dirMap, parentPath, zipPath, rootDir)
149	}
150
151	// Create current directory
152	var parent *ZipDir
153	if parentPath == "" || parentPath == "." || parentPath == path {
154		parent = rootDir
155	} else {
156		parent = dirMap[parentPath]
157	}
158
159	newDir := &ZipDir{
160		Dir: &Dir{
161			File: &File{
162				Name:   filepath.Base(path),
163				Flag:   'Z',
164				Size:   4096, // virtual directory size
165				Usage:  4096,
166				Mtime:  time.Now(),
167				Parent: parent,
168			},
169			ItemCount: 1,
170			Files:     make(fs.Files, 0),
171		},
172		zipPath: zipPath,
173	}
174
175	dirMap[path] = newDir
176	parent.AddFile(newDir)
177}
178
179// getZipFileSize gets the total uncompressed size of a zip file
180func getZipFileSize(zipPath string) (uncompressed, compressed int64, err error) {
181	reader, err := zip.OpenReader(zipPath)
182	if err != nil {
183		return 0, 0, err
184	}
185	defer reader.Close()
186
187	var uncompressedSize, compressedSize int64
188	for _, f := range reader.File {
189		if !f.FileInfo().IsDir() {
190			uncompressedSize += int64(f.UncompressedSize64)
191			compressedSize += int64(f.CompressedSize64)
192		}
193	}
194
195	return uncompressedSize, compressedSize, nil
196}