Java:通过 HTTP 传输 Zipfile 的内容
我有相当多的可流数据(>100MB),为了压缩,我想将其打包在 http 服务器上的 zip 文件中。所以这个 zip 文件包含一个文件。
现在,java 客户端是否可以通过 http 传输数据,即使数据打包在 zip 文件中?
根据维基百科,ZIP 不是按顺序...
http://en.wikipedia.org /wiki/ZIP_(file_format)#Structure
如果这仍然可能以某种方式实现,那么如何实现呢?
编辑:关于gzip:正如我所说,我使用自定义java客户端(不是网络浏览器)gzip在java http实现中可用吗?
I have quite some amount of streamable data (>100MB), which, for the sake of compression, i would like to host packed in a zipfile on an http-server. So this zipfile contains a single file.
Now is it possible for a java-client to stream the data via http, even though it is packed in a zipfile?
According to wikipedia, ZIPs are not sequentially...
http://en.wikipedia.org/wiki/ZIP_(file_format)#Structure
If this is still possible somehow, then how?
edit: about gzip: as i said, i use a custom java client (not a webbrowser) is gzip available in the java http implementation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
下面是客户端可以用来从压缩流中读取的代码片段(有效):
与普通 InputStream 的主要区别在于迭代条目。例如,您可能知道您想要第一个条目,因此不需要名称匹配参数等。
Here's a snippet of code (that works) that the client can use to read from the zipped stream:
The main difference with a normal InputStream is iterating through the entries. You may know, for example, that you want the first entry, so no need for the name matching parameter etc.
Java 通过
GZipInputStream
(解压缩)和GZipOutputStream
(压缩)支持gzip
格式。zip
和gzip
内部使用相同的压缩格式,主要区别在于元数据:zip
位于文件末尾,gzip
开头(并且gzip
只支持一个简单的封闭文件)。对于流式传输一个大文件,使用 gzip 会是更好的选择 - 甚至更重要,因为您不需要访问元数据。
我不确定 HTTPConnection 是否发送
Accept-Encoding: gzip
,然后如果服务器使用Content-Encoding: gzip
传递内容,则自动处理内容膨胀,但您肯定如果服务器只是发送.gz
文件(即使用Content-Encoding: Identity
),则可以手动执行此操作。(顺便说一句,请确保从流中读取的缓冲区不要太小,因为每个 deflate 调用都会产生本机调用开销,因为 Java 的 GZipInputStream 使用本机 zlib 实现。)
Java supports the
gzip
format with theGZipInputStream
(decompressing) andGZipOutputStream
(compressing). Bothzip
andgzip
use the same compressing format internally, the main difference is in the metadata:zip
has it at the end of the file,gzip
at the beginning (andgzip
only supports one enclosed file easily).For your of streaming one big file, using
gzip
will be the better thing to do - even more as you don't need access to the metadata.I'm not sure if the HTTPConnection sends
Accept-Encoding: gzip
and then handles inflating the content automatically if the server delivers it withContent-Encoding: gzip
, but you surely can do it manually if the server simply sends a the.gz
file as such (i.e. withContent-Encoding: identity
).(By the way, make sure to read from the stream with not too small buffers, as each deflate call will have a native call overhead, since Java's GZipInputStream uses the native zlib implementation.)
让网络服务器进行压缩会更有意义吗?如果您只是想减少所使用的带宽量,而不是真正想要将压缩的文件存储在服务器上,那么这只是一个配置问题,例如,请参阅:
http://tomcat.apache.org/tomcat-5.5-doc/config/http.html
的HTTP/1.1 GZIP 压缩。服务器可以强制对客户端的响应进行压缩。
另请参阅http://en.wikipedia.org/wiki/HTTP_compression。
客户端将接收压缩包并处理解压缩。也应该可以流式传输文件,因此客户端在执行有用的操作之前不需要所有文件,因为服务器可以压缩各个块。
Would it make more sense to let the web server do the zipping? If you are simply trying to reduce the amount of bandwidth being used, rather than really wanting to store the file zipped up on the server, this would simply be a matter of configurations, for example see:
http://tomcat.apache.org/tomcat-5.5-doc/config/http.html
for HTTP/1.1 GZIP compression. The server can force the response to the client to be zipped.
See also http://en.wikipedia.org/wiki/HTTP_compression.
The client will receive zipped packets and handle the unzipping. It should be possible to stream the file too, so the client doesn't need all the file before it can do something useful, because the server can zip individual chunks.
是的,您可以,流式传输 zip 并将 MIME 类型用作 application/zip
如果您确实想在另一端播放流式音乐,那么这不能简单地完成,因为您只能解压一次整个 zip 可以在客户端上找到。
如果您担心大小,您可以降低 mp3 比特率或使用 ogg/vorbis 等格式
Yes you can, Stream the zip and use the MIME type as application/zip
If you actually want to play stream music on the other end, then it can't be done trivially as you can only unpack once the entire zip is available on client.
If size is you concern, you can either turn down your mp3 bit-rate or use formats such as ogg/vorbis
使用GZIP,然后就可以进行流式传输。无论如何,Gzip 使用 zip 的默认压缩算法。
Use GZIP and then you can stream. Gzip uses the default compression algorithm of zip anyway.