poi-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Matt Rogghe <mrog...@blazent.com>
Subject RE: Closing an OPCPackage object
Date Tue, 03 Aug 2010 18:03:15 GMT
I indicated the wrong builder method previously.  I use the 'open(java.lang.String path, PackageAccess
access)' method.  This is the only open method which contains an option to control the package
access method.

In the first code example below, if you uncomment the .revert() line, the delete works.  When
it is commented the file will not be deleted.

In the second code example I changed the code to use a buffered input stream rather than the
String and package access options.  The second option works as is.

Code #1:

import java.io.File;
import java.io.IOException;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackageAccess;


public class OPCError 
{
	public static void main (String args[]) throws InvalidFormatException, IOException
	{
		File fileToProcess = new File("F:\\temp\\opcerror.xlsx");
		
		OPCPackage xlsxPackage = OPCPackage.open(fileToProcess.getAbsolutePath(), PackageAccess.READ);
		
		xlsxPackage.close();
//		xlsxPackage.revert();
		
		if (fileToProcess.delete())
			System.out.println("Deleted");
		else
			System.out.println("Not deleted");
	}
}

-------------------------------------------------------------------------------------------

Code #2

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;

public class OPCError 
{
	public static void main (String args[]) throws InvalidFormatException, IOException
	{
		File fileToProcess = new File("F:\\temp\\opcerror.xlsx");
		InputStream bis = new BufferedInputStream(new FileInputStream(fileToProcess));
		
		OPCPackage xlsxPackage = OPCPackage.open(bis);
		
		xlsxPackage.close();
//		xlsxPackage.revert();
		
		if (fileToProcess.delete())
			System.out.println("Deleted");
		else
			System.out.println("Not deleted");
	}
}

-----Original Message-----
From: Nick Burch [mailto:nick.burch@alfresco.com] 
Sent: Tuesday, August 03, 2010 12:12 PM
To: POI Users List
Subject: Re: Closing an OPCPackage object

On Mon, 2 Aug 2010, Matt Rogghe wrote:
> After playing around for a while and making sure all input/output 
> streams and buffers have been closed gracefully and the package object 
> is also closed gracefully, I decided to use the .revert() method of the 
> OPCPackage class in addition to the close() method.

close should call closeImpl if it was opened from a file, which ought to 
close down the input source too

Are you creating your opcpackage from an InputStream or a File?

Nick

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Mime
View raw message