FileSystem fileSystem;
try {
fileSystem = FileSystems.newFileSystem(path2File.toPath(), null);//path2File是压缩包的File
Path zipXmlPath =fileSystem.getPath("/description.xml"); //获取压缩包内xml的path
String description1= path2File.getParent()+"/description1.xml";
File description1File = new File(description1);
if(!description1File.exists()){
description1File.createNewFile();//此文件用来存放从的description.xml复制的内容
}
Path dPath1 =description1File.toPath();
Files.copy(zipXmlPath,dPath1, StandardCopyOption.REPLACE_EXISTING);//将压缩文件的description.xml复制内容到description1.xml文件中
String modifyXml= path2File.getParent()+"/description.xml";
File modifyFile = new File(modifyXml);
if(!modifyFile.exists()){
modifyFile.createNewFile();//此文件用来存放description1.xml修改后的内容
}
Path modifyPath =modifyFile.toPath();
BufferedReader reader = Files.newBufferedReader(dPath1, StandardCharsets.UTF_8);
BufferedWriter writer = Files.newBufferedWriter(modifyPath, StandardCharsets.UTF_8);
String str = null;
boolean flag=true;
while ((str = reader.readLine()) != null) {
writer.write(str);
writer.write("\r\n");
}
writer.flush();
writer.close();
Files.copy(modifyPath,zipXmlPath, StandardCopyOption.REPLACE_EXISTING); //将修改后的文件再复制到压缩文件内
在不解压缩的情况下修改压缩包内的文件
最新推荐文章于 2025-03-19 14:00:40 发布