wide/util/zip.go

231 lines
4.3 KiB
Go
Raw Normal View History

2018-01-01 07:35:13 +03:00
// Copyright (c) 2014-2018, b3log.org & hacpai.com
2014-12-07 06:42:34 +03:00
//
2014-11-24 12:24:35 +03:00
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
2014-12-07 06:42:34 +03:00
//
2018-03-12 07:28:33 +03:00
// https://www.apache.org/licenses/LICENSE-2.0
2014-12-07 06:42:34 +03:00
//
2014-11-24 12:24:35 +03:00
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2014-11-24 05:39:33 +03:00
package util
import (
"archive/zip"
2015-03-28 07:15:44 +03:00
"bytes"
2014-11-24 05:39:33 +03:00
"io"
"io/ioutil"
"os"
"path/filepath"
2015-03-28 07:15:44 +03:00
"unicode/utf8"
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"
2014-11-24 05:39:33 +03:00
)
type myzip struct{}
// Zip utilities.
var Zip = myzip{}
2014-12-07 06:42:34 +03:00
// ZipFile represents a zip file.
2014-11-24 05:39:33 +03:00
type ZipFile struct {
zipFile *os.File
writer *zip.Writer
}
2014-12-07 06:42:34 +03:00
// Create creates a zip file with the specified filename.
2014-11-24 05:39:33 +03:00
func (*myzip) Create(filename string) (*ZipFile, error) {
file, err := os.Create(filename)
2015-11-20 04:13:34 +03:00
if nil != err {
2014-11-24 05:39:33 +03:00
return nil, err
}
return &ZipFile{zipFile: file, writer: zip.NewWriter(file)}, nil
}
2014-12-07 06:42:34 +03:00
// Close closes the zip file writer.
2014-11-24 05:39:33 +03:00
func (z *ZipFile) Close() error {
2014-11-24 12:20:43 +03:00
err := z.writer.Close()
2014-11-24 05:39:33 +03:00
if nil != err {
return err
}
2014-11-24 12:20:43 +03:00
return z.zipFile.Close() // close the underlying writer
2014-11-24 05:39:33 +03:00
}
2014-12-07 06:42:34 +03:00
// AddEntryN adds entries.
2014-11-24 12:20:43 +03:00
func (z *ZipFile) AddEntryN(path string, names ...string) error {
2014-11-24 05:39:33 +03:00
for _, name := range names {
2014-11-24 12:20:43 +03:00
zipPath := filepath.Join(path, name)
2014-11-24 05:39:33 +03:00
err := z.AddEntry(zipPath, name)
2015-11-20 04:13:34 +03:00
if nil != err {
2014-11-24 05:39:33 +03:00
return err
}
}
return nil
}
2014-12-07 06:42:34 +03:00
// AddEntry adds a entry.
2014-11-24 12:20:43 +03:00
func (z *ZipFile) AddEntry(path, name string) error {
fi, err := os.Stat(name)
2015-11-20 04:13:34 +03:00
if nil != err {
2014-11-24 12:20:43 +03:00
return err
}
fh, err := zip.FileInfoHeader(fi)
2015-11-20 04:13:34 +03:00
if nil != err {
2014-11-24 12:20:43 +03:00
return err
}
fh.Name = filepath.ToSlash(filepath.Clean(path))
fh.Method = zip.Deflate // data compression algorithm
if fi.IsDir() {
fh.Name = fh.Name + "/" // be care the ending separator
}
entry, err := z.writer.CreateHeader(fh)
2015-11-20 04:13:34 +03:00
if nil != err {
2014-11-24 05:39:33 +03:00
return err
}
2014-11-24 12:20:43 +03:00
if fi.IsDir() {
return nil
}
2014-11-24 05:39:33 +03:00
file, err := os.Open(name)
2015-11-20 04:13:34 +03:00
if nil != err {
2014-11-24 05:39:33 +03:00
return err
}
defer file.Close()
_, err = io.Copy(entry, file)
return err
}
2014-12-07 06:42:34 +03:00
// AddDirectoryN adds directories.
2014-11-24 12:20:43 +03:00
func (z *ZipFile) AddDirectoryN(path string, names ...string) error {
2014-11-24 05:39:33 +03:00
for _, name := range names {
2014-11-24 12:20:43 +03:00
err := z.AddDirectory(path, name)
2015-11-20 04:13:34 +03:00
if nil != err {
2014-11-24 05:39:33 +03:00
return err
}
}
return nil
}
2014-12-07 06:42:34 +03:00
// AddDirectory adds a directory.
2014-11-24 12:20:43 +03:00
func (z *ZipFile) AddDirectory(path, dirName string) error {
2014-11-24 05:39:33 +03:00
files, err := ioutil.ReadDir(dirName)
2015-11-20 04:13:34 +03:00
if nil != err {
2014-11-24 05:39:33 +03:00
return err
}
2015-11-20 04:13:34 +03:00
if 0 == len(files) {
err := z.AddEntry(path, dirName)
if nil != err {
return err
}
return nil
}
2014-11-24 05:39:33 +03:00
for _, file := range files {
localPath := filepath.Join(dirName, file.Name())
2014-11-24 12:20:43 +03:00
zipPath := filepath.Join(path, file.Name())
2014-11-24 05:39:33 +03:00
err = nil
if file.IsDir() {
err = z.AddDirectory(zipPath, localPath)
} else {
err = z.AddEntry(zipPath, localPath)
}
2015-11-20 04:13:34 +03:00
if nil != err {
2014-11-24 05:39:33 +03:00
return err
}
}
return nil
}
2015-03-21 09:39:26 +03:00
func cloneZipItem(f *zip.File, dest string) error {
// create full directory path
2015-03-28 07:15:44 +03:00
fileName := f.Name
if !utf8.ValidString(fileName) {
data, err := ioutil.ReadAll(transform.NewReader(bytes.NewReader([]byte(fileName)), simplifiedchinese.GB18030.NewDecoder()))
if nil == err {
fileName = string(data)
} else {
2015-04-01 05:47:12 +03:00
logger.Error(err)
2015-03-28 07:15:44 +03:00
}
}
path := filepath.Join(dest, fileName)
2015-03-21 09:39:26 +03:00
err := os.MkdirAll(filepath.Dir(path), os.ModeDir|os.ModePerm)
if nil != err {
return err
}
if f.FileInfo().IsDir() {
2015-11-20 04:38:29 +03:00
err = os.Mkdir(path, os.ModeDir|os.ModePerm)
if nil != err {
return err
}
2015-03-21 09:39:26 +03:00
return nil
}
// clone if item is a file
rc, err := f.Open()
if nil != err {
return err
}
defer rc.Close()
2015-11-20 04:38:29 +03:00
// use os.Create() since Zip don't store file permissions
2015-03-21 09:39:26 +03:00
fileCopy, err := os.Create(path)
if nil != err {
return err
}
defer fileCopy.Close()
_, err = io.Copy(fileCopy, rc)
if nil != err {
return err
}
return nil
}
// Unzip extracts a zip file specified by the zipFilePath to the destination.
func (*myzip) Unzip(zipFilePath, destination string) error {
r, err := zip.OpenReader(zipFilePath)
if nil != err {
return err
}
defer r.Close()
for _, f := range r.File {
err = cloneZipItem(f, destination)
if nil != err {
return err
}
}
return nil
}