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

The following examples show how to use org.apache.poi.poifs.filesystem.POIFSFileSystem#close() . 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: Poi3Test.java    From easyexcel with Apache License 2.0 6 votes vote down vote up
@Test
public void Encryption() throws Exception {
    String file = TestFileUtil.getPath() + "large" + File.separator + "large07.xlsx";
    POIFSFileSystem fs = new POIFSFileSystem();
    EncryptionInfo info = new EncryptionInfo(EncryptionMode.agile);
    Encryptor enc = info.getEncryptor();
    enc.confirmPassword("foobaa");
    OPCPackage opc = OPCPackage.open(new File(file), PackageAccess.READ_WRITE);
    OutputStream os = enc.getDataStream(fs);
    opc.save(os);
    opc.close();

    // Write out the encrypted version
    FileOutputStream fos = new FileOutputStream("D:\\test\\99999999999.xlsx");
    fs.writeFilesystem(fos);
    fos.close();
    fs.close();

}
 
Example 2
Source File: WriteContextImpl.java    From easyexcel with Apache License 2.0 5 votes vote down vote up
/**
 * To encrypt
 */
private boolean doOutputStreamEncrypt07() throws Exception {
    if (StringUtils.isEmpty(writeWorkbookHolder.getPassword())
        || !ExcelTypeEnum.XLSX.equals(writeWorkbookHolder.getExcelType())) {
        return false;
    }
    if (writeWorkbookHolder.getFile() != null) {
        return false;
    }
    File tempXlsx = FileUtils.createTmpFile(UUID.randomUUID().toString() + ".xlsx");
    FileOutputStream tempFileOutputStream = new FileOutputStream(tempXlsx);
    try {
        writeWorkbookHolder.getWorkbook().write(tempFileOutputStream);
    } finally {
        try {
            writeWorkbookHolder.getWorkbook().close();
            tempFileOutputStream.close();
        } catch (Exception e) {
            if (!tempXlsx.delete()) {
                throw new ExcelGenerateException("Can not delete temp File!");
            }
            throw e;
        }
    }
    POIFSFileSystem fileSystem = null;
    try {
        fileSystem = openFileSystemAndEncrypt(tempXlsx);
        fileSystem.writeFilesystem(writeWorkbookHolder.getOutputStream());
    } finally {
        if (fileSystem != null) {
            fileSystem.close();
        }
        if (!tempXlsx.delete()) {
            throw new ExcelGenerateException("Can not delete temp File!");
        }
    }
    return true;
}
 
Example 3
Source File: HPSFPropertiesOnlyDocument.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Write out, with any properties changes, but nothing else
 */
public void write(File newFile) throws IOException {
    POIFSFileSystem fs = POIFSFileSystem.create(newFile);
    try {
        write(fs);
        fs.writeFilesystem();
    } finally {
        fs.close();
    }
}
 
Example 4
Source File: MSExcelOOXMLSignUtil.java    From hadoopoffice with Apache License 2.0 5 votes vote down vote up
private void signEncryptedPackage(InputStream tmpFileInputStream, SignatureConfig sc, String password) throws IOException, InvalidFormatException, FormatNotUnderstoodException, XMLSignatureException, MarshalException {

	POIFSFileSystem poifsTemp = new POIFSFileSystem(tmpFileInputStream);
	EncryptionInfo info = new EncryptionInfo(poifsTemp);
	Decryptor d = Decryptor.getInstance(info);

	try {
		if (!d.verifyPassword(password)) {
			throw new FormatNotUnderstoodException("Error: Cannot decrypt new Excel file (.xlsx) for signing. Invalid password");
		}
		// signing
		OPCPackage pkg = OPCPackage.open(d.getDataStream(poifsTemp));
		sc.setOpcPackage(pkg);
		
		SignatureInfo si = new SignatureInfo();
		si.setSignatureConfig(sc);
		si.confirmSignature();
		// encrypt again
		Encryptor enc = info.getEncryptor();
		enc.confirmPassword(password);
		POIFSFileSystem poifs = new POIFSFileSystem();
		OutputStream os = enc.getDataStream(poifs);
		pkg.save(os);
		pkg.close();
		if (os!=null) {
			os.close();
		}
		poifs.writeFilesystem(this.finalOutputStream);
		if (poifs!=null) {
			poifs.close();
		}
		if (poifsTemp!=null) {
			poifsTemp.close();
		}
	} catch (GeneralSecurityException e) {
		
		LOG.error(e);
		throw new FormatNotUnderstoodException("Error: Cannot decrypt new Excel file (.xlsx)  for signing.");
	} 
}
 
Example 5
Source File: MppDump.java    From mpxj with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * This method opens the input and output files and kicks
 * off the processing.
 *
 * @param input Name of the input file
 * @param output Name of the output file
 * @throws Exception Thrown on file read errors
 */
private static void process(String input, String output) throws Exception
{
   FileInputStream is = new FileInputStream(input);
   PrintWriter pw = new PrintWriter(new FileWriter(output));

   POIFSFileSystem fs = new POIFSFileSystem(is);
   dumpTree(pw, fs.getRoot(), "", true, true, null);

   is.close();
   pw.flush();
   pw.close();
   fs.close();
}
 
Example 6
Source File: POIFSLister.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public static void viewFileOld(final String filename, boolean withSizes) throws IOException {
   POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(filename));
   displayDirectory(fs.getRoot(), "", withSizes);
   fs.close();
}
 
Example 7
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 8
Source File: HSSFWorkbook.java    From lams with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Method write - write out this workbook to a new {@link File}. Constructs
 * a new POI POIFSFileSystem, passes in the workbook binary representation and
 * writes it out. If the file exists, it will be replaced, otherwise a new one
 * will be created.
 * 
 * Note that you cannot write to the currently open File using this method.
 * If you opened your Workbook from a File, you <i>must</i> use the {@link #write()}
 * method instead!
 * 
 * @param newFile The new File you wish to write the XLS to
 *
 * @exception IOException if anything can't be written.
 * @see org.apache.poi.poifs.filesystem.POIFSFileSystem
 */
@Override
public void write(File newFile) throws IOException {
    POIFSFileSystem fs = POIFSFileSystem.create(newFile);
    try {
        write(fs);
        fs.writeFilesystem();
    } finally {
        fs.close();
    }
}