User (Old forums)MemberFebruary 6, 2007 at 9:28 amPost count: 23064
I am getting an “Expected end of stream not found” exception when I run the following code. I am trying to open a zipped file, unzip it with a stream, and them write it back to disk. I am using the CompressedStream class because I will eventually be doing this with MemoryStreams instead of files (currently using MemoryStream throws the same exception, its just easier to demonstrate with files). The exception is thrown on the first stream.Read. Any insight would be appreciated.
FileStream fileIn = new FileStream(“c:\\temp.zip”, FileMode.Open, FileAccess.Read);
FileStream fileOut = new FileStream(“c:\\test.pdf”, FileMode.Create, FileAccess.Write);
CompressedStream stream = new CompressedStream(fileIn);
byte b = new byte;
fileIn.Position = 0;
while ((len = stream.Read(b, 0, b.Length)) > 0)
fileOut.Write(b, 0, len);
The file was zipped with winzip 9
Imported from legacy forums. Posted by egenius (had 3094 views)User (Old forums)MemberFebruary 7, 2007 at 2:57 pmPost count: 23064
CompressedStream is a compression class use to compress/uncompress data, it is not equivalent to the Zip File format. It simply output the data, with no formating (or minimal formating in cases of FormattedCompressedStream classes).
The Zip File format will create a file that has local headers for each file in the archive, and a central directory containing information on the where to find each local header in the archive, the name of each file, etc…
So to unzip a file within an archive, you need to use class that can handle this format. These classes are ZipArchive, ZippedFile and ZippedFolder. In your case, you would probably do something like the following :
AbstractFile source = new DiskFile( “C:\temp.zip” );
//I assume the file you want to unzip within the zip file is test.pdf
ZippedFile fileWithinArchive = new ZippedFile( source, “\test.pdf” );
using( MemoryStream destStream = new MemoryStream() )
using( Stream stream = fileWithinArchive.OpenRead() )
byte buffer = new byte[ 400 ];
int bytesRead = 0;
//read the stream form the zipped file within the archive
while( ( bytesRead = stream.Read( buffer, 0, buffer.Length ) ) > 0 )
destStream.Write( buffer, 0, bytesRead );
Imported from legacy forums. Posted by André (had 207 views)User (Old forums)MemberFebruary 7, 2007 at 3:39 pmPost count: 23064
Thanks for the reply. Let me explain a little more about what I am trying to. We use a third party fax service. When we receive our faxes from their webservice the format is a byte array of a zipped pdf. What I would like to do is unzip the pdf without ever having to write to or read from a physical file (i.e. create a MemoryStream from the byte array and apply the decompression to that). Is there a way to accomplish this with your software.
Imported from legacy forums. Posted by egenius (had 221 views)User (Old forums)MemberFebruary 8, 2007 at 2:35 pmPost count: 23064
You can use a MemoryFile, open a writing stream on it, then write the byte array to it using the stream. Then do the same as I suggested.
AbstractFile zip = new MemoryFile();
using( Stream archiveStream = zip.OpenWrite( true ) )
archiveStream.Write( byteArrayFromFax, 0, byteArrayFromFax.Length );
ZippedFile fileWihtinArchive = new ZippedFile( zip, @”\test.pdf” );
using( MemoryStream uncompressedStream = new MemoryStream() )
using( Stream zippedFileStream = fileWihtinArchive.OpenRead() )
byte buffer = new byte[ 32768 ];
int bytesRead = 0;
while( ( bytesRead = zippedFileStream.Read( buffer, 0, buffer.Length ) ) > 0 )
uncompressedStream.Write( buffer, 0, bytesRead );
//do what you need to with the uncompressed stream
Imported from legacy forums. Posted by André (had 4097 views)
- You must be logged in to reply to this topic.