LittleEndianByteArrayOutputStream bos = new LittleEndianByteArrayOutputStream(buf, 0);\r
out.write(bos);\r
\r
- return dir.createDocument(parts[parts.length-1], bos.getWriteIndex(), new POIFSWriterListener(){\r
+ String fileName = parts[parts.length-1];\r
+ \r
+ if (dir.hasEntry(fileName)) {\r
+ dir.getEntry(fileName).delete();\r
+ }\r
+ \r
+ return dir.createDocument(fileName, bos.getWriteIndex(), new POIFSWriterListener(){\r
public void processPOIFSWriterEvent(POIFSWriterEvent event) {\r
try {\r
event.getStream().write(buf, 0, event.getLimit());\r
import org.apache.poi.poifs.filesystem.POIFSFileSystem;\r
import org.apache.poi.util.BoundedInputStream;\r
import org.apache.poi.util.IOUtils;\r
+import org.apache.poi.util.TempFile;\r
+import org.apache.poi.xwpf.usermodel.XWPFDocument;\r
import org.junit.Assume;\r
+import org.junit.Ignore;\r
import org.junit.Test;\r
\r
public class TestEncryptor {\r
assertThat(payloadExpected, equalTo(payloadActual));\r
}\r
\r
+ @Test\r
+ @Ignore\r
+ public void testInPlaceRewrite() throws Exception {\r
+ File f = TempFile.createTempFile("protected_agile", ".docx");\r
+ // File f = new File("protected_agile.docx");\r
+ FileOutputStream fos = new FileOutputStream(f);\r
+ InputStream fis = POIDataSamples.getPOIFSInstance().openResourceAsStream("protected_agile.docx");\r
+ IOUtils.copy(fis, fos);\r
+ fis.close();\r
+ fos.close();\r
+ \r
+ NPOIFSFileSystem fs = new NPOIFSFileSystem(f, false);\r
+\r
+ // decrypt the protected file - in this case it was encrypted with the default password\r
+ EncryptionInfo encInfo = new EncryptionInfo(fs);\r
+ Decryptor d = encInfo.getDecryptor();\r
+ boolean b = d.verifyPassword(Decryptor.DEFAULT_PASSWORD);\r
+ assertTrue(b);\r
+\r
+ // do some strange things with it ;)\r
+ XWPFDocument docx = new XWPFDocument(d.getDataStream(fs));\r
+ docx.getParagraphArray(0).insertNewRun(0).setText("POI was here! All your base are belong to us!");\r
+ docx.getParagraphArray(0).insertNewRun(1).addBreak();\r
+\r
+ // and encrypt it again\r
+ Encryptor e = encInfo.getEncryptor();\r
+ e.confirmPassword("AYBABTU");\r
+ docx.write(e.getDataStream(fs));\r
+ \r
+ fs.close();\r
+ }\r
+ \r
\r
private void listEntry(DocumentNode de, String ext, String path) throws IOException {\r
path += "\\" + de.getName().replace('\u0006', '_');\r