在Java中,有没有办法知道gzip压缩的byte[]已经被多次压缩?

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

Is there any way to know that gzip compressed byte[] has been compressed more than once in java?

问题

我正在使用io.netty.handler.codec.http.HttpContentDecompressor在我的Spring Boot应用程序服务器中解压gzip压缩的请求,它成功地进行了解压缩。但是,当客户端错误地发送了两次压缩的请求主体时,Netty的HttpContentDecompressor只能解压缩一次,请求主体仍然是压缩的。因此,我的下游应用程序开始失败,因为它们假设只会收到已解压缩的数据。是否有方法可以知道接收到的byte[]是否已完全压缩或仍然未解压缩?

英文:

I am using io.netty.handler.codec.http.HttpContentDecompressor to decompress the gzip compressed request in my spring boot application server and its decompressing it fine. However when client send me 2 times compressed request body(by mistake) then netty HttpContentDecompressor could decompress it only once and request body was still compressed. So my downstream application started getting failed as they had assumption that they will only get the decompressed data.
Is there any way to know that receiving byte[] has been fully compressed or still decompressed?

答案1

得分: 2

gzip压缩文件包含众所周知的头部字节。

https://en.wikipedia.org/wiki/Gzip

> 一个包含魔术数字(1f 8b)、压缩方法(08代表DEFLATE)、1字节头部标志、4字节时间戳、压缩标志和操作系统ID的10字节头部。

在第一次解压缩后,请检查是否存在1f 8b

另外,如果您接受要处理的文件且文件类型很重要,最好验证受限文件集合。

英文:

gziped files contain well-known header bytes

https://en.wikipedia.org/wiki/Gzip

> a 10-byte header, containing a magic number (1f 8b), the compression method (08 for DEFLATE), 1-byte of header flags, a 4-byte timestamp, compression flags and the operating system ID.

After your first decompression, check to see if 1f 8b is present.

Aside, if you are accepting files for processing and the file type is important, it's good practice to validate the restricted sets of files.

huangapple
  • 本文由 发表于 2023年4月19日 21:41:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76055253.html
匿名

发表评论

匿名网友

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

确定