add UnZipContent function for gcompress

This commit is contained in:
john 2019-07-24 15:05:02 +08:00
parent b4a0cca9c4
commit 1bc8c9e3e2
3 changed files with 32 additions and 2 deletions

View File

@ -8,6 +8,7 @@ package gcompress
import ( import (
"archive/zip" "archive/zip"
"bytes"
"github.com/gogf/gf/g/os/gfile" "github.com/gogf/gf/g/os/gfile"
"io" "io"
"os" "os"
@ -47,10 +48,24 @@ func ZipPath(path, dest string, prefix ...string) error {
// UnZipFile decompresses <archive> to <dest> using zip compressing algorithm. // UnZipFile decompresses <archive> to <dest> using zip compressing algorithm.
func UnZipFile(archive, dest string) error { func UnZipFile(archive, dest string) error {
reader, err := zip.OpenReader(archive) readerCloser, err := zip.OpenReader(archive)
if err != nil { if err != nil {
return err return err
} }
defer readerCloser.Close()
return unZipFileWithReader(&readerCloser.Reader, dest)
}
// UnZipContent decompresses <data> to <dest> using zip compressing algorithm.
func UnZipContent(data []byte, dest string) error {
reader, err := zip.NewReader(bytes.NewReader(data), int64(len(data)))
if err != nil {
return err
}
return unZipFileWithReader(reader, dest)
}
func unZipFileWithReader(reader *zip.Reader, dest string) error {
if err := os.MkdirAll(dest, 0755); err != nil { if err := os.MkdirAll(dest, 0755); err != nil {
return err return err
} }
@ -62,7 +77,7 @@ func UnZipFile(archive, dest string) error {
} }
dir := filepath.Dir(path) dir := filepath.Dir(path)
if len(dir) > 0 { if len(dir) > 0 {
if _, err = os.Stat(dir); os.IsNotExist(err) { if _, err := os.Stat(dir); os.IsNotExist(err) {
err = os.MkdirAll(dir, 0755) err = os.MkdirAll(dir, 0755)
if err != nil { if err != nil {
return err return err

Binary file not shown.

View File

@ -0,0 +1,15 @@
package main
import (
"fmt"
"github.com/gogf/gf/g/encoding/gcompress"
"github.com/gogf/gf/g/os/gfile"
)
func main() {
err := gcompress.UnZipContent(
gfile.GetBinContents(`D:\Workspace\Go\GOPATH\src\github.com\gogf\gf\geg\encoding\gcompress\data.zip`),
`D:\Workspace\Go\GOPATH\src\github.com\gogf\gf\geg`,
)
fmt.Println(err)
}