2

我正在创建一个 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);
 }
4

1 回答 1

2

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

byte[] zipBytes = bos.toByteArray();
ByteArrayInputStream bis = new ByteArrayInputStream(zipBytes);
ZipInputStream zis = new ZipInputStream(bis);
于 2017-11-21T14:13:16.733 回答