0

Hello I am trying to compress a file using GZipStream.

I have created my own extension, let's call it .myextension

I try to compress .myextension and keep its extension. I mean that I am trying to compress a .myextension to the same extension. Example: I have myfile.myextension and I want to compress it to myfile.myextension. It works. I can compress my file really well.

The problem is that when I try to decompress it using GZipStream it says that the magic number is incorrect.

How can I fix that? When decompressing should I just change the extension to .gz? Should I convert it somehow? Please help me I have no idea how to continue.

morcillo
  • 1,091
  • 5
  • 19
  • 51
  • 2
    This has nothing to do with the file extension. Can you decompress the compressed file using an application such as 7-zip? If yes, your decompress code probably contains an error; please post the code. – Yogu Apr 15 '12 at 20:54
  • 2
    It has nothing to do with the filename extension. You'll need to leave better breadcrumbs. – Hans Passant Apr 15 '12 at 20:55
  • extension is not a problem at all,do you read file to string before decompression instead of byte[]? show some code of decompression – Arsen Mkrtchyan Apr 15 '12 at 20:57
  • I use this code I found on msdn. I'm using it to learn how compressing and decompressing works with C#. After that I am going to compress and dfecompress my files my own way. http://msdn.microsoft.com/en-us/library/system.io.compression.gzipstream.aspx – morcillo Apr 15 '12 at 21:02
  • Please provide the first 20 bytes of the file you wrote in hex. – Mark Adler Apr 16 '12 at 04:00

2 Answers2

1

This is a common question. I would like to provide you the similar threads with the solutions:

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=427166&SiteID=1

A 'Magic Number' is usually a fixed value, and often appearing somewhat arbitrary, possibly indecipherable. For example, a line of code may have:

If X = 31 then
    'Do Something
End If

In this case, 31 is a 'Magic Number': It has no obvious meaning (and as far as coding is concerned, a term of derision).

Files (of different types) often have the first few bytes set to certain values, for example, a file which has the first two bytes as then hexadecimal numbers 42 4D is a Bitmap file. These numbers are 'magic numbers' (In this case, 42 4D corresponds to the characters BM). Other files have similar 'magic numbers'.

http://forums.microsoft.com/msdn/showpost.aspx?postid=1154042&siteid=1

Of course, the minute someone (team) develops a no-fuss compression/decompression custom task which supports zip,bzip2, gzip, rar, cab, jar, data and iso files, I'll use that, until that time, I'll stick with the open-source command-line utilities.

Of course, you can code up a solution, but this one is such low hanging fruit. For handling zip files, there is no native .NET library (at least not yet). Now there is support is for handling the compressed streams INSIDE the zip file, but not navigating the archive itself.

Now, as I mentioned in a previously, there are plenty of open-source zip utils like those on Sourceforge. These work fine on Win2003 Server x64, I can attest to that.

However, if you're insistent on a .NET solution for zip decompression, use http://www.icsharpcode.net/OpenSource/SharpZipLib/, which is open source, and which has a clean and reliable 100% .NET implementation.

malik masis
  • 487
  • 1
  • 6
  • 15
0

First off, from other users who have had various issues, GZipStream should not be used since it has bugs. It does not compress short strings correctly and it does not detect corrupted compressed data. It is a very poor implementation.

As for your problem, others using GZipStream see a four-byte prefix to the gzip data which is the number of uncompressed bytes. If that is written to the file, that would cause the problem you are seeing. The gzip file should start with the hex bytes 1f 8b.

Community
  • 1
  • 1
Mark Adler
  • 101,978
  • 13
  • 118
  • 158