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"
|
2021-01-07 04:13:04 +00:00
|
|
|
"flag"
|
2021-01-06 23:45:43 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2021-01-07 02:33:35 +00:00
|
|
|
|
|
|
|
// 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() {
|
2021-01-07 04:13:04 +00:00
|
|
|
|
|
|
|
// -file argument, required.
|
2021-01-06 23:45:43 +00:00
|
|
|
var file string
|
2021-01-07 04:13:04 +00:00
|
|
|
flag.StringVar(&file, "file", "", "File to compress. (Required)")
|
2021-01-06 23:45:43 +00:00
|
|
|
|
2021-01-07 04:13:04 +00:00
|
|
|
// -path argument, optional
|
2021-01-06 23:45:43 +00:00
|
|
|
var path string
|
2021-01-07 04:13:04 +00:00
|
|
|
flag.StringVar(&path, "path", "", "Path to save the file. (Optional)")
|
2021-01-06 23:45:43 +00:00
|
|
|
|
2021-01-07 04:13:04 +00:00
|
|
|
// -comp argument, required.
|
2021-01-06 23:45:43 +00:00
|
|
|
var comp string
|
2021-01-07 04:13:04 +00:00
|
|
|
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()
|
2021-01-07 02:33:35 +00:00
|
|
|
} else if *&comp == "zstd" {
|
|
|
|
f, _ = os.Create(*&path + *&file + ".zst")
|
2021-01-07 04:13:04 +00:00
|
|
|
w := zstd.NewWriterLevel(f, *&lvl)
|
2021-01-07 02:33:35 +00:00
|
|
|
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
|
|
|
}
|
2021-01-07 04:13:04 +00:00
|
|
|
|
|
|
|
fmt.Println("Successfully compressed ", *&file, " using ", *&comp)
|
2021-01-06 23:45:43 +00:00
|
|
|
}
|