I've just tried the following code for decompressing a .zip file, but get the following error message:
"The magic number in GZip header is not correct. Make sure you are passing in a GZip stream."
The error is on line 27: "int bytesRead = compressedZipStream.Read(smallBuffer, 0, 100);"
My code is as follows:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.IO.Compression;
namespace GZipText_decompression
{
class decompressionTest
{
static void Main(string[] args)
{
FileStream inputFile= null;
GZipStream compressedZipStream = null;
try
{
//determine the uncompressed size of the file
inputFile = new FileStream("c:\\translated_catalog.zip", FileMode.Open, FileAccess.Read, FileShare.Read);
compressedZipStream = new GZipStream(inputFile, CompressionMode.Decompress);
int offSet = 0;
int totalBytes = 0;
byte[] smallBuffer = new byte[100];
while (true)
{
int bytesRead = compressedZipStream.Read(smallBuffer, 0, 100);
if (bytesRead == 0)
{
break;
}
offSet += bytesRead;
totalBytes += bytesRead;
}
compressedZipStream.Close();
//open and read contents of the file no that the uncompressed size is known
inputFile = new FileStream("c:\\translated_catalog.zip", FileMode.Open, FileAccess.Read, FileShare.Read);
//Decompress the file contents
compressedZipStream = new GZipStream(inputFile, CompressionMode.Decompress);
byte[] buffer = new byte[totalBytes];
compressedZipStream.Read(buffer, 0, totalBytes);
//Display decompression information
Console.WriteLine(Encoding.UTF7.GetString(buffer)) ;
Console.WriteLine("Compressed size is {0} and decompressed size is {1} ", inputFile.Length, buffer.Length);
compressedZipStream.Close();
Console.ReadLine();
}
finally
{
if (inputFile != null)
{
inputFile.Close();
}
if (compressedZipStream != null)
{
compressedZipStream.Close();
}
}
}
}
}
The file that I'm decompressing is a .zip file.
Any ideas?