从ZipInputStream类读入一个字节数组输出流

从ZipInputStream类读入一个字节数组输出流

 

java zipinputstream zipoutputstream
我试图读取一个单个文件 java.util.zip.ZipInputStream ,并将其复制到一个 java.io.ByteArrayOutputStream (这样我就可以创建一个 java.io.ByteArrayInputStream 并将其交给一个第三方库,最终会关闭流,我不希望我的 ZipInputStream 越来越封闭)。 我可能失去了基本的这里,但我从来没有在这里输入while循环:
ByteArrayOutputStream streamBuilder = new ByteArrayOutputStream();
int bytesRead;
byte[] tempBuffer = new byte[8192*2];
try {
 while ((bytesRead = zipStream.read(tempBuffer)) != -1) {
  streamBuilder.write(tempBuffer, 0, bytesRead);
 }
} catch (IOException e) {
 // ...
}
我缺少的是将要复制的数据流? 编辑: 我应该更早,这 ZipInputStream 从文件中,所以我不认为我一个 ZipFile 。它从一个文件上传通过servlet。 另外 CodeGo.net,我已经叫 getNextEntry() ZipInputStream 在开始讨论之前对这个代码片段。如果我不尝试将文件复制到另一个 InputStream (通过 OutputStream 上面提到的),只是通过 ZipInputStream 我的第三方库,该库关闭流,我什么都做不了更多,就像对付流中的剩余文件。
本文地址 :CodeGo.net/4012/ 
------------------------------------------------------------------------------------------------------------------------- 
1.  你的循环看起来有效-这是什么下面的代码(只在它自己)返回?
zipStream.read(tempBuffer)
如果它返回-1,然后才把它的zipStream中是封闭的,并且所有的赌注都关闭。这是你的调试器,并确保什么东西被传递给你的是实际有效的。 当你调用getNextEntry(),它返回一个值,并且是在(即不getCompressedSize()返回一个有效的值)的数据?如果你只是读不有预读ZIP条目嵌入一个ZIP文件,然后ZipInputStream类是不会为你工作。 关于Zip格式有用的花絮: 嵌入在一个zip文件中的每个文件都有一个头。此标头可以包含(如流的长度 CodeGo.net,它的文件,在CRC的偏移)-或者它可以包含魔法值,基本上说“是不是在流头,你要检查zip后同步”。 每个压缩文件中,则具有连接到包含所有zip条目,沿与实际数据文件的结尾的表。在最后的表是强制性的,并且在它的值必须是正确的。与此相反,嵌入在数据流中的值没有被提供。 如果ZipFile的,它会读取该表在zip的结尾。如果ZipInputStream类,我怀疑getNextEntry()尝试嵌入在数据流中的条目。如果未指定这些值,然后ZipInputStream类已经不知道多久的流可能是。该算法是自终止(你其实并不需要知道输出流的长度,以完全恢复输出),但它是可能的,这种阅读器的Java版本并没有处理这种情况非常好。 我会说,这是相当有一个servlet返回一个ZipInputStream类(它是多收到InflatorInputStream中,如果你将要的内容。 
2.  我从IO项目IOUtils。
IOUtils.copy(zipStream, byteArrayOutputStream);

3.  我想,你试过从阅读 FileInputStream 像这样:
ZipInputStream in = new ZipInputStream(new FileInputStream(...));
这是不行的! 总之,使用ZipFile中,也不要重新发明轮子。使用commons-io的地方可能! 看起来像这样:
ZipFile zipFile = new ZipFile("foo.zip");
InputStream in = null;
ByteArrayOutputStream out = null;
try {
 in = zipFile.getInputStream(zipFile.getEntry("fileInTheZip"));
 out = new ByteArrayOutputStream();
 IOUtils.copy(in, out);
} finally {
 IOUtils.closeQuietly(in);
 IOUtils.closeQuietly(out);
}
您可能也有兴趣在文件工具,如果你不知道这个类呢! 
4.  您可以将您自己的包装周围,忽略close()方法的ZipInputStream类和其交给第三方库。
thirdPartyLib.handleZipData(new CloseIgnoringInputStream(zipStream));

class CloseIgnoringInputStream extends InputStream
{
 private ZipInputStream stream;
 public CloseIgnoringInputStream(ZipInputStream inStream)
 {
 	stream = inStream;
 }
 public int read() throws IOException {
 	return stream.read();
 }
 public void close()
 {
 	//ignore
 }
 public void reallyClose() throws IOException
 {
 	stream.close();
 }
}

5.  我会打电话getNextEntry()上的ZipInputStream类,直到它在你想要的条目(使用的ZipEntry。等)。调用getNextEntry()将propel“光标”,以它返回的条目的开头。然后,使用ZipEntry.getSize(),以确定有多少个字节,你应该zipInputStream.read()。 
6.  你错过的呼叫 入门的ZipEntry=(ZipEntry的)zipStream.getNextEntry(); 以定位所述优先条目的优先个字节。
`ByteArrayOutputStream streamBuilder = new ByteArrayOutputStream();
 int bytesRead;
 byte[] tempBuffer = new byte[8192*2];
 ZipEntry entry = (ZipEntry) zipStream.getNextEntry();
 try {
  while ( (bytesRead = zipStream.read(tempBuffer)) != -1 ){
  streamBuilder.write(tempBuffer, 0, bytesRead);
  }
 } catch (IOException e) {
  ...
 }`

7.  目前还不清楚你是如何得到的zipStream中。当你得到它这样它应该工作:
 zipStream = zipFile.getInputStream(zipEntry)

8.  t是不清楚你是如何得到的zipStream中。当你得到它这样它应该工作:
 zipStream = zipFile.getInputStream(zipEntry)
如果您是从ZipFile中取得ZipInputStream类,你可以得到一个流的三维党库,让它和你之前得到另一种输入的代码。 一个InputStream是一个光标。如果你有整个数据(如zip档)你可以问N个游标权。 一个diferent情况是,如果你只有一个“gzip”这个InputStream中,只有一个压缩字throttling。在这种情况下,你的ByteArrayOutputStream缓冲区,使所有的感觉。 
9.  请尽量代码波纹管
private static byte[] getZipArchiveContent(File zipName) throws WorkflowServiceBusinessException {
 BufferedInputStream buffer = null;
 FileInputStream fileStream = null;
 ByteArrayOutputStream byteOut = null;
 byte data[] = new byte[BUFFER];
 try {
 try {
 fileStream = new FileInputStream(zipName);
 buffer = new BufferedInputStream(fileStream);
 byteOut = new ByteArrayOutputStream();
 int count;
 while((count = buffer.read(data, 0, BUFFER)) != -1) {
  byteOut.write(data, 0, count);
 }
 } catch(Exception e) {
 throw new WorkflowServiceBusinessException(e.getMessage(), e);
 } finally {
 if(null != fileStream) {
  fileStream.close();
 }
 if(null != buffer) {
  buffer.close();
 }
 if(null != byteOut) {
  byteOut.close();
 }
 }
 } catch(Exception e) {
 throw new WorkflowServiceBusinessException(e.getMessage(), e);
 }
 return byteOut.toByteArray();
 }

10.  检查输入流定位在乞讨。 否则,我不认为你需要写入的结果数据流,而你正在阅读的,除非你在另一个线程处理这个确切的数据流。 只要创建一个字节数组,读取输入流,然后创建输出流。
本文标题 :从ZipInputStream类读入一个字节数组输出流
本文地址 :CodeGo.net/4012/ 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值