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