title: Apache POI fails to save (HWPFDocument.write) large word doc files
body: <p>I want to remove word metadata from .doc files. My <code>.docx</code> files works fine with <code>XWPFDocument</code>, but the following code for removing metadata fails for large (> 1MB) files. For example using a 6MB .doc file with images, it outputs a 4.5MB file in which some images are removed.</p>
  
  <pre><code>public static InputStream removeMetaData(InputStream inputStream) throws IOException {
      POIFSFileSystem fss = new POIFSFileSystem(inputStream);
      HWPFDocument doc = new HWPFDocument(fss);
  
      // **it even fails on large files if you remove from here to 'until' below**
      SummaryInformation si = doc.getSummaryInformation();
      si.removeAuthor();
      si.removeComments();
      si.removeLastAuthor();
      si.removeKeywords();
      si.removeSubject();
      si.removeTitle();
  
      doc.getDocumentSummaryInformation().removeCategory();
      doc.getDocumentSummaryInformation().removeCompany();
      doc.getDocumentSummaryInformation().removeManager();
      try {
          doc.getDocumentSummaryInformation().removeCustomProperties();
      } catch (Exception e) {
          // can not remove above
      }
      // until
  
      ByteArrayOutputStream os = new ByteArrayOutputStream();
      doc.write(os);
      os.flush();
      os.close();
      return new ByteArrayInputStream(os.toByteArray());
  }
  </code></pre>
  
  <p>Related posts:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/8484906/how-to-save-the-word-document-using-poi-api">How to save the Word Document using POI API?</a></li>
  <li><a href="https://stackoverflow.com/questions/9758955/saving-poi-document-correctly">https://stackoverflow.com/questions/9758955/saving-poi-document-correctly</a></li>
  </ul>