首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将ZipOutputStream转换为FileInputStream

将ZipOutputStream转换为FileInputStream
EN

Stack Overflow用户
提问于 2013-05-09 13:19:04
回答 1查看 7K关注 0票数 2

我有一个代码,它接受一个文件,压缩它,并将它存储在一个数据库中。

看起来像这样

代码语言:javascript
复制
          // STEP 1 - Create a ZIP file
          byte[] buffer = new byte[1024];// 18024
          ZipOutputStream outZip = new ZipOutputStream(new FileOutputStream("file.zip"));
          outZip.setLevel(Deflater.DEFAULT_COMPRESSION);
          FileInputStream in = new FileInputStream(c:\file.hex);
          outZip.putNextEntry(new ZipEntry("file.hex"));
          int len;
          while (( len = in.read(buffer)) > 0)
                outZip.write(buffer, 0, len);
          outZip.closeEntry();
          in.close();
          outZip.close();

          // STEP 2 - Insert zip file to DB
          file = new File("file.zip");
          FileInputStream fis = new FileInputStream( file );

我存储在DB中的fis对象。

但是我希望完全避免文件系统,而不是创建file.zip。我认为我需要直接将ZipOutputStream转换成FileInputStream,但我找不到方法。

有什么简单的方法可以做到这一点吗?

  • 在解压缩文件时,我也遇到了同样的问题,为了读取它,我必须创建两个不同的临时文件-- file.zip和file.hex。
EN

回答 1

Stack Overflow用户

发布于 2013-05-09 13:25:08

您根本不需要创建FileOutputStream。使用ByteArrayOutputStream代替:

代码语言:javascript
复制
ByteArrayOutputStream zipBytes = new ByteArrayOutputStream()
ZipOutputStream outZip = new ZipOutputStream(zipBytes);

// run your code that adds zip entries....

zipBytes.getBytes() // returns array of bytes that contain your zipped information.
票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16462798

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档