wide/util/zip.go

137 lines
2.6 KiB
Go
Raw Normal View History

2014-11-24 12:24:35 +03:00
// Copyright (c) 2014, B3log
//
// 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
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// 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"
"io"
"io/ioutil"
"os"
"path/filepath"
)
type myzip struct{}
// Zip utilities.
var Zip = myzip{}
type ZipFile struct {
zipFile *os.File
writer *zip.Writer
}
func (*myzip) Create(filename string) (*ZipFile, error) {
file, err := os.Create(filename)
if err != nil {
return nil, err
}
return &ZipFile{zipFile: file, writer: zip.NewWriter(file)}, nil
}
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-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)
if err != nil {
return err
}
}
return nil
}
2014-11-24 12:20:43 +03:00
func (z *ZipFile) AddEntry(path, name string) error {
fi, err := os.Stat(name)
if err != nil {
return err
}
fh, err := zip.FileInfoHeader(fi)
if err != nil {
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)
2014-11-24 05:39:33 +03:00
if err != nil {
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)
if err != nil {
return err
}
defer file.Close()
_, err = io.Copy(entry, file)
return err
}
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)
2014-11-24 05:39:33 +03:00
if err != nil {
return err
}
}
return nil
}
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)
if err != nil {
return err
}
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() {
2014-11-24 12:20:43 +03:00
z.AddEntry(path, dirName)
2014-11-24 05:39:33 +03:00
err = z.AddDirectory(zipPath, localPath)
} else {
err = z.AddEntry(zipPath, localPath)
}
if err != nil {
return err
}
}
return nil
}