Reputation: 310
I'm trying to edit the contents of an odt file using zip4j (I tried using java ZipEntries but I couldn't even delete the entries from the file itself that's why I chose to use a library instead). I can confirm that the file I am trying to overwrite exits I can even read from it and tell when it was created so that part works. Now when I'm trying to edit the odt contents (removing or overwriting) Zip4j throws a ZipException which says: cannot rename modified zip file. What am I doing wrong?
try
{
File temp = new File(f.getParent()+"/tmp/content.xml");
new File(temp.getParent()).mkdirs();
FileUtils.write(temp, "", encoding);
net.lingala.zip4j.ZipFile zf = new net.lingala.zip4j.ZipFile(f.getPath());
ZipParameters p = new ZipParameters();
p.setEncryptionMethod(EncryptionMethod.NONE);
p.setOverrideExistingFilesInZip(true);
p.setFileNameInZip("content.xml");
p.setCompressionMethod(CompressionMethod.DEFLATE);
zf.addFile(temp, p);
}
catch (Exception e)
{
e.printStackTrace();
}
Upvotes: 0
Views: 882
Reputation: 109613
The zip file system with its jar:file:
protocol is supported by Path & Files. A Path maintains its FileSystem, so one can use all operations.
Path osPath = Paths.get("C:/ ... .odt");
URI uri = URI.create("jar:" + osPath.toUri());
Map<String, String> env = new HashMap<>();
env.put("create", "true");
try (FileSystem zipFS = FileSystems.newFileSystem(uri, env)) {
Files.copy(zipFS.getPath("/media/image1.png"), osPath.resolveSibling("image1.png"),
StandardCopyOption.REPLACE_EXISTING);
Files.move(zipFS.getPath("/media/image2a.png"), zipFS.getPath("/media/image2.png"));
}
Upvotes: 1