27 Star 109 Fork 24

MJ PC Lab/go-http-file-server

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
archiveZip.go 1.06 KB
一键复制 编辑 原始数据 按行查看 历史
package serverHandler
import (
"archive/zip"
"io"
"net/http"
"os"
)
func writeZip(zw *zip.Writer, f *os.File, fInfo os.FileInfo, archivePath string) error {
if archivePath[0] == '/' {
archivePath = archivePath[1:]
}
if fInfo.IsDir() {
archivePath += "/"
}
var size int64
if !fInfo.IsDir() {
size = fInfo.Size()
}
w, err := zw.Create(archivePath)
if err != nil {
return err
}
if size == 0 || f == nil || fInfo.IsDir() {
return nil
}
_, err = io.Copy(w, f)
if err != nil {
return err
}
return nil
}
func (h *aliasHandler) zip(w http.ResponseWriter, r *http.Request, pageData *responseData) {
if !pageData.CanArchive {
w.WriteHeader(http.StatusBadRequest)
return
}
selections, ok := h.normalizeArchiveSelections(r)
if !ok {
return
}
zipWriter := zip.NewWriter(w)
defer func() {
err := zipWriter.Close()
h.logError(err)
}()
h.archive(
w,
r,
pageData,
selections,
".zip",
"application/zip",
func(f *os.File, fInfo os.FileInfo, relPath string) error {
return writeZip(zipWriter, f, fInfo, relPath)
},
)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/mjpclab/go-http-file-server.git
git@gitee.com:mjpclab/go-http-file-server.git
mjpclab
go-http-file-server
go-http-file-server
v1.15.1

搜索帮助