Java Code Examples for org.apache.poi.poifs.filesystem.POIFSFileSystem#getRoot()

The following examples show how to use org.apache.poi.poifs.filesystem.POIFSFileSystem#getRoot() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HSSFWorkbook.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Adds an OLE package manager object with the given POIFS to the sheet
 *
 * @param poiData an POIFS containing the embedded document, to be added
 * @param label the label of the payload
 * @param fileName the original filename
 * @param command the command to open the payload
 * @return the index of the added ole object
 * @throws IOException if the object can't be embedded
 */
public int addOlePackage(POIFSFileSystem poiData, String label, String fileName, String command)
throws IOException {
	DirectoryNode root = poiData.getRoot();
	Map<String,ClassID> olemap = getOleMap();
	for (Map.Entry<String,ClassID> entry : olemap.entrySet()) {
		if (root.hasEntry(entry.getKey())) {
			root.setStorageClsid(entry.getValue());
			break;
		}
	}

	ByteArrayOutputStream bos = new ByteArrayOutputStream();
	poiData.writeFilesystem(bos);
    return addOlePackage(bos.toByteArray(), label, fileName, command);
}
 
Example 2
Source File: Poi3Test.java    From easyexcel with Apache License 2.0 5 votes vote down vote up
@Test
public void Encryption2() throws Exception {
    Biff8EncryptionKey.setCurrentUserPassword("123456");
    POIFSFileSystem fs = new POIFSFileSystem(new File("d:/test/simple03.xls"), true);
    HSSFWorkbook hwb = new HSSFWorkbook(fs.getRoot(), true);
    Biff8EncryptionKey.setCurrentUserPassword(null);
    System.out.println(hwb.getSheetAt(0).getSheetName());

}
 
Example 3
Source File: WordTableCellContentOleObject.java    From sun-wordtable-read with Apache License 2.0 5 votes vote down vote up
/**
	 * 读取文档类的OLE对象,包括Docx、Doc、xlsx、xls、ppt、pptx等
	 * 暂未实现,需要进行大量资料查阅、寻找解决方案
	 * @param poifs
	 * @return
	 */
	private WcOleObject readDocumentOle(POIFSFileSystem poifs, InputStream is) {
		DirectoryNode directory = poifs.getRoot();
		if (!directory.hasEntry("WordDocument")) {
			return null;
		}
		
		List strings = POIFSViewEngine.inspectViewable(poifs, true, 0, "  ");
		Iterator iter = strings.iterator();

		while (iter.hasNext()) {
			//os.write( ((String)iter.next()).getBytes());
			System.out.println(iter.next());
		}
		throw new NotImplementedException("暂未实现");
		
		
//		WcOleObject oleObject = new WcOleObject();
//		try {
//			DocumentNode entry = (DocumentNode) directory.getEntry("WpsCustomData");
//			byte[] data = new byte[entry.getSize()];
//			directory.createDocumentInputStream(entry).read(data);
//			
//			XWPFDocument doc = new XWPFDocument(directory.createDocumentInputStream(entry)); // 载入文档  
//			doc.toString();
//			oleObject.setFileName(FilenameUtils.getName(entry.getName()));
//			//
//			// //byte[] data =
//			// IOUtils.toByteArray(packagePart.getInputStream());
//			oleObject.setDataSize(data.length);
//			oleObject.setData(data);
//		} catch (Exception e) {
//			logger.error(e.getMessage(), e);
//		}

		//return oleObject;
	}
 
Example 4
Source File: CIFSContentComparator.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
private boolean isContentIdentical(POIFSFileSystem fs1, POIFSFileSystem fs2, Collection<String> excludes) throws IOException
{
    DirectoryEntry de1 = fs1.getRoot();
    DirectoryEntry de2 = fs2.getRoot();

    FilteringDirectoryNode fs1Filtered = new FilteringDirectoryNode(de1, excludes);
    FilteringDirectoryNode fs2Filtered = new FilteringDirectoryNode(de2, excludes);

    boolean retVal = EntryUtils.areDirectoriesIdentical(fs1Filtered, fs2Filtered);
    if(logger.isDebugEnabled())
    {
        logger.debug("returning equal="+ retVal);
    }
    return retVal;
}
 
Example 5
Source File: MPPReader.java    From mpxj with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * This method allows us to peek into the OLE compound document to extract the file format.
 * This allows the UniversalProjectReader to determine if this is an MPP file, or if
 * it is another type of OLE compound document.
 *
 * @param fs POIFSFileSystem instance
 * @return file format name
 * @throws IOException
 */
public static String getFileFormat(POIFSFileSystem fs) throws IOException
{
   String fileFormat = "";
   DirectoryEntry root = fs.getRoot();
   if (root.getEntryNames().contains("\1CompObj"))
   {
      CompObj compObj = new CompObj(new DocumentInputStream((DocumentEntry) root.getEntry("\1CompObj")));
      fileFormat = compObj.getFileFormat();
   }
   return fileFormat;
}
 
Example 6
Source File: EncryptionInfo.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Opens for decryption
 */
public EncryptionInfo(POIFSFileSystem fs) throws IOException {
   this(fs.getRoot());
}
 
Example 7
Source File: ExcelExtractor.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public ExcelExtractor(POIFSFileSystem fs) throws IOException {
	this(fs.getRoot());
}
 
Example 8
Source File: EventBasedExcelExtractor.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public EventBasedExcelExtractor(POIFSFileSystem fs) {
   this(fs.getRoot());
   super.setFilesystem(fs);
}
 
Example 9
Source File: OLE2Bleach.java    From DocBleach with MIT License 4 votes vote down vote up
protected void sanitize(BleachSession session, POIFSFileSystem fsIn, POIFSFileSystem fs) {
  DirectoryEntry rootIn = fsIn.getRoot();
  DirectoryEntry rootOut = fs.getRoot();

  sanitize(session, rootIn, rootOut);
}
 
Example 10
Source File: ProjectCleanUtility.java    From mpxj with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Process a project file to make it anonymous.
 *
 * @param input input file name
 * @param output output file name
 */
private void processMPP(String input, String output) throws IOException
{
   String varDataFileName;
   String projectDirName;
   int mppFileType = NumberHelper.getInt(m_project.getProjectProperties().getMppFileType());
   switch (mppFileType)
   {
      case 8:
      {
         projectDirName = "   1";
         varDataFileName = "FixDeferFix   0";
         break;
      }

      case 9:
      {
         projectDirName = "   19";
         varDataFileName = "Var2Data";
         break;
      }

      case 12:
      {
         projectDirName = "   112";
         varDataFileName = "Var2Data";
         break;
      }

      case 14:
      {
         projectDirName = "   114";
         varDataFileName = "Var2Data";
         break;
      }

      default:
      {
         throw new IllegalArgumentException("Unsupported file type " + mppFileType);
      }
   }

   //
   // Load the raw file
   //
   FileInputStream is = new FileInputStream(input);
   POIFSFileSystem fs = new POIFSFileSystem(is);
   is.close();

   //
   // Locate the root of the project file system
   //
   DirectoryEntry root = fs.getRoot();
   m_projectDir = (DirectoryEntry) root.getEntry(projectDirName);

   //
   // Process Tasks
   //
   processFile((DirectoryEntry) m_projectDir.getEntry("TBkndTask"), varDataFileName, m_project.getTasks(), true, TaskField.NAME);

   //
   // Process Resources
   //
   processFile((DirectoryEntry) m_projectDir.getEntry("TBkndRsc"), varDataFileName, m_project.getResources(), true, ResourceField.NAME, ResourceField.INITIALS);

   //
   // Process project properties
   //
   List<ProjectProperties> projectProperties = Arrays.asList(m_project.getProjectProperties());

   processFile(m_projectDir, "Props", projectProperties, true, PROJECT_FIELDS);
   processFile(root, "\005SummaryInformation", projectProperties, false, PROJECT_FIELDS);
   processFile(root, "\005DocumentSummaryInformation", projectProperties, false, PROJECT_FIELDS);

   //
   // Write the replacement raw file
   //
   FileOutputStream os = new FileOutputStream(output);
   fs.writeFilesystem(os);
   os.flush();
   os.close();
   fs.close();
}
 
Example 11
Source File: POIDocument.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Constructs from the default POIFS
 * 
 * @param fs the filesystem the document is read from
 */
protected POIDocument(POIFSFileSystem fs) {
    this(fs.getRoot());
 }
 
Example 12
Source File: HSSFWorkbook.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Given a POI POIFSFileSystem object, read in its Workbook and populate
 * the high and low level models.  If you're reading in a workbook... start here!
 *
 * @param fs the POI filesystem that contains the Workbook stream.
 * @param preserveNodes whether to preserve other nodes, such as
 *        macros.  This takes more memory, so only say yes if you
 *        need to. If set, will store all of the POIFSFileSystem
 *        in memory
 * @see org.apache.poi.poifs.filesystem.POIFSFileSystem
 * @exception IOException if the stream cannot be read
 */
public HSSFWorkbook(POIFSFileSystem fs, boolean preserveNodes)
        throws IOException {
    this(fs.getRoot(), fs, preserveNodes);
}