如何将 ZipInputStream 转换为 InputStream?
我有代码,其中 ZipInputSream 转换为 byte[],但我不知道如何将其转换为输入流。
private void convertStream(String encoding, ZipInputStream in) throws IOException,
UnsupportedEncodingException
{
final int BUFFER = 1;
@SuppressWarnings("unused")
int count = 0;
byte data[] = new byte[BUFFER];
while ((count = in.read(data, 0, BUFFER)) != -1)
{
// How can I convert data to InputStream here ?
}
}
I have code, where ZipInputSream is converted to byte[], but I don't know how I can convert that to inputstream.
private void convertStream(String encoding, ZipInputStream in) throws IOException,
UnsupportedEncodingException
{
final int BUFFER = 1;
@SuppressWarnings("unused")
int count = 0;
byte data[] = new byte[BUFFER];
while ((count = in.read(data, 0, BUFFER)) != -1)
{
// How can I convert data to InputStream here ?
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
ZipInputStream 是InputStream 的子类。
http://download.oracle.com/javase /6/docs/api/java/util/zip/ZipInputStream.html
ZipInputStream is a subclass of InputStream.
http://download.oracle.com/javase/6/docs/api/java/util/zip/ZipInputStream.html
这是我解决这个问题的方法。现在我可以将单个文件作为 InputStream 从 ZipInputStream 获取到内存。
Here is how I solved this problem. Now I can get single files from ZipInputStream to memory as InputStream.
请查看下面的函数示例,该函数将从 ZIP 存档中提取所有文件。此函数不适用于子文件夹中的文件:
Please find below example of function that will extract all files from ZIP archive. This function will not work with files in subfolders:
以下适用于我的 FileOutputStream:
Below works for me for FileOutputStream:
ZipInputStream
允许直接读取 ZIP 内容:使用getNextEntry()
进行迭代,直到找到要读取的条目,然后从ZipInputStream
中读取。如果您不想只读取 ZIP 内容,但需要在进入下一步之前对流应用额外的转换,则可以使用
PipedInputStream
和PipedOutputStream
。这个想法与此类似(从内存中编写,甚至可能无法编译):ZipInputStream
allows to read ZIP contents directly: iterate usinggetNextEntry()
until you find the entry you want to read and then just read from theZipInputStream
.If you don't want to just read ZIP content, but you need to apply an additional transform to the stream before passing to the next step, you can use
PipedInputStream
andPipedOutputStream
. The idea would be similar to this (written from memory, might not even compile):邮政编码相当简单,但我在将 ZipInputStream 作为输入流返回时遇到了问题。由于某种原因,zip 中包含的某些文件的字符被删除。以下是我的解决方案,到目前为止它一直有效。
The zip code is fairly easy but I had issues with returning ZipInputStream as Inputstream. For some reason, some of the files contained within the zip had characters being dropped. The below was my solution and so far its been working.