Created
February 21, 2025 23:05
-
-
Save froody/be6eb6f7c46dc81ee96226119160c81c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"os" | |
"log" | |
"bufio" | |
"flag" | |
"github.com/klauspost/compress/zstd" | |
) | |
func do_compress(input_file string, output_file string) { | |
input, err := os.Open(input_file) | |
reader := bufio.NewReader(input) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer input.Close() | |
output, err := os.Create(output_file) | |
writer := bufio.NewWriter(output) | |
enc, _ := zstd.NewWriter(writer) | |
_, err = enc.ReadFrom(reader) | |
if err != nil { | |
log.Fatal(err) | |
} | |
enc.Flush() | |
writer.Flush() | |
defer enc.Close() | |
defer output.Close() | |
} | |
func do_decompress(input_file string, output_file string) { | |
input, err := os.Open(input_file) | |
reader := bufio.NewReader(input) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer input.Close() | |
output, err := os.Create(output_file) | |
writer := bufio.NewWriter(output) | |
dec, _ := zstd.NewReader(reader) | |
_, err = dec.WriteTo(writer) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer dec.Close() | |
defer output.Close() | |
} | |
func main() { | |
decompress := flag.Bool("d", false, "Decompress") | |
input_file := flag.String("i", "", "Input file") | |
output_file := flag.String("o", "", "Output file") | |
flag.Parse() | |
if *decompress { | |
println("Decompressing") | |
do_decompress(*input_file, *output_file) | |
} else { | |
println("Compressing") | |
do_compress(*input_file, *output_file) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment