Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
|
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 13th, 2007, 02:34 PM
SV SV is offline
Authorized User
 
Join Date: Oct 2007
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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



 
Old November 14th, 2007, 03:39 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

GZipStream and DeflateStream are different compression methods - they are not compatible.

If you compress using GZipStream then decompress using GZipStream as well.

/- Sam Judson : Wrox Technical Editor -/
 
Old November 14th, 2007, 05:49 PM
SV SV is offline
Authorized User
 
Join Date: Oct 2007
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks S.J. As per your suggestion the following solution worked for me.

        // Read the file we are going to compress into a FileStream
        string filename = Server.MapPath("wizard.zip");

        FileStream infile = File.OpenRead(filename);
        GZipStream gzipStream = new GZipStream(infile, CompressionMode.Decompress);

        byte[] buffer = new byte[infile.Length + 100];

        gzipStream.Read(buffer, 0, (int) infile.Length);
        string x = Encoding.ASCII.GetString(buffer);
 
Old November 15th, 2007, 05:20 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

And also, just so you are aware, there is a difference between a 'zip' file and a file created using the GZipStream compression (technically a GZip file).

Also, GZip and Deflate use the same algorithm to compress data, but GZip includes header details at the start of the data stream, hence why they are not compatible.

http://en.wikipedia.org/wiki/Gzip
http://en.wikipedia.org/wiki/DEFLATE
http://en.wikipedia.org/wiki/ZIP_%28file_format%29

/- Sam Judson : Wrox Technical Editor -/





Similar Threads
Thread Thread Starter Forum Replies Last Post
Win32 Exception not24 C# 1 October 6th, 2007 04:22 PM
Exception Handling NewTitle2007 BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 6 August 8th, 2007 04:03 AM
COM Exception muralidharan.d VS.NET 2002/2003 0 August 7th, 2007 02:49 PM
Read Only Form Is not Read only lryckman Access VBA 3 June 12th, 2007 06:30 AM
Exception Ochi C# 1 January 15th, 2006 11:38 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.