mirror of
https://github.com/moonD4rk/HackBrowserData.git
synced 2026-05-19 18:58:03 +02:00
feat: add compress result to zip file Close #24
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
@@ -12,6 +14,8 @@ import (
|
||||
"hack-browser-data/log"
|
||||
)
|
||||
|
||||
const Prefix = "[x]: "
|
||||
|
||||
func CopyDB(src, dst string) error {
|
||||
locals, _ := filepath.Glob("*")
|
||||
for _, v := range locals {
|
||||
@@ -104,3 +108,41 @@ func MakeDir(dirName string) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func Compress(exportDir string) error {
|
||||
files, err := ioutil.ReadDir(exportDir)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
}
|
||||
var b = new(bytes.Buffer)
|
||||
zw := zip.NewWriter(b)
|
||||
for _, f := range files {
|
||||
fw, _ := zw.Create(f.Name())
|
||||
fileName := path.Join(exportDir, f.Name())
|
||||
fileContent, err := ioutil.ReadFile(fileName)
|
||||
if err != nil {
|
||||
zw.Close()
|
||||
return err
|
||||
}
|
||||
_, err = fw.Write(fileContent)
|
||||
if err != nil {
|
||||
zw.Close()
|
||||
return err
|
||||
}
|
||||
err = os.Remove(fileName)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
}
|
||||
}
|
||||
if err := zw.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
zipName := exportDir + `/archive.zip`
|
||||
outFile, _ := os.Create(zipName)
|
||||
_, err = b.WriteTo(outFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Printf("%s Compress success, zip filename is %s \n", Prefix, zipName)
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user