我有一个名为test.zip的压缩文件,该压缩文件中有2个文件file1.txt和file2.txt。 我想使用核心Java概念更新file1.txt文件并添加一个文件file3.txt iin zip文件,而不提取zip文件。 谁能建议我针对该用例使用哪种方法。 预先谢谢你。
答案 0 :(得分:0)
获取要执行的操作的文件,
try (FileInputStream fis = new FileInputStream( String fileName = "C:/Users/Administrator/Desktop/log.zip";);
BufferedInputStream bis = new BufferedInputStream(fis);
ZipInputStream zis = new ZipInputStream(bis)) {
ZipEntry ze;
while ((ze = zis.getNextEntry()) != null) {
System.out.format("File: %s Size: %d Last Modified %s %n",
ze.getName(), ze.getSize(),
LocalDate.ofEpochDay(ze.getTime() / MILLS_IN_DAY));
}
}
然后获取如下的bufferreader对象,
ZipEntry entry = entries.nextElement();
InputStream stream = zipFile.getInputStream(entry);
BufferedReader br = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
拥有后,您可以将内容修改为
while (line = br.readLine() != null)
{
if (line.contains("java"))
line = line.replace("java", " ");
lines.add(line);
}
FileWriter fw = new FileWriter(f1);
BufferedWriter out = new BufferedWriter(fw);
out.write(lines.toString());