Java:通过 HTTP 传输 Zipfile 的内容

发布于 2024-11-12 22:41:07 字数 415 浏览 2 评论 0原文

我有相当多的可流数据(>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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

青衫负雪 2024-11-19 22:41:07

下面是客户端可以用来从压缩流中读取的代码片段(有效):

static void processZippedInputStream(InputStream in, String entryNameRegex)
throws IOException
{
    ZipInputStream zin = new ZipInputStream(in);
    ZipEntry ze;
    while ((ze = zin.getNextEntry()) != null)
    {
        if (ze.getName().matches(entryNameRegex))
        {
            // treat zin as a normal input stream - ie read() from it till "empty" etc
            break;
        }
        zin.closeEntry();
    }
    zin.close();
}

与普通 InputStream 的主要区别在于迭代条目。例如,您可能知道您想要第一个条目,因此不需要名称匹配参数等。

Here's a snippet of code (that works) that the client can use to read from the zipped stream:

static void processZippedInputStream(InputStream in, String entryNameRegex)
throws IOException
{
    ZipInputStream zin = new ZipInputStream(in);
    ZipEntry ze;
    while ((ze = zin.getNextEntry()) != null)
    {
        if (ze.getName().matches(entryNameRegex))
        {
            // treat zin as a normal input stream - ie read() from it till "empty" etc
            break;
        }
        zin.closeEntry();
    }
    zin.close();
}

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.

绝情姑娘 2024-11-19 22:41:07

Java 通过 GZipInputStream(解压缩)和 GZipOutputStream(压缩)支持 gzip 格式。 zipgzip 内部使用相同的压缩格式,主要区别在于元数据: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 the GZipInputStream (decompressing) and GZipOutputStream (compressing). Both zip and gzip 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 (and gzip 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 with Content-Encoding: gzip, but you surely can do it manually if the server simply sends a the .gz file as such (i.e. with Content-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.)

茶花眉 2024-11-19 22:41:07

让网络服务器进行压缩会更有意义吗?如果您只是想减少所使用的带宽量,而不是真正想要将压缩的文件存储在服务器上,那么这只是一个配置问题,例如,请参阅:

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.

失退 2024-11-19 22:41:07

是的,您可以,流式传输 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

丘比特射中我 2024-11-19 22:41:07

使用GZIP,然后就可以进行流式传输。无论如何,Gzip 使用 zip 的默认压缩算法。

Use GZIP and then you can stream. Gzip uses the default compression algorithm of zip anyway.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文