Recommended way to check if a file is compressed /zipped

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

Recommended way to check if a file is compressed /zipped

问题

我正在尝试检查一个上传的文件(没有文件扩展名)是否被压缩/打包。我已经搜索了fileinfo、fileheader等,但还没有找到一种方法来实现这个。也许可以通过魔术数字来判断?谢谢。

英文:

I am trying to check if an uploaded file ( no file extension) is compressed/ zipped.
I searched through the fileinfo, fileheader etc but haven't found a way to do this. May be the magic number?
Thanks

答案1

得分: 4

一个快速且简便的内置方法是使用http.DetectContentType()。它不仅可以检测gzip/zip,还可以做更多的事情,但由于它不会检查太多的内容,所以非常快速。如果你想检查一个较小的集合,并跳过对字符串进行测试,可以查看src/net/http/sniff.go中的源代码,并提取你想要的测试。

英文:

A quick-and-easy built-in way is with http.DetectContentType(). It does much more than just gzip/zip, but it's very fast because it doesn't check for too many things. If you want to check for a smaller set, and skip testing a string, check the source code in src/net/http/sniff.go and pull out the tests you want.

huangapple
  • 本文由 发表于 2015年6月2日 03:12:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/30581384.html
匿名

发表评论

匿名网友

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

确定