|
| 1 | +#!/usr/bin/env kscript |
| 2 | +@file:DependsOn("com.github.ajalt:clikt:3.2.0") // For command line parsing |
| 3 | + |
| 4 | +import com.github.ajalt.clikt.core.CliktCommand |
| 5 | +import com.github.ajalt.clikt.parameters.options.* |
| 6 | +import com.github.ajalt.clikt.parameters.types.long |
| 7 | +import com.github.ajalt.clikt.parameters.types.path |
| 8 | +import java.io.File |
| 9 | +import java.nio.file.Files |
| 10 | +import java.nio.file.Path |
| 11 | +import java.util.zip.ZipEntry |
| 12 | +import java.util.zip.ZipOutputStream |
| 13 | + |
| 14 | +class FindLargeFiles : CliktCommand(help = "Finds files larger than a specified size and optionally zips them.") { |
| 15 | + val directory: Path by option("-d", "--directory", help = "Directory to search (default: current directory)") |
| 16 | + .path(mustExist = true, canBeFile = false, mustBeReadable = true) |
| 17 | + .default(Path.of(".")) |
| 18 | + |
| 19 | + val minSize: Long by option("-s", "--size", help = "Minimum file size in MB") |
| 20 | + .long() |
| 21 | + .default(100L) |
| 22 | + |
| 23 | + val archivePath: Path? by option("-a", "--archive", help = "Optional ZIP file path to archive found files") |
| 24 | + .path(canBeDir = false) // Removed mustBeWritable as it causes issues if file doesn't exist for parent dir check |
| 25 | + |
| 26 | + val quiet: Boolean by option("-q", "--quiet", help = "Suppress listing of found files").flag(default = false) |
| 27 | + |
| 28 | + val force: Boolean by option("-f", "--force", help="Force overwrite of existing archive").flag(default = false) |
| 29 | + |
| 30 | + |
| 31 | + override fun run() { |
| 32 | + val minSizeBytes = minSize * 1024 * 1024 |
| 33 | + if (!quiet) { |
| 34 | + echo("Searching for files larger than $minSize MB ($minSizeBytes bytes) in directory: $directory") |
| 35 | + } |
| 36 | + |
| 37 | + val largeFiles = mutableListOf<File>() |
| 38 | + |
| 39 | + directory.toFile().walkTopDown().forEach { file -> |
| 40 | + if (file.isFile && file.length() >= minSizeBytes) { |
| 41 | + largeFiles.add(file) |
| 42 | + if (!quiet) { |
| 43 | + echo("Found: ${file.absolutePath} (${file.length() / (1024 * 1024)} MB)") |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + if (largeFiles.isEmpty()) { |
| 49 | + if (!quiet) { |
| 50 | + echo("No files found larger than $minSize MB.") |
| 51 | + } |
| 52 | + return |
| 53 | + } |
| 54 | + |
| 55 | + if (!quiet) { |
| 56 | + echo("\nFound ${largeFiles.size} file(s) larger than $minSize MB.") |
| 57 | + } |
| 58 | + |
| 59 | + archivePath?.let { zipPath -> |
| 60 | + if (Files.exists(zipPath) && !force) { |
| 61 | + // Simplified prompt for non-interactive, or use a different Clikt mechanism for actual prompting if needed |
| 62 | + val shouldOverwrite = System.getenv("KSCRIPT_OVERWRITE_ARCHIVE") == "true" // Example: control via env var |
| 63 | + if (!shouldOverwrite) { |
| 64 | + echo("Archive '$zipPath' exists. Overwrite not forced and not confirmed. Archiving cancelled.", err = true) |
| 65 | + return |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + if (!quiet) { |
| 70 | + echo("Archiving ${largeFiles.size} file(s) to $zipPath ...") |
| 71 | + } |
| 72 | + try { |
| 73 | + ZipOutputStream(Files.newOutputStream(zipPath)).use { zos -> |
| 74 | + largeFiles.forEach { file -> |
| 75 | + // Ensure relative paths for zip entries if directory is not current |
| 76 | + val entryPath = if (directory.toFile().absolutePath == Path.of(".").toFile().absolutePath) { |
| 77 | + file.toPath() // Use relative path if searching "." |
| 78 | + } else { |
| 79 | + directory.relativize(file.toPath()) |
| 80 | + } |
| 81 | + val zipEntry = ZipEntry(entryPath.toString()) |
| 82 | + zos.putNextEntry(zipEntry) |
| 83 | + Files.copy(file.toPath(), zos) |
| 84 | + zos.closeEntry() |
| 85 | + if (!quiet) { |
| 86 | + echo(" Added: ${file.name}") |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + if (!quiet) { |
| 91 | + echo("Archiving complete: $zipPath") |
| 92 | + } |
| 93 | + } catch (e: Exception) { |
| 94 | + echo("Error during archiving: ${e.message}", err = true) |
| 95 | + // e.printStackTrace() // for more detailed debug if necessary |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +fun main(args: Array<String>) = FindLargeFiles().main(args) |
0 commit comments