【发布时间】:2012-07-15 04:18:54
【问题描述】:
我的用例要求我打开一个 txt 文件,比如 abc.txt,它位于一个 zip 存档中,其中包含表单中的键值对
key1=value1
key2=value2
.. 以此类推,每个键值对都在一个新行中。 我必须更改与某个键对应的一个值,并将文本文件放回存档的新副本中。我如何在 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();
【问题讨论】:
-
那么,除了刷新输出流之外,还有什么不工作的?
-
我没听懂你。我没有明确刷新输出流。
标签: java zip file-handling