title: Apache POI - reading modifies excel file
body: <p>Whenever I open a excel file using the Apatche POI the file gets modified, even though I'm just reading the file and not making any modification.</p>
  
  <p>Take for instance such test code.</p>
  
  <pre><code>public class ApachePoiTest {
  
      @Test
      public void readingShouldNotModifyFile() throws Exception {
          final File testFile = new File("C:/work/src/test/resources/Book2.xlsx");
          final byte[] originalChecksum = calculateChecksum(testFile);
          Assert.assertTrue("Calculating checksum modified file",
              MessageDigest.isEqual(originalChecksum, calculateChecksum(testFile)));
          try (Workbook wb = WorkbookFactory.create(testFile)) {
              Assert.assertNotNull("Reading file with Apache POI", wb);
          }
          Assert.assertTrue("Reading file with Apache POI modified file",
              MessageDigest.isEqual(originalChecksum, calculateChecksum(testFile)));
      }
  
      @Test
      public void readingInputStreamShouldNotModifyFile() throws Exception {
          final File testFile = new File("C:/work/src/test/resources/Book2.xlsx");
          final byte[] originalChecksum = calculateChecksum(testFile);
          Assert.assertTrue("Calculating checksum modified file",
              MessageDigest.isEqual(originalChecksum, calculateChecksum(testFile)));
          try (InputStream is = new FileInputStream(testFile); Workbook wb = WorkbookFactory.create(is)) {
              Assert.assertNotNull("Reading file with Apache POI", wb);
          }
          Assert.assertTrue("Reading file with Apache POI modified file",
              MessageDigest.isEqual(originalChecksum, calculateChecksum(testFile)));
      }
  
      private byte[] calculateChecksum(final File file) throws Exception {
          final MessageDigest md = MessageDigest.getInstance("MD5");
          md.reset();
          try (InputStream is = new FileInputStream(file)) {
              final byte[] bytes = new byte[2048];
              int numBytes;
              while ((numBytes = is.read(bytes)) != -1) {
                  md.update(bytes, 0, numBytes);
              }
              return md.digest();
          }
      }
  }
  </code></pre>
  
  <p>Test <code>readingShouldNotModifyFile</code> always fails, because the file gets always modified by Apache POI. More to it when testing on a blank excel file freshly created with MS Office, Apache POI cuts the file from 8.1 kb to 6.2 kb and corrupts the file.</p>
  
  <p>Tested with:</p>
  
  <pre><code>&lt;dependency&gt;
      &lt;groupId&gt;org.apache.poi&lt;/groupId&gt;
      &lt;artifactId&gt;poi-ooxml&lt;/artifactId&gt;
      &lt;version&gt;3.15&lt;/version&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p>and also with version 3.12</p>
  
  <p>Can I prevent Apache POI from modifying my files by other means then passing <code>InputStream</code> instead of <code>File</code>. I don't want to pass <code>InputStream</code> because I'm concerned about Apache's warning that it takes more memory and has some specific requirements to the <code>InputStream</code>.</p>