英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论