wide/util/zip.go

144 lines
2.8 KiB
Go
Raw Permalink Normal View History

2014-11-24 12:24:35 +03:00
// Copyright (c) 2014, B3log
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
//
2014-11-24 12:24:35 +03:00
// http://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"
"io"
"io/ioutil"
"os"
"path/filepath"
)
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)
if err != nil {
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)
if err != nil {
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)
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-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)
2014-11-24 05:39:33 +03:00
if err != nil {
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)
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
}