我的用例需要我打開一個txt文件,說的abc.txt這是包含在形式修改ZIP檔案文本文件中的Java
鍵1鍵 - 值對一個zip壓縮包內=值1
鍵2 =值
..等,其中每個鍵 - 值對是在一個新行。 我必須更改與某個特定鍵相對應的一個值,並將該文本文件重新放回到存檔的新副本中。我如何在java中做到這一點?
我迄今爲止嘗試:
ZipFile zipFile = new ZipFile("test.zip");
final ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("out.zip"));
for(Enumeration e = zipFile.entries(); e.hasMoreElements();) {
ZipEntry entryIn = (ZipEntry) e.nextElement();
if(!entryIn.getName().equalsIgnoreCase("abc.txt")){
zos.putNextEntry(entryIn);
InputStream is = zipFile.getInputStream(entryIn);
byte [] buf = new byte[1024];
int len;
while((len = (is.read(buf))) > 0) {
zos.write(buf, 0, len);
}
}
else{
// I'm not sure what to do here
// Tried a few things and the file gets corrupt
}
zos.closeEntry();
}
zos.close();
那麼,其他然後刷新輸出流,有什麼不工作? – MadProgrammer 2012-07-16 10:25:16
我沒有得到你。我沒有明確地刷新輸出流。 – Prabhakar 2012-07-17 04:19:50