adding decompress

This commit is contained in:
2020-07-07 15:03:33 -04:00
parent c07e09d155
commit ebd6e095f7
77 changed files with 185 additions and 50 deletions

View File

@@ -2,10 +2,14 @@ package engine
import (
"bytes"
"compress/gzip"
"io/ioutil"
"os"
"encoding/gob"
"fmt"
"io"
gzip "github.com/klauspost/pgzip"
)
type SomeStruct struct {
@@ -14,6 +18,51 @@ type SomeStruct struct {
C float64
}
// CompressFile uses pgzip to compress a file for storage
func CompressFile(path string) error {
fileBytes, err := ioutil.ReadFile(path)
if err != nil {
return fmt.Errorf("unable to read file: %s", path)
}
file, err := os.Create(path)
if err != nil {
return err
}
w := gzip.NewWriter(file)
w.SetConcurrency(100000, 10)
bytesWritten, err := w.Write(fileBytes)
if err != nil {
return err
}
fmt.Println("%d Bytes Written", bytesWritten)
w.Close()
return nil
}
// RestoreFile takes the compressed file path and the target path and decompresses the file into the target path
func RestoreFile(compressPath, targetPath string) error {
fileBytes, err := os.Open(compressPath)
if err != nil {
return fmt.Errorf("unable to read file: %s", compressPath)
}
err = os.Remove(targetPath)
if err != nil {
return fmt.Errorf("unable to remove target file to replace with decompressed file: %s", targetPath)
}
file, err := os.Create(targetPath)
if err != nil {
return err
}
r, err := gzip.NewReader(fileBytes)
bytesWritten, err := r.WriteTo(file)
if err != nil {
return err
}
fmt.Println("%d Bytes Written", bytesWritten)
r.Close()
return nil
}
//1.
func StructToBytes(obj SomeStruct) (bytes.Buffer, error) {
//now gob this