1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-23 10:37:09 -05:00
v2fly/tools/build/archive.go

63 lines
1.1 KiB
Go
Raw Normal View History

2015-10-18 05:42:00 -04:00
package main
import (
"archive/zip"
"io"
"os"
"path/filepath"
)
type ZipWorker struct {
zipWriter *zip.Writer
root string
}
func NewZipWorker(zipFile io.Writer, root string) *ZipWorker {
return &ZipWorker{
zipWriter: zip.NewWriter(zipFile),
root: root,
}
}
func (worker *ZipWorker) run() error {
defer worker.close()
return filepath.Walk(worker.root, worker.zipAllFiles)
}
func (worker *ZipWorker) zipAllFiles(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
fileWriter, err := worker.zipWriter.Create(path)
if err != nil {
return err
}
fileReader, err := os.Open(path)
if err != nil {
return err
}
_, err = io.Copy(fileWriter, fileReader)
if err != nil {
return err
}
return nil
}
func (worker *ZipWorker) close() {
worker.zipWriter.Close()
}
func zipFolder(folder string, file string) error {
if _, err := os.Stat(file); err == nil {
os.Remove(file)
}
zipFile, err := os.Create(file)
if err != nil {
return err
}
defer zipFile.Close()
return NewZipWorker(zipFile, folder).run()
}