使用Go语言从zip文件中提供内容的HTTP服务器。

huangapple go评论113阅读模式
英文:

go - serve contents from a zip file in http

问题

我正在查看github.com/remyoudompheng/go-misc/zipfs来从zip文件中提供内容的http服务。

这个简单的示例可以工作,我可以获取files/archives.zip中包含的{FILE}:
http://localhost:8080/zip/{FILE}

  1. package main
  2. import (
  3. "archive/zip"
  4. "github.com/remyoudompheng/go-misc/zipfs"
  5. "log"
  6. "net/http"
  7. )
  8. func main() {
  9. z, err := zip.OpenReader("files/archive.zip")
  10. if err != nil {
  11. log.Fatal(err)
  12. }
  13. defer z.Close()
  14. http.Handle("/zip/", http.StripPrefix("/zip/", http.FileServer(zipfs.NewZipFS(&z.Reader))))
  15. log.Fatal(http.ListenAndServe(":8080", nil))
  16. }

现在,假设我想要像**http://localhost:8080/zip/{ZIPFILE}/{FILE}**这样的东西。
我尝试注册一个函数,但不起作用。

  1. package main
  2. import (
  3. "archive/zip"
  4. "github.com/remyoudompheng/go-misc/zipfs"
  5. "html"
  6. "log"
  7. "net/http"
  8. "strings"
  9. )
  10. func servezip(res http.ResponseWriter, req *http.Request) {
  11. zippath := "files/" + strings.Split(html.EscapeString(req.URL.Path), "/")[2] + ".zip"
  12. z, err := zip.OpenReader(zippath)
  13. if err != nil {
  14. http.Error(res, err.Error(), 404)
  15. return
  16. }
  17. defer z.Close()
  18. http.StripPrefix("/zip/", http.FileServer(zipfs.NewZipFS(&z.Reader)))
  19. }
  20. func main() {
  21. http.HandleFunc("/zip/", servezip)
  22. log.Fatal(http.ListenAndServe(":8080", nil))
  23. }

我漏掉了什么?handleFunc能返回http.FileServer吗?

英文:

i'm looking at github.com/remyoudompheng/go-misc/zipfs to serve content from a zip file in http.

this minimal example works, i can get a {FILE} contained in files/archives.zip:
http://localhost:8080/zip/{FILE}

  1. package main
  2. import (
  3. "archive/zip"
  4. "github.com/remyoudompheng/go-misc/zipfs"
  5. "log"
  6. "net/http"
  7. )
  8. func main() {
  9. z, err := zip.OpenReader("files/archive.zip")
  10. if err != nil {
  11. log.Fatal(err)
  12. }
  13. defer z.Close()
  14. http.Handle("/zip/", http.StripPrefix("/zip/", http.FileServer(zipfs.NewZipFS(&z.Reader))))
  15. log.Fatal(http.ListenAndServe(":8080", nil))
  16. }

now, suppose that i want something like http://localhost:8080/zip/{ZIPFILE}/{FILE}
i'm trying registering a func but doesn't work

  1. package main
  2. import (
  3. "archive/zip"
  4. "github.com/remyoudompheng/go-misc/zipfs"
  5. "html"
  6. "log"
  7. "net/http"
  8. "strings"
  9. )
  10. func servezip(res http.ResponseWriter, req *http.Request) {
  11. zippath := "files/" + strings.Split(html.EscapeString(req.URL.Path), "/")[2] + ".zip"
  12. z, err := zip.OpenReader(zippath)
  13. if err != nil {
  14. http.Error(res, err.Error(), 404)
  15. return
  16. }
  17. defer z.Close()
  18. http.StripPrefix("/zip/", http.FileServer(zipfs.NewZipFS(&z.Reader)))
  19. }
  20. func main() {
  21. http.HandleFunc("/zip/", servezip)
  22. log.Fatal(http.ListenAndServe(":8080", nil))
  23. }

what i'm missing? can an handlefunc return an http.fileserver?

答案1

得分: 1

你需要调用你创建的处理程序。尝试将servezip中的最后一行更改为:

  1. http.StripPrefix("/zip/", http.FileServer(zipfs.NewZipFS(&z.Reader))).ServeHTTP(res, req)
英文:

You need to call the handler that you create. Try changing the last line in servezip to

  1. http.StripPrefix("/zip/", http.FileServer(zipfs.NewZipFS(&z.Reader))).ServeHTTP(res, req)

huangapple
  • 本文由 发表于 2014年11月21日 00:15:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/27044349.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定