Digby/main.go

75 lines
1.8 KiB
Go
Raw Permalink Normal View History

2021-01-06 23:45:43 +00:00
/*
Digby: A multi-file copmressor
2021-01-07 16:13:31 +00:00
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.
2021-01-06 23:45:43 +00:00
*/
package main
import (
"bufio"
"compress/gzip"
"compress/zlib"
"flag"
2021-01-06 23:45:43 +00:00
"fmt"
"io/ioutil"
"os"
// Third-party packages
2021-01-07 16:13:31 +00:00
"github.com/DataDog/zstd" // zstd support
"github.com/itchio/lzma" // lzma support
2021-01-06 23:45:43 +00:00
)
func main() {
// -file argument, required.
2021-01-06 23:45:43 +00:00
var file string
flag.StringVar(&file, "file", "", "File to compress. (Required)")
2021-01-06 23:45:43 +00:00
// -path argument, optional
2021-01-06 23:45:43 +00:00
var path string
flag.StringVar(&path, "path", "", "Path to save the file. (Optional)")
2021-01-06 23:45:43 +00:00
// -comp argument, required.
2021-01-06 23:45:43 +00:00
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()
2021-01-06 23:45:43 +00:00
// 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()
2021-01-07 16:13:31 +00:00
} else if *&comp == "lzma" {
f, _ = os.Create(*&path + *&file + ".lzma")
w := lzma.NewWriter(f)
w.Write(data)
w.Close()
2021-01-06 23:45:43 +00:00
}
fmt.Println("Successfully compressed ", *&file, " using ", *&comp)
2021-01-06 23:45:43 +00:00
}