/* Digby: A multi-file copmressor There are some drawbacks to this application. 1) You can't compress multiple files or a folder at this moment. 2) It puts the files into memory, which is bad if the file you want to compress is bigger than the memory you have. 3) There's no support for using the `*` modifier for compressing all files in working directory. */ package main import ( "bufio" "compress/gzip" "compress/zlib" "flag" "fmt" "io/ioutil" "os" // Third-party packages "github.com/DataDog/zstd" // zstd support "github.com/itchio/lzma" // lzma support ) func main() { // -file argument, required. var file string flag.StringVar(&file, "file", "", "File to compress. (Required)") // -path argument, optional var path string flag.StringVar(&path, "path", "", "Path to save the file. (Optional)") // -comp argument, required. var comp string flag.StringVar(&comp, "comp", "", "Compression Algorithm. (Required)") // -lvl argument, optional (only used for zstd.) var lvl int flag.Int("lvl", *&lvl, "Level of compression. (Optional)") flag.Parse() // Compression shit f, _ := os.Open(*&path + *&file) read := bufio.NewReader(f) data, _ := ioutil.ReadAll(read) if *&comp == "gzip" { f, _ = os.Create(*&path + *&file + ".gz") w := gzip.NewWriter(f) w.Write(data) w.Close() } else if *&comp == "zlib" { f, _ = os.Create(*&path + *&file + ".zlib") w := zlib.NewWriter(f) w.Write(data) w.Close() } else if *&comp == "zstd" { f, _ = os.Create(*&path + *&file + ".zst") w := zstd.NewWriterLevel(f, *&lvl) w.Write(data) w.Close() } else if *&comp == "lzma" { f, _ = os.Create(*&path + *&file + ".lzma") w := lzma.NewWriter(f) w.Write(data) w.Close() } fmt.Println("Successfully compressed ", *&file, " using ", *&comp) }