Using DeflateStream.Read gives exception
I am getting InvalidDataException: Unknown block type. Stream might be corrupted exception.
I created a zip file using the following code:
string filename = Server.MapPath("wizard.txt");
FileStream infile = File.OpenRead(filename);
byte[] buffer = new byte[infile.Length];
infile.Read(buffer,0, (int) infile.Length);
infile.Close();
FileStream outfile = File.Create(
Path.ChangeExtension(filename,"zip"));
GZipStream gzipStream =
new GZipStream(outfile, CompressionMode.Compress);
gzipStream.Write(buffer, 0, buffer.Length);
gzipStream.Close();
I unzip the zipped file using the following code:
string filename = Server.MapPath("wizard.zip");
FileStream infile = File.OpenRead(filename);
DeflateStream deflateStream =
new DeflateStream(infile, CompressionMode.Decompress);
byte[] buffer = new byte[infile.Length + 100];
int offset = 0;
int totalCount = 0;
while (true)
{
int bytesRead = deflateStream.Read(buffer, 0, 100);
if (bytesRead == 0)
{ break; }
offset += bytesRead;
totalCount += bytesRead;
}
FileStream outfile = File.Create(
Path.ChangeExtension(filename, "txt"));
outfile.Write(buffer, 0, buffer.Length);
outfile.Close();
I get the following Exception. Any advice?
Stack Trace:
[InvalidDataException: Unknown block type. Stream might be corrupted.]
System.IO.Compression.Inflater.Decode() +407
System.IO.Compression.Inflater.Inflate(Byte[] bytes, Int32 offset, Int32 length) +127
System.IO.Compression.DeflateStream.Read(Byte[] array, Int32 offset, Int32 count) +134
_Default.Page_Load(Object sender, EventArgs e) in c:\Surinder\Book Notes\ASPNet\Ch24\21\Default.aspx.cs:28
System.Web.Util.CalliHelper.EventArgFunctionCaller (IntPtr fp, Object o, Object t, EventArgs e) +13
System.Web.Util.CalliEventHandlerDelegateProxy.Cal lback(Object sender, EventArgs e) +45
System.Web.UI.Control.OnLoad(EventArgs e) +80
System.Web.UI.Control.LoadRecursive() +49
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3745
|