Dump: add output format tar and output to stdout (#10376)

* Dump: Use mholt/archive/v3 to support tar including many compressions

Signed-off-by: Philipp Homann <homann.philipp@googlemail.com>

* Dump: Allow dump output to stdout

Signed-off-by: Philipp Homann <homann.philipp@googlemail.com>

* Dump: Fixed bug present since #6677 where SessionConfig.Provider is never "file"

Signed-off-by: Philipp Homann <homann.philipp@googlemail.com>

* Dump: never pack RepoRootPath, LFS.ContentPath and LogRootPath when they are below AppDataPath

Signed-off-by: Philipp Homann <homann.philipp@googlemail.com>

* Dump: also dump LFS (fixes #10058)

Signed-off-by: Philipp Homann <homann.philipp@googlemail.com>

* Dump: never dump CustomPath if CustomPath is a subdir of or equal to AppDataPath (fixes #10365)

Signed-off-by: Philipp Homann <homann.philipp@googlemail.com>

* Use log.Info instead of fmt.Fprintf

Signed-off-by: Philipp Homann <homann.philipp@googlemail.com>

* import ordering

* make fmt

Co-authored-by: zeripath <art27@cantab.net>
Co-authored-by: techknowlogick <techknowlogick@gitea.io>
Co-authored-by: Matti R <matti@mdranta.net>
This commit is contained in:
PhilippHomann 2020-06-05 22:47:39 +02:00 committed by GitHub
parent 209b17c4e2
commit 684b7a999f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
303 changed files with 301317 additions and 1183 deletions

View file

@ -1,25 +0,0 @@
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so
# Folders
_obj
_test
.idea
# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out
*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*
_testmain.go
cae.iml
*.exe
.DS_Store

191
vendor/github.com/unknwon/cae/LICENSE generated vendored
View file

@ -1,191 +0,0 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction, and
distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by the copyright
owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all other entities
that control, are controlled by, or are under common control with that entity.
For the purposes of this definition, "control" means (i) the power, direct or
indirect, to cause the direction or management of such entity, whether by
contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity exercising
permissions granted by this License.
"Source" form shall mean the preferred form for making modifications, including
but not limited to software source code, documentation source, and configuration
files.
"Object" form shall mean any form resulting from mechanical transformation or
translation of a Source form, including but not limited to compiled object code,
generated documentation, and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or Object form, made
available under the License, as indicated by a copyright notice that is included
in or attached to the work (an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object form, that
is based on (or derived from) the Work and for which the editorial revisions,
annotations, elaborations, or other modifications represent, as a whole, an
original work of authorship. For the purposes of this License, Derivative Works
shall not include works that remain separable from, or merely link (or bind by
name) to the interfaces of, the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including the original version
of the Work and any modifications or additions to that Work or Derivative Works
thereof, that is intentionally submitted to Licensor for inclusion in the Work
by the copyright owner or by an individual or Legal Entity authorized to submit
on behalf of the copyright owner. For the purposes of this definition,
"submitted" means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems, and
issue tracking systems that are managed by, or on behalf of, the Licensor for
the purpose of discussing and improving the Work, but excluding communication
that is conspicuously marked or otherwise designated in writing by the copyright
owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf
of whom a Contribution has been received by Licensor and subsequently
incorporated within the Work.
2. Grant of Copyright License.
Subject to the terms and conditions of this License, each Contributor hereby
grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
irrevocable copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the Work and such
Derivative Works in Source or Object form.
3. Grant of Patent License.
Subject to the terms and conditions of this License, each Contributor hereby
grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
irrevocable (except as stated in this section) patent license to make, have
made, use, offer to sell, sell, import, and otherwise transfer the Work, where
such license applies only to those patent claims licensable by such Contributor
that are necessarily infringed by their Contribution(s) alone or by combination
of their Contribution(s) with the Work to which such Contribution(s) was
submitted. If You institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work or a
Contribution incorporated within the Work constitutes direct or contributory
patent infringement, then any patent licenses granted to You under this License
for that Work shall terminate as of the date such litigation is filed.
4. Redistribution.
You may reproduce and distribute copies of the Work or Derivative Works thereof
in any medium, with or without modifications, and in Source or Object form,
provided that You meet the following conditions:
You must give any other recipients of the Work or Derivative Works a copy of
this License; and
You must cause any modified files to carry prominent notices stating that You
changed the files; and
You must retain, in the Source form of any Derivative Works that You distribute,
all copyright, patent, trademark, and attribution notices from the Source form
of the Work, excluding those notices that do not pertain to any part of the
Derivative Works; and
If the Work includes a "NOTICE" text file as part of its distribution, then any
Derivative Works that You distribute must include a readable copy of the
attribution notices contained within such NOTICE file, excluding those notices
that do not pertain to any part of the Derivative Works, in at least one of the
following places: within a NOTICE text file distributed as part of the
Derivative Works; within the Source form or documentation, if provided along
with the Derivative Works; or, within a display generated by the Derivative
Works, if and wherever such third-party notices normally appear. The contents of
the NOTICE file are for informational purposes only and do not modify the
License. You may add Your own attribution notices within Derivative Works that
You distribute, alongside or as an addendum to the NOTICE text from the Work,
provided that such additional attribution notices cannot be construed as
modifying the License.
You may add Your own copyright statement to Your modifications and may provide
additional or different license terms and conditions for use, reproduction, or
distribution of Your modifications, or for any such Derivative Works as a whole,
provided Your use, reproduction, and distribution of the Work otherwise complies
with the conditions stated in this License.
5. Submission of Contributions.
Unless You explicitly state otherwise, any Contribution intentionally submitted
for inclusion in the Work by You to the Licensor shall be under the terms and
conditions of this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify the terms of
any separate license agreement you may have executed with Licensor regarding
such Contributions.
6. Trademarks.
This License does not grant permission to use the trade names, trademarks,
service marks, or product names of the Licensor, except as required for
reasonable and customary use in describing the origin of the Work and
reproducing the content of the NOTICE file.
7. Disclaimer of Warranty.
Unless required by applicable law or agreed to in writing, Licensor provides the
Work (and each Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied,
including, without limitation, any warranties or conditions of TITLE,
NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are
solely responsible for determining the appropriateness of using or
redistributing the Work and assume any risks associated with Your exercise of
permissions under this License.
8. Limitation of Liability.
In no event and under no legal theory, whether in tort (including negligence),
contract, or otherwise, unless required by applicable law (such as deliberate
and grossly negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special, incidental,
or consequential damages of any character arising as a result of this License or
out of the use or inability to use the Work (including but not limited to
damages for loss of goodwill, work stoppage, computer failure or malfunction, or
any and all other commercial damages or losses), even if such Contributor has
been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability.
While redistributing the Work or Derivative Works thereof, You may choose to
offer, and charge a fee for, acceptance of support, warranty, indemnity, or
other liability obligations and/or rights consistent with this License. However,
in accepting such obligations, You may act only on Your own behalf and on Your
sole responsibility, not on behalf of any other Contributor, and only if You
agree to indemnify, defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason of your
accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work
To apply the Apache License to your work, attach the following boilerplate
notice, with the fields enclosed by brackets "[]" replaced with your own
identifying information. (Don't include the brackets!) The text should be
enclosed in the appropriate comment syntax for the file format. We also
recommend that a file or class name and description of purpose be included on
the same "printed page" as the copyright notice for easier identification within
third-party archives.
Copyright [yyyy] [name of copyright owner]
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.

View file

@ -1,37 +0,0 @@
Compression and Archive Extensions
==================================
[![Go Walker](http://gowalker.org/api/v1/badge)](http://gowalker.org/github.com/unknwon/cae)
[中文文档](README_ZH.md)
Package cae implements PHP-like Compression and Archive Extensions.
But this package has some modifications depends on Go-style.
Reference: [PHP:Compression and Archive Extensions](http://www.php.net/manual/en/refs.compression.php).
Code Convention: based on [Go Code Convention](https://github.com/unknwon/go-code-convention).
### Implementations
Package `zip`([Go Walker](http://gowalker.org/github.com/unknwon/cae/zip)) and `tz`([Go Walker](http://gowalker.org/github.com/unknwon/cae/tz)) both enable you to transparently read or write ZIP/TAR.GZ compressed archives and the files inside them.
- Features:
- Add file or directory from everywhere to archive, no one-to-one limitation.
- Extract part of entries, not all at once.
- Stream data directly into `io.Writer` without any file system storage.
### Test cases and Coverage
All subpackages use [GoConvey](http://goconvey.co/) to write test cases, and coverage is more than 80 percent.
### Use cases
- [Gogs](https://github.com/gogits/gogs): self hosted Git service in the Go Programming Language.
- [GoBlog](https://github.com/fuxiaohei/GoBlog): personal blogging application.
- [GoBuild](https://github.com/shxsun/gobuild/): online Go cross-platform compilation and download service.
## License
This project is under Apache v2 License. See the [LICENSE](LICENSE) file for the full license text.

View file

@ -1,29 +0,0 @@
压缩与打包扩展
=============
[![Go Walker](http://gowalker.org/api/v1/badge)](http://gowalker.org/github.com/unknwon/cae)
包 cae 实现了 PHP 风格的压缩与打包扩展。
但本包依据 Go 语言的风格进行了一些修改。
引用:[PHP:Compression and Archive Extensions](http://www.php.net/manual/en/refs.compression.php)
编码规范:基于 [Go 编码规范](https://github.com/unknwon/go-code-convention)
### 实现
`zip`([Go Walker](http://gowalker.org/github.com/unknwon/cae/zip)) 和 `tz`([Go Walker](http://gowalker.org/github.com/unknwon/cae/tz)) 都允许你轻易的读取或写入 ZIP/TAR.GZ 压缩档案和其内部文件。
- 特性:
- 将任意位置的文件或目录加入档案,没有一对一的操作限制。
- 只解压部分文件,而非一次性解压全部。
- 将数据以流的形式直接写入 `io.Writer` 而不需经过文件系统的存储。
### 测试用例与覆盖率
所有子包均采用 [GoConvey](http://goconvey.co/) 来书写测试用例,覆盖率均超过 80%。
## 授权许可
本项目采用 Apache v2 开源授权许可证,完整的授权说明已放置在 [LICENSE](LICENSE) 文件中。

108
vendor/github.com/unknwon/cae/cae.go generated vendored
View file

@ -1,108 +0,0 @@
// 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 cae implements PHP-like Compression and Archive Extensions.
package cae
import (
"io"
"os"
"strings"
)
// A Streamer describes an streamable archive object.
type Streamer interface {
StreamFile(string, os.FileInfo, []byte) error
StreamReader(string, os.FileInfo, io.Reader) error
Close() error
}
// A HookFunc represents a middleware for packing and extracting archive.
type HookFunc func(string, os.FileInfo) error
// HasPrefix returns true if name has any string in given slice as prefix.
func HasPrefix(name string, prefixes []string) bool {
for _, prefix := range prefixes {
if strings.HasPrefix(name, prefix) {
return true
}
}
return false
}
// IsEntry returns true if name equals to any string in given slice.
func IsEntry(name string, entries []string) bool {
for _, e := range entries {
if e == name {
return true
}
}
return false
}
// IsFilter returns true if given name matches any of global filter rule.
func IsFilter(name string) bool {
if strings.Contains(name, ".DS_Store") {
return true
}
return false
}
// IsExist returns true if given path is a file or directory.
func IsExist(path string) bool {
_, err := os.Stat(path)
return err == nil || os.IsExist(err)
}
// Copy copies file from source to target path.
func Copy(dest, src string) error {
// Gather file information to set back later.
si, err := os.Lstat(src)
if err != nil {
return err
}
// Handle symbolic link.
if si.Mode()&os.ModeSymlink != 0 {
target, err := os.Readlink(src)
if err != nil {
return err
}
// NOTE: os.Chmod and os.Chtimes don't recoganize symbolic link,
// which will lead "no such file or directory" error.
return os.Symlink(target, dest)
}
sr, err := os.Open(src)
if err != nil {
return err
}
defer sr.Close()
dw, err := os.Create(dest)
if err != nil {
return err
}
defer dw.Close()
if _, err = io.Copy(dw, sr); err != nil {
return err
}
// Set back file information.
if err = os.Chtimes(dest, si.ModTime(), si.ModTime()); err != nil {
return err
}
return os.Chmod(dest, si.Mode())
}

View file

@ -1,67 +0,0 @@
// 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
}

View file

@ -1,77 +0,0 @@
// 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
}

View file

@ -1,364 +0,0 @@
// 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
}

View file

@ -1,238 +0,0 @@
// 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")
}