将字符串写入 zip 文件而不在磁盘上创建文件。 Java

Write string to file inside zip without creating file on disk. Java

我需要从 zip "Inputs" 存档中读取(需要做其他事情)所有 txt 文件,并保存到其他 "Result" 而无需将文件解压缩到磁盘,所有需要做的 [=25= 】 记忆中。 使用 ZipInputStream / ZipOutputStream 读取文件。

问题是写入结果 zip 文件。我试试:

String zipEntryString = toStringZipEntry(zis);           // Read file to string
String parcedStringFile = parceContacts(zipEntryString); // Just return the same string at this moment nothing do

// Try write to string resultZip
InputStream is = new  ByteArrayInputStream(parcedStringFile.getBytes(ENCODING));
zos.putNextEntry(zipEntry);

int bufferSize;
byte[] buffer = new byte[BUFFER_SIZE];
while ((bufferSize = is.read(buffer, 0, buffer.length)) != -1) {
   zos.write(buffer, 0, bufferSize);
}

// Also try in whis way
// byte[] stringBytes = parcedStringFile.getBytes();
// zos.putNextEntry(zipEntry);
// zos.write(stringBytes);

is.close();

结果我创建了文件:

2b37 2028 3130 3129 2031 3131 2d32 3232
2d31 3120 2061 6263 4065 7274 2e63 6f6d
2c20 6465 6640 7364 662e 6f72 670d 0a2b
3120 2831 3032 2920 3132 3335 3332 2d32
... 
0000 0000 0000 0000 0000 0000 0000 0000

据我所知,文件写入的是字节而不是字符串。

我应该做错什么?我想念什么?

您可以使用 JIMFS(Java In Memory File System) 等内存文件系统库,它由 google 开发,是同类中最好的库之一。

成立。

int ch;
while ((ch = is.read()) != 0) {
  zos.write(ch);
}