forked from forgejo/forgejo
Added all required dependencies
This commit is contained in:
parent
78f86abba4
commit
1ebb35b988
660 changed files with 502447 additions and 0 deletions
67
vendor/github.com/Unknwon/cae/zip/read.go
generated
vendored
Normal file
67
vendor/github.com/Unknwon/cae/zip/read.go
generated
vendored
Normal file
|
@ -0,0 +1,67 @@
|
|||
// Copyright 2013 Unknown
|
||||
//
|
||||
// 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.
|
||||
|
||||
package zip
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// OpenFile is the generalized open call; most users will use Open
|
||||
// instead. It opens the named zip file with specified flag
|
||||
// (O_RDONLY etc.) if applicable. If successful,
|
||||
// methods on the returned ZipArchive can be used for I/O.
|
||||
// If there is an error, it will be of type *PathError.
|
||||
func (z *ZipArchive) Open(name string, flag int, perm os.FileMode) error {
|
||||
// Create a new archive if it's specified and not exist.
|
||||
if flag&os.O_CREATE != 0 {
|
||||
f, err := os.Create(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
zw := zip.NewWriter(f)
|
||||
if err = zw.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
rc, err := zip.OpenReader(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
z.ReadCloser = rc
|
||||
z.FileName = name
|
||||
z.Comment = rc.Comment
|
||||
z.NumFiles = len(rc.File)
|
||||
z.Flag = flag
|
||||
z.Permission = perm
|
||||
z.isHasChanged = false
|
||||
|
||||
z.files = make([]*File, z.NumFiles)
|
||||
for i, f := range rc.File {
|
||||
z.files[i] = &File{}
|
||||
z.files[i].FileHeader, err = zip.FileInfoHeader(f.FileInfo())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
z.files[i].Name = strings.Replace(f.Name, "\\", "/", -1)
|
||||
if f.FileInfo().IsDir() && !strings.HasSuffix(z.files[i].Name, "/") {
|
||||
z.files[i].Name += "/"
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
77
vendor/github.com/Unknwon/cae/zip/stream.go
generated
vendored
Normal file
77
vendor/github.com/Unknwon/cae/zip/stream.go
generated
vendored
Normal file
|
@ -0,0 +1,77 @@
|
|||
// Copyright 2014 Unknown
|
||||
//
|
||||
// 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.
|
||||
|
||||
package zip
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// A StreamArchive represents a streamable archive.
|
||||
type StreamArchive struct {
|
||||
*zip.Writer
|
||||
}
|
||||
|
||||
// NewStreamArachive returns a new streamable archive with given io.Writer.
|
||||
// It's caller's responsibility to close io.Writer and streamer after operation.
|
||||
func NewStreamArachive(w io.Writer) *StreamArchive {
|
||||
return &StreamArchive{zip.NewWriter(w)}
|
||||
}
|
||||
|
||||
// StreamFile streams a file or directory entry into StreamArchive.
|
||||
func (s *StreamArchive) StreamFile(relPath string, fi os.FileInfo, data []byte) error {
|
||||
if fi.IsDir() {
|
||||
fh, err := zip.FileInfoHeader(fi)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fh.Name = relPath + "/"
|
||||
if _, err = s.Writer.CreateHeader(fh); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
fh, err := zip.FileInfoHeader(fi)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fh.Name = filepath.Join(relPath, fi.Name())
|
||||
fh.Method = zip.Deflate
|
||||
fw, err := s.Writer.CreateHeader(fh)
|
||||
if err != nil {
|
||||
return err
|
||||
} else if _, err = fw.Write(data); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// StreamReader streams data from io.Reader to StreamArchive.
|
||||
func (s *StreamArchive) StreamReader(relPath string, fi os.FileInfo, r io.Reader) (err error) {
|
||||
fh, err := zip.FileInfoHeader(fi)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fh.Name = filepath.Join(relPath, fi.Name())
|
||||
|
||||
fw, err := s.Writer.CreateHeader(fh)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = io.Copy(fw, r)
|
||||
return err
|
||||
}
|
364
vendor/github.com/Unknwon/cae/zip/write.go
generated
vendored
Normal file
364
vendor/github.com/Unknwon/cae/zip/write.go
generated
vendored
Normal file
|
@ -0,0 +1,364 @@
|
|||
// Copyright 2013 Unknown
|
||||
//
|
||||
// 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.
|
||||
|
||||
package zip
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/Unknwon/cae"
|
||||
)
|
||||
|
||||
// Switcher of printing trace information when pack and extract.
|
||||
var Verbose = true
|
||||
|
||||
// extractFile extracts zip.File to file system.
|
||||
func extractFile(f *zip.File, destPath string) error {
|
||||
filePath := path.Join(destPath, f.Name)
|
||||
os.MkdirAll(path.Dir(filePath), os.ModePerm)
|
||||
|
||||
rc, err := f.Open()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer rc.Close()
|
||||
|
||||
fw, err := os.Create(filePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer fw.Close()
|
||||
|
||||
if _, err = io.Copy(fw, rc); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Skip symbolic links.
|
||||
if f.FileInfo().Mode()&os.ModeSymlink != 0 {
|
||||
return nil
|
||||
}
|
||||
// Set back file information.
|
||||
if err = os.Chtimes(filePath, f.ModTime(), f.ModTime()); err != nil {
|
||||
return err
|
||||
}
|
||||
return os.Chmod(filePath, f.FileInfo().Mode())
|
||||
}
|
||||
|
||||
var defaultExtractFunc = func(fullName string, fi os.FileInfo) error {
|
||||
if !Verbose {
|
||||
return nil
|
||||
}
|
||||
|
||||
fmt.Println("Extracting file..." + fullName)
|
||||
return nil
|
||||
}
|
||||
|
||||
// ExtractToFunc extracts the whole archive or the given files to the
|
||||
// specified destination.
|
||||
// It accepts a function as a middleware for custom operations.
|
||||
func (z *ZipArchive) ExtractToFunc(destPath string, fn cae.HookFunc, entries ...string) (err error) {
|
||||
destPath = strings.Replace(destPath, "\\", "/", -1)
|
||||
isHasEntry := len(entries) > 0
|
||||
if Verbose {
|
||||
fmt.Println("Unzipping " + z.FileName + "...")
|
||||
}
|
||||
os.MkdirAll(destPath, os.ModePerm)
|
||||
for _, f := range z.File {
|
||||
f.Name = strings.Replace(f.Name, "\\", "/", -1)
|
||||
|
||||
// Directory.
|
||||
if strings.HasSuffix(f.Name, "/") {
|
||||
if isHasEntry {
|
||||
if cae.IsEntry(f.Name, entries) {
|
||||
if err = fn(f.Name, f.FileInfo()); err != nil {
|
||||
continue
|
||||
}
|
||||
os.MkdirAll(path.Join(destPath, f.Name), os.ModePerm)
|
||||
}
|
||||
continue
|
||||
}
|
||||
if err = fn(f.Name, f.FileInfo()); err != nil {
|
||||
continue
|
||||
}
|
||||
os.MkdirAll(path.Join(destPath, f.Name), os.ModePerm)
|
||||
continue
|
||||
}
|
||||
|
||||
// File.
|
||||
if isHasEntry {
|
||||
if cae.IsEntry(f.Name, entries) {
|
||||
if err = fn(f.Name, f.FileInfo()); err != nil {
|
||||
continue
|
||||
}
|
||||
err = extractFile(f, destPath)
|
||||
}
|
||||
} else {
|
||||
if err = fn(f.Name, f.FileInfo()); err != nil {
|
||||
continue
|
||||
}
|
||||
err = extractFile(f, destPath)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ExtractToFunc extracts the whole archive or the given files to the
|
||||
// specified destination.
|
||||
// It accepts a function as a middleware for custom operations.
|
||||
func ExtractToFunc(srcPath, destPath string, fn cae.HookFunc, entries ...string) (err error) {
|
||||
z, err := Open(srcPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer z.Close()
|
||||
return z.ExtractToFunc(destPath, fn, entries...)
|
||||
}
|
||||
|
||||
// ExtractTo extracts the whole archive or the given files to the
|
||||
// specified destination.
|
||||
// Call Flush() to apply changes before this.
|
||||
func (z *ZipArchive) ExtractTo(destPath string, entries ...string) (err error) {
|
||||
return z.ExtractToFunc(destPath, defaultExtractFunc, entries...)
|
||||
}
|
||||
|
||||
// ExtractTo extracts given archive or the given files to the
|
||||
// specified destination.
|
||||
func ExtractTo(srcPath, destPath string, entries ...string) (err error) {
|
||||
return ExtractToFunc(srcPath, destPath, defaultExtractFunc, entries...)
|
||||
}
|
||||
|
||||
// extractFile extracts file from ZipArchive to file system.
|
||||
func (z *ZipArchive) extractFile(f *File) error {
|
||||
if !z.isHasWriter {
|
||||
for _, zf := range z.ReadCloser.File {
|
||||
if f.Name == zf.Name {
|
||||
return extractFile(zf, path.Dir(f.tmpPath))
|
||||
}
|
||||
}
|
||||
}
|
||||
return cae.Copy(f.tmpPath, f.absPath)
|
||||
}
|
||||
|
||||
// Flush saves changes to original zip file if any.
|
||||
func (z *ZipArchive) Flush() error {
|
||||
if !z.isHasChanged || (z.ReadCloser == nil && !z.isHasWriter) {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Extract to tmp path and pack back.
|
||||
tmpPath := path.Join(os.TempDir(), "cae", path.Base(z.FileName))
|
||||
os.RemoveAll(tmpPath)
|
||||
defer os.RemoveAll(tmpPath)
|
||||
|
||||
for _, f := range z.files {
|
||||
if strings.HasSuffix(f.Name, "/") {
|
||||
os.MkdirAll(path.Join(tmpPath, f.Name), os.ModePerm)
|
||||
continue
|
||||
}
|
||||
|
||||
// Relative path inside zip temporary changed.
|
||||
f.tmpPath = path.Join(tmpPath, f.Name)
|
||||
if err := z.extractFile(f); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if z.isHasWriter {
|
||||
return packToWriter(tmpPath, z.writer, defaultPackFunc, true)
|
||||
}
|
||||
|
||||
if err := PackTo(tmpPath, z.FileName); err != nil {
|
||||
return err
|
||||
}
|
||||
return z.Open(z.FileName, os.O_RDWR|os.O_TRUNC, z.Permission)
|
||||
}
|
||||
|
||||
// packFile packs a file or directory to zip.Writer.
|
||||
func packFile(srcFile string, recPath string, zw *zip.Writer, fi os.FileInfo) error {
|
||||
if fi.IsDir() {
|
||||
fh, err := zip.FileInfoHeader(fi)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fh.Name = recPath + "/"
|
||||
if _, err = zw.CreateHeader(fh); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
fh, err := zip.FileInfoHeader(fi)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fh.Name = recPath
|
||||
fh.Method = zip.Deflate
|
||||
|
||||
fw, err := zw.CreateHeader(fh)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if fi.Mode()&os.ModeSymlink != 0 {
|
||||
target, err := os.Readlink(srcFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err = fw.Write([]byte(target)); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
f, err := os.Open(srcFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
if _, err = io.Copy(fw, f); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// packDir packs a directory and its subdirectories and files
|
||||
// recursively to zip.Writer.
|
||||
func packDir(srcPath string, recPath string, zw *zip.Writer, fn cae.HookFunc) error {
|
||||
dir, err := os.Open(srcPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer dir.Close()
|
||||
|
||||
fis, err := dir.Readdir(0)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, fi := range fis {
|
||||
if cae.IsFilter(fi.Name()) {
|
||||
continue
|
||||
}
|
||||
curPath := srcPath + "/" + fi.Name()
|
||||
tmpRecPath := filepath.Join(recPath, fi.Name())
|
||||
if err = fn(curPath, fi); err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
if fi.IsDir() {
|
||||
if err = packFile(srcPath, tmpRecPath, zw, fi); err != nil {
|
||||
return err
|
||||
}
|
||||
err = packDir(curPath, tmpRecPath, zw, fn)
|
||||
} else {
|
||||
err = packFile(curPath, tmpRecPath, zw, fi)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// packToWriter packs given path object to io.Writer.
|
||||
func packToWriter(srcPath string, w io.Writer, fn func(fullName string, fi os.FileInfo) error, includeDir bool) error {
|
||||
zw := zip.NewWriter(w)
|
||||
defer zw.Close()
|
||||
|
||||
f, err := os.Open(srcPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
fi, err := f.Stat()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
basePath := path.Base(srcPath)
|
||||
if fi.IsDir() {
|
||||
if includeDir {
|
||||
if err = packFile(srcPath, basePath, zw, fi); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
basePath = ""
|
||||
}
|
||||
return packDir(srcPath, basePath, zw, fn)
|
||||
}
|
||||
return packFile(srcPath, basePath, zw, fi)
|
||||
}
|
||||
|
||||
// packTo packs given source path object to target path.
|
||||
func packTo(srcPath, destPath string, fn cae.HookFunc, includeDir bool) error {
|
||||
fw, err := os.Create(destPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer fw.Close()
|
||||
|
||||
return packToWriter(srcPath, fw, fn, includeDir)
|
||||
}
|
||||
|
||||
// PackToFunc packs the complete archive to the specified destination.
|
||||
// It accepts a function as a middleware for custom operations.
|
||||
func PackToFunc(srcPath, destPath string, fn func(fullName string, fi os.FileInfo) error, includeDir ...bool) error {
|
||||
isIncludeDir := false
|
||||
if len(includeDir) > 0 && includeDir[0] {
|
||||
isIncludeDir = true
|
||||
}
|
||||
return packTo(srcPath, destPath, fn, isIncludeDir)
|
||||
}
|
||||
|
||||
var defaultPackFunc = func(fullName string, fi os.FileInfo) error {
|
||||
if !Verbose {
|
||||
return nil
|
||||
}
|
||||
|
||||
if fi.IsDir() {
|
||||
fmt.Printf("Adding dir...%s\n", fullName)
|
||||
} else {
|
||||
fmt.Printf("Adding file...%s\n", fullName)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// PackTo packs the whole archive to the specified destination.
|
||||
// Call Flush() will automatically call this in the end.
|
||||
func PackTo(srcPath, destPath string, includeDir ...bool) error {
|
||||
return PackToFunc(srcPath, destPath, defaultPackFunc, includeDir...)
|
||||
}
|
||||
|
||||
// Close opens or creates archive and save changes.
|
||||
func (z *ZipArchive) Close() (err error) {
|
||||
if err = z.Flush(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if z.ReadCloser != nil {
|
||||
if err = z.ReadCloser.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
z.ReadCloser = nil
|
||||
}
|
||||
return nil
|
||||
}
|
238
vendor/github.com/Unknwon/cae/zip/zip.go
generated
vendored
Normal file
238
vendor/github.com/Unknwon/cae/zip/zip.go
generated
vendored
Normal file
|
@ -0,0 +1,238 @@
|
|||
// Copyright 2013 Unknown
|
||||
//
|
||||
// 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.
|
||||
|
||||
// Package zip enables you to transparently read or write ZIP compressed archives and the files inside them.
|
||||
package zip
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"errors"
|
||||
"io"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/Unknwon/cae"
|
||||
)
|
||||
|
||||
// A File represents a file or directory entry in archive.
|
||||
type File struct {
|
||||
*zip.FileHeader
|
||||
oldName string // NOTE: unused, for future change name feature.
|
||||
oldComment string // NOTE: unused, for future change comment feature.
|
||||
absPath string // Absolute path of local file system.
|
||||
tmpPath string
|
||||
}
|
||||
|
||||
// A ZipArchive represents a file archive, compressed with Zip.
|
||||
type ZipArchive struct {
|
||||
*zip.ReadCloser
|
||||
FileName string
|
||||
Comment string
|
||||
NumFiles int
|
||||
Flag int
|
||||
Permission os.FileMode
|
||||
|
||||
files []*File
|
||||
isHasChanged bool
|
||||
|
||||
// For supporting flushing to io.Writer.
|
||||
writer io.Writer
|
||||
isHasWriter bool
|
||||
}
|
||||
|
||||
// OpenFile is the generalized open call; most users will use Open
|
||||
// instead. It opens the named zip file with specified flag
|
||||
// (O_RDONLY etc.) if applicable. If successful,
|
||||
// methods on the returned ZipArchive can be used for I/O.
|
||||
// If there is an error, it will be of type *PathError.
|
||||
func OpenFile(name string, flag int, perm os.FileMode) (*ZipArchive, error) {
|
||||
z := new(ZipArchive)
|
||||
err := z.Open(name, flag, perm)
|
||||
return z, err
|
||||
}
|
||||
|
||||
// Create creates the named zip file, truncating
|
||||
// it if it already exists. If successful, methods on the returned
|
||||
// ZipArchive can be used for I/O; the associated file descriptor has mode
|
||||
// O_RDWR.
|
||||
// If there is an error, it will be of type *PathError.
|
||||
func Create(name string) (*ZipArchive, error) {
|
||||
os.MkdirAll(path.Dir(name), os.ModePerm)
|
||||
return OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
|
||||
}
|
||||
|
||||
// Open opens the named zip file for reading. If successful, methods on
|
||||
// the returned ZipArchive can be used for reading; the associated file
|
||||
// descriptor has mode O_RDONLY.
|
||||
// If there is an error, it will be of type *PathError.
|
||||
func Open(name string) (*ZipArchive, error) {
|
||||
return OpenFile(name, os.O_RDONLY, 0)
|
||||
}
|
||||
|
||||
// New accepts a variable that implemented interface io.Writer
|
||||
// for write-only purpose operations.
|
||||
func New(w io.Writer) *ZipArchive {
|
||||
return &ZipArchive{
|
||||
writer: w,
|
||||
isHasWriter: true,
|
||||
}
|
||||
}
|
||||
|
||||
// List returns a string slice of files' name in ZipArchive.
|
||||
// Specify prefixes will be used as filters.
|
||||
func (z *ZipArchive) List(prefixes ...string) []string {
|
||||
isHasPrefix := len(prefixes) > 0
|
||||
names := make([]string, 0, z.NumFiles)
|
||||
for _, f := range z.files {
|
||||
if isHasPrefix && !cae.HasPrefix(f.Name, prefixes) {
|
||||
continue
|
||||
}
|
||||
names = append(names, f.Name)
|
||||
}
|
||||
return names
|
||||
}
|
||||
|
||||
// AddEmptyDir adds a raw directory entry to ZipArchive,
|
||||
// it returns false if same directory enry already existed.
|
||||
func (z *ZipArchive) AddEmptyDir(dirPath string) bool {
|
||||
dirPath = strings.Replace(dirPath, "\\", "/", -1)
|
||||
|
||||
if !strings.HasSuffix(dirPath, "/") {
|
||||
dirPath += "/"
|
||||
}
|
||||
|
||||
for _, f := range z.files {
|
||||
if dirPath == f.Name {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
dirPath = strings.TrimSuffix(dirPath, "/")
|
||||
if strings.Contains(dirPath, "/") {
|
||||
// Auto add all upper level directories.
|
||||
z.AddEmptyDir(path.Dir(dirPath))
|
||||
}
|
||||
z.files = append(z.files, &File{
|
||||
FileHeader: &zip.FileHeader{
|
||||
Name: dirPath + "/",
|
||||
UncompressedSize: 0,
|
||||
},
|
||||
})
|
||||
z.updateStat()
|
||||
return true
|
||||
}
|
||||
|
||||
// AddDir adds a directory and subdirectories entries to ZipArchive.
|
||||
func (z *ZipArchive) AddDir(dirPath, absPath string) error {
|
||||
dir, err := os.Open(absPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer dir.Close()
|
||||
|
||||
// Make sure we have all upper level directories.
|
||||
z.AddEmptyDir(dirPath)
|
||||
|
||||
fis, err := dir.Readdir(0)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, fi := range fis {
|
||||
curPath := absPath + "/" + fi.Name()
|
||||
tmpRecPath := path.Join(dirPath, fi.Name())
|
||||
if fi.IsDir() {
|
||||
if err = z.AddDir(tmpRecPath, curPath); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
if err = z.AddFile(tmpRecPath, curPath); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// updateStat should be called after every change for rebuilding statistic.
|
||||
func (z *ZipArchive) updateStat() {
|
||||
z.NumFiles = len(z.files)
|
||||
z.isHasChanged = true
|
||||
}
|
||||
|
||||
// AddFile adds a file entry to ZipArchive.
|
||||
func (z *ZipArchive) AddFile(fileName, absPath string) error {
|
||||
fileName = strings.Replace(fileName, "\\", "/", -1)
|
||||
absPath = strings.Replace(absPath, "\\", "/", -1)
|
||||
|
||||
if cae.IsFilter(absPath) {
|
||||
return nil
|
||||
}
|
||||
|
||||
f, err := os.Open(absPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
fi, err := f.Stat()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
file := new(File)
|
||||
file.FileHeader, err = zip.FileInfoHeader(fi)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
file.Name = fileName
|
||||
file.absPath = absPath
|
||||
|
||||
z.AddEmptyDir(path.Dir(fileName))
|
||||
|
||||
isExist := false
|
||||
for _, f := range z.files {
|
||||
if fileName == f.Name {
|
||||
f = file
|
||||
isExist = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !isExist {
|
||||
z.files = append(z.files, file)
|
||||
}
|
||||
|
||||
z.updateStat()
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteIndex deletes an entry in the archive by its index.
|
||||
func (z *ZipArchive) DeleteIndex(idx int) error {
|
||||
if idx >= z.NumFiles {
|
||||
return errors.New("index out of range of number of files")
|
||||
}
|
||||
|
||||
z.files = append(z.files[:idx], z.files[idx+1:]...)
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteName deletes an entry in the archive by its name.
|
||||
func (z *ZipArchive) DeleteName(name string) error {
|
||||
for i, f := range z.files {
|
||||
if f.Name == name {
|
||||
return z.DeleteIndex(i)
|
||||
}
|
||||
}
|
||||
return errors.New("entry with given name not found")
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue