【问题标题】:Converting ZipOutputStream to ZipInputStream将 ZipOutputStream 转换为 ZipInputStream
【发布时间】:2017-11-21 14:07:28
【问题描述】:

我正在创建一个 ZipOutputStream 并向其中添加文件,这是在内存中完成的,稍后我希望能够从中获取/读取文件。我尝试了多种排列和组合,但无法完成。 我知道如果我使用 FileOutpuStream 并将物理文件附加到它,我将能够使用它,但我不能创建物理文件。

当前代码:-

 ByteArrayOutputStream bos = new ByteArrayOutputStream();
 ZipOutputStream zos = new ZipOutputStream(bos);
 ZipEntry ze = new ZipEntry(“Temp.txt”);
 zos.putNextEntry(ze);
 FileInputStream fis = new FileInputStream("Test/File1.txt");
 byte[] buffer = new byte[1024];
 int len;
 while ((len = fis.read(buffer)) > 0) {
   //System.out.println(new String(buffer));
    zos.write(buffer, 0, len);
 }

【问题讨论】:

    标签: java file-io zipoutputstream


    【解决方案1】:

    你在正确的轨道上!要读回文件,只需使用:

    byte[] zipBytes = bos.toByteArray();
    ByteArrayInputStream bis = new ByteArrayInputStream(zipBytes);
    ZipInputStream zis = new ZipInputStream(bis);
    

    【讨论】:

      猜你喜欢
      • 2013-12-11
      • 1970-01-01
      • 2011-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多