我的用例需要我打开一个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