|
| 1 | +// Package files provides utilities for file operations, archive handling, |
| 2 | +// and path manipulation. |
| 3 | +// |
| 4 | +// The package offers comprehensive support for working with various archive |
| 5 | +// formats (tar, zip, gzip, xz), file path validation, glob pattern matching, |
| 6 | +// and common file operations. |
| 7 | +// |
| 8 | +// Key Features: |
| 9 | +// - Archive extraction with detailed results and statistics |
| 10 | +// - Support for tar, tar.gz, tar.xz, and zip formats |
| 11 | +// - Path traversal vulnerability protection |
| 12 | +// - Glob pattern matching with doublestar support |
| 13 | +// - File compression and decompression |
| 14 | +// - Safe file operations with validation |
| 15 | +// |
| 16 | +// Archive Extraction: |
| 17 | +// |
| 18 | +// // Simple extraction |
| 19 | +// err := files.UnarchiveSimple("package.tar.gz", "/dest") |
| 20 | +// |
| 21 | +// // Extraction with detailed results |
| 22 | +// result, err := files.Unarchive("package.tar.gz", "/dest") |
| 23 | +// fmt.Printf("Extracted %d files (%s)\n", len(result.Files), result.String()) |
| 24 | +// |
| 25 | +// // Extraction with options |
| 26 | +// result, err := files.Unarchive("archive.zip", "/dest", |
| 27 | +// files.WithOverwrite(true)) |
| 28 | +// |
| 29 | +// // Extract only executables |
| 30 | +// err := files.UnarchiveExecutables("binaries.tar.gz", "/usr/local/bin") |
| 31 | +// |
| 32 | +// Compression: |
| 33 | +// |
| 34 | +// // Gzip a file |
| 35 | +// data, err := files.GzipFile("document.txt") |
| 36 | +// |
| 37 | +// // Create a zip archive |
| 38 | +// err := files.Zip("/source/dir", "archive.zip") |
| 39 | +// |
| 40 | +// // Decompress xz |
| 41 | +// err := files.Unxz("file.xz", "file.txt") |
| 42 | +// |
| 43 | +// // Decompress gzip |
| 44 | +// err := files.Ungzip("file.gz", "file.txt") |
| 45 | +// |
| 46 | +// Path Operations: |
| 47 | +// |
| 48 | +// // Validate path (check for traversal attacks) |
| 49 | +// err := files.ValidatePath("../../etc/passwd") // Returns error |
| 50 | +// |
| 51 | +// // Check file type by extension |
| 52 | +// if files.IsValidPathType("config.yaml", ".yaml", ".yml") { |
| 53 | +// // Process YAML file |
| 54 | +// } |
| 55 | +// |
| 56 | +// // Get base name without extension |
| 57 | +// name := files.GetBaseName("config.yaml") // "config" |
| 58 | +// |
| 59 | +// Glob Matching: |
| 60 | +// |
| 61 | +// // Expand glob patterns |
| 62 | +// matches, err := files.UnfoldGlobs("**/*.go", "**/*.yaml") |
| 63 | +// |
| 64 | +// // Match with doublestar patterns |
| 65 | +// files, err := files.DoubleStarGlob("/project", []string{"**/*.go"}) |
| 66 | +// |
| 67 | +// File Operations: |
| 68 | +// |
| 69 | +// // Check if file exists |
| 70 | +// if files.Exists("/path/to/file") { |
| 71 | +// // File exists |
| 72 | +// } |
| 73 | +// |
| 74 | +// // Safe read (returns empty string on error) |
| 75 | +// content := files.SafeRead("/path/to/file") |
| 76 | +// |
| 77 | +// // Copy file |
| 78 | +// err := files.Copy("/source/file", "/dest/file") |
| 79 | +// |
| 80 | +// // Copy from reader with permissions |
| 81 | +// n, err := files.CopyFromReader(reader, "/dest/file", 0644) |
| 82 | +// |
| 83 | +// // Create temp file with custom name |
| 84 | +// tmpFile := files.TempFileName("prefix-", ".txt") |
| 85 | +// |
| 86 | +// Archive Results: |
| 87 | +// |
| 88 | +// The Archive type provides detailed information about extraction operations: |
| 89 | +// |
| 90 | +// result, _ := files.Unarchive("package.tar.gz", "/dest") |
| 91 | +// fmt.Printf("Files: %d\n", len(result.Files)) |
| 92 | +// fmt.Printf("Directories: %d\n", len(result.Directories)) |
| 93 | +// fmt.Printf("Symlinks: %d\n", len(result.Symlinks)) |
| 94 | +// fmt.Printf("Size: %s -> %s (%.1f%% compression)\n", |
| 95 | +// formatSize(result.CompressedSize), |
| 96 | +// formatSize(result.ExtractedSize), |
| 97 | +// result.CompressionRatio*100) |
| 98 | +// |
| 99 | +// Security: |
| 100 | +// |
| 101 | +// The package includes protection against path traversal vulnerabilities |
| 102 | +// when extracting archives. All paths are validated to ensure they don't |
| 103 | +// escape the destination directory. |
| 104 | +package files |
0 commit comments