检查流是否为 zip 文件 [英] Checking if a stream is a zip file

查看:38
本文介绍了检查流是否为 zip 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们需要确定传入的 InputStream 是对 zip 文件的引用还是对 zip 数据的引用.我们没有对流的底层源的引用.我们的目标是将此流的内容复制到指向备用位置的 OutputStream 中.

We have a requirement to determine whether an incoming InputStream is a reference to an zip file or zip data. We do not have reference to the underlying source of the stream. We aim to copy the contents of this stream into an OutputStream directed at an alternate location.

我尝试使用 ZipInputStream 读取流并提取 ZipEntry.如果流是常规文件,则 ZipEntry 为 null - 正如预期的那样 - 但是,在检查 ZipEntry 时,我从流中丢失了最初的几个字节.因此,当我知道流是常规流时,我已经丢失了流中的初始数据.

I tried reading the stream using ZipInputStream and extracting a ZipEntry. The ZipEntry is null if the stream is a regular file - as expected - however, in checking for a ZipEntry I loose the initial couple of bytes from the stream. Hence, by the time I know that the stream is a regular stream, I have already lost initial data from the stream.

关于如何检查 InputStream 是否是没有数据丢失的存档的任何想法都会有所帮助.

Any thoughts around how to check if the InputStream is an archive without data loss would be helpful.

谢谢.

推荐答案

假设您的原始输入流没有缓冲,我会尝试将原始流包装在 BufferedInputStream 中,然后再将其包装在 ZipInputStream 中进行检查.您可以在 BufferedInputStream 中使用mark"和reset",在您检查后返回到流中的初始位置.

Assuming your original inputstream is not buffered, I would try wrapping the original stream in a BufferedInputStream, before wrapping that in a ZipInputStream to check. You can use "mark" and "reset" in the BufferedInputStream to return to the initial position in the stream, after your check.

这篇关于检查流是否为 zip 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆