adding decompress
This commit is contained in:
@@ -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
|
||||
|
Reference in New Issue
Block a user