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

时间:2015-04-10 10:36:49

标签: java zip

我需要阅读(需要其他东西)来自zip"输入"的所有txt文件。存档,并保存到其他"结果"没有将文件解压缩到磁盘,所有都需要做到飞行"飞行"在记忆中。 使用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

正如我所看到的,文件写入字节不是字符串。

我该怎么办?我错过了什么?

2 个答案:

答案 0 :(得分:0)

您可以在内存文件系统库中使用由谷歌开发的JIMFS(Java In Memory File System)库,这是最好的库之一。

答案 1 :(得分:-1)

成立。

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