Extracting to variable

Oct 15, 2010 at 12:55 PM

Hello,

I have been trying to work out how to extract a file to a variable rather than creating a file on the hard drive.

Specifically, I am trying to read the document.xml file from within a .docx file and I only want to process the file and not store it.

I am using C#. Is there a method for this? I have seen some sample code on a website that uses ionic.utils.zip.ZipFile.Extract(filepath, stream). Is this no longer available?

Coordinator
Oct 15, 2010 at 1:10 PM

There are two ways to do what you want.

ZipEntry.OpenReader() and ZipEntry.Extract(Stream).  Using the former, you can just read into your own buffer.  If you use the latter, you can extract to a MemoryStream, then examine the buffer associated to the MemoryStream.

ps: yes, the ZipFile.Extract() methods have been removed as an unnecessary duplication.  The Extract() methods are now present on the ZipEntry class.  If you point me to the specific sample code on the DotNetZip website that uses ZipFile.Extract(), I'll fix it. 

 

Oct 15, 2010 at 1:30 PM

Thanks Cheeso thats exactly what I need.

I'm not a great programmer though and still having some problems working out how to get my ZipEntry from the ZipFile. I have opened the zip file with ZipFile.Read(string), but am not sure how to search the collection of entries in the ZipFile object for the document.xml file. Please help.

Oct 15, 2010 at 1:46 PM

think i've got it but havent tested it:

using

(ZipFile zip = ZipFile.Read(filename))

 ZipEntry entry = zip.Single();

 MemoryStream stream = new MemoryStream();

entry.Extract(stream);

}

{

 ICollection<ZipEntry> entries = zip.SelectEntries("name = document.xml");

Oct 15, 2010 at 1:48 PM

That did not come out how i pasted it:

using (ZipFile zip = ZipFile.Read(filename))

{

   ICollection<ZipEntry> entries = zip.SelectEntries("name = document.xml");

   ZipEntry entry = zip.Single();

   MemoryStream stream = new MemoryStream();

   entry.Extract(stream);

}

Coordinator
Oct 18, 2010 at 9:28 PM

ok, that looks reasonable.  (If you are selecting on zipentry name only, rather than use the ZipFile.SelectEntries() method,  you can alternatively use the string indexer on ZipFile.  zip["document.xml"]. )

Once  you've extracted the entry to the MemoryStream, you can examine the contents or do whatever else you like with the data.