473,785 Members | 3,245 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

GZipStream Decompression Failure

Hi,

I have been using the new GZipStream classes, and have been experiencing
problems when attemping to decompress files, which from experience, seem to
be failing when the original file size exceeds something like 64 MB.

For example, when I attempt to decompress a text file of size 1.18 MB to its
original size of 106 MB, I receive the following error message:

System.IO.IOExc eption: Insufficient system resources exist to complete the
requested service.
Source: mscorlib
StackTrace: " at System.IO.__Err or.WinIOError(I nt32 errorCode, String
maybeFullPath)\ r\n at System.IO.FileS tream.WriteCore (Byte[] buffer, Int32
offset, Int32 count)\r\n at System.IO.FileS tream.Write(Byt e[] array, Int32
offset, Int32 count)\r\n at %MethodName% in %SourceFilePath %:line 96"
TargetSite: {Void WinIOError(Int3 2, System.String)}

However, it works perfectly fine for files of an original size which is,
approximately, less than 64 MB in size.

Can anyone kindly provide any assistance as to whether there is a solution,
or whether there is just a flaw in the class?

Thanks
Aug 30 '07 #1
6 7454
On Aug 30, 11:44 am, BDRichardson
<BDRichard...@d iscussions.micr osoft.comwrote:
I have been using the new GZipStream classes, and have been experiencing
problems when attemping to decompress files, which from experience, seem to
be failing when the original file size exceeds something like 64 MB.
<snip>
Can anyone kindly provide any assistance as to whether there is a solution,
or whether there is just a flaw in the class?
Well, you haven't shown any of the code you're using to decompress.

Could you produce a short but complete program which demonstrates the
problem?
See http://pobox.com/~skeet/csharp/complete.html for what I mean by
that.

Jon

Aug 30 '07 #2
The Method:

public static void DecompressFile( FileInfo FileToDecompres s, String
DestinationDire ctory, Boolean DeleteOriginalF ile)
{
FileStream fsSource = null;
FileStream fsDestination = null;
GZipStream compressedStrea m = null;

try
{
Byte[] buffer;

fsSource = new FileStream( FileToDecompres s.FullName, FileMode.Open,
FileAccess.Read , FileShare.Read) ;

// The original file size may be obtained from the footer of the
compressed file
buffer = new Byte[ 4];
fsSource.Positi on = Convert.ToInt32 ( fsSource.Length ) - 4;
fsSource.Read( buffer, 0, 4);
Int32 _OriginalFileSi ze = BitConverter.To Int32( buffer, 0);

// Read the decompressed file contents
buffer = new Byte[ _OriginalFileSi ze];
fsSource.Positi on = 0;

compressedStrea m = new GZipStream( fsSource, CompressionMode .Decompress,
true);
compressedStrea m.Read( buffer, 0, _OriginalFileSi ze);
compressedStrea m.Flush();
compressedStrea m.Close();

// Write the decompressed data to a new file
String _OriginalFileNa me = FileToDecompres s.Name.Substrin g( 0,
FileToDecompres s.Name.Length - 5);
fsDestination = new FileStream( DestinationDire ctory +
Path.DirectoryS eparatorChar + _OriginalFileNa me, FileMode.Create ,
FileAccess.Writ e, FileShare.Write );
fsDestination.W rite( buffer, 0, _OriginalFileSi ze);
fsDestination.C lose();
fsSource.Close( );

System.Diagnost ics.Debug.Write Line( String.Format( "The file {0} was
decompressed as {1}.", FileToDecompres s.Name, _OriginalFileNa me));

if( DeleteOriginalF ile)
{
FileToDecompres s.Delete();
System.Diagnost ics.Debug.Write Line( String.Format( "The following file
was deleted: {0}", FileToDecompres s.Name));
}
}
catch( System.Exceptio n ex)
{
System.Diagnost ics.Debug.Write Line( String.Format( "The following
exception occurred within the application:\n{ 0}", ex.Message));
}
finally
{
if( fsSource != null) fsSource.Close( );
if( compressedStrea m != null)
{
compressedStrea m.Flush();
compressedStrea m.Close();
}

if( fsDestination != null) fsDestination.C lose();
}
}

Aug 30 '07 #3
On Aug 30, 12:08 pm, BDRichardson
<BDRichard...@d iscussions.micr osoft.comwrote:
The Method:
Please see http://pobox.com/~skeet/csharp/incomplete.html

However, it looks to me like the problem is that you're trying to read
the whole thing in one go. Aside from anything else, this is
horrendously inefficient in terms of memory. It's much better to use a
buffer, read into that, write from it, then repeat until you're done.

I've got a method in my MiscUtil library for copying a whole stream.
See
http://pobox.com/~skeet/csharp/miscutil

It won't *quite* work out of the box in this case because you've
appended the original size to the end of the compressed stream. Is
this absolutely required? If you really need the information, could
you not put it at the *start* of the stream rather than the end? (That
way it can be skipped over very easily, rather than giving a
compressed stream which has invalid data at the end.)

Jon

Aug 30 '07 #4
Yowser! Buffer bomb!

You shouldn't be allocating a buffer for the entire file (which could
be huge), but rather just create a small buffer and loop over the
data - something like (untested):

static void Main() {
using(Stream inFile = File.OpenRead(" in.gzip"))
using(GZipStrea m zip = new GZipStream(inFi le,
CompressionMode .Decompress))
using (Stream outFile = File.OpenWrite( "out.whatever") ) {
CopyStream(zip, outFile);
outFile.Close() ;
}
}
static long CopyStream(Stre am source, Stream destination) {
if (source == null) throw new ArgumentNullExc eption("source" );
if (!source.CanRea d) throw new ArgumentExcepti on("Cannot read
from source");
if (destination == null) throw new
ArgumentNullExc eption("destina tion");
if (!destination.C anWrite) throw new ArgumentExcepti on("Cannot
write to destination");
const int BUFFER_SIZE = 4096;
byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead;
long totalBytesRead = 0;
while ((bytesRead = source.Read(buf fer, 0, BUFFER_SIZE)) 0)
{
destination.Wri te(buffer, 0, bytesRead);
totalBytesRead += bytesRead;
}
destination.Flu sh();
return totalBytesRead;
}

Marc
Aug 30 '07 #5
KABOOOOM!

OK, shoot me now!

I'm sure its not that obvious that I'm new to Streams... yeah right!
Although I didn't & don't totally understand streams, I do recall when I
first started carving the code, I suspected that it might be dumping the
whole buffer at once. Obvious really when you take a careful look at the
code!

I've altered it slightly so that it now only reads and writes a small buffer
at a time, and it works a treat.

Many thanks guys, you've given me the slap I needed!
Aug 30 '07 #6
[being new to streams]
Please rest assured that I also learnt this the hard way. No doubt Jon
or one of the other stalwarts set me on the right path - sorry if it
sounded condascending, it wasn't my intent - just to stress that a
radical change of direction was needed ;-p
Many thanks guys, you've given me the slap I needed!
No problem

Marc

Aug 30 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2195
by: Craig Buchanan | last post by:
when i try to open a file that has been compressed with GZipStream (source from microsoft's 101 Samples for Visual Studio 2005), i get an "invalid archive" message from winzip, zipgenius and xp's compressed folder app. using DeflateStream resulted in the same error. am i missing something simple? thanks, Craig Buchanan
1
3091
by: Robinson | last post by:
I'm trying to work out how to decompress a GZipStream. I'm not sure how to allocate the destination buffer, given that I cannot query the stream to find out the total decompressed size of the item. A hack here http://www.geekpedia.com/tutorial191_Unzipping-compressed-files-using-GZipStream.html shows that you can get the last 4 bytes of the stream and convert it into an integer "size". But this doesn't seem right to me. I could also...
1
3638
by: hakan.thornqvist | last post by:
I am using GZipStream for compression, and the size and contents of the compressed byte array differs from time to time, using the same source. byte uncompressed = Enc.GetBytes(xmlNode.OuterXml); byte compressed = compress(uncompressed); private byte compress(byte uncompressed) { MemoryStream ms = new MemoryStream();
1
10208
by: sedwick | last post by:
I'm using .NET 2.0.50727 in VS 2005 Pro, ENU Service Pack 1 (KB926601). I've been experimenting with the System.IO.Compression classes GZipStream and DeflateStream, and I found it interesting to note that they'll create different-sized compressed files depending on the procedure you use to take in the source file's data. With one, almost every file I feed them for compression ends up significantly bigger. That code is below, almost verbatim...
0
1251
by: pierreuk | last post by:
Hi All, I have been looking at the best way of doing this but can't find a solution that doesn't involve using bytes array. Basically I want to wrap a XmlTextWriter in a GZipStream so I produce a GZipped Xml file. I have been trying this: using (GZipStream gzipout = new GZipStream(File.Create(Path), CompressionMode.Compress)) {
4
3457
by: Arnie | last post by:
Folks, The GZipStream class in .NET is throwing an exception during an OS unseal/power up (first boot experience). The .NET system is fully up as other apps seem to run fine.
0
1336
by: DR | last post by:
Why is its substantialy slower to load 50GB of gzipped file (20GB gzipped file) then loading 50GB unzipped data? im using System.IO.Compression.GZipStream and its not maxing out the cpu while loading the gzip data! Im using the default buffer of the stream that i open on the 20GB gzipped file and pass it into the GZipStream ctor. then System.IO.Compression.GZipStream takes an hour! when just loading 50GB file of data takes a few minutes!
2
2947
by: DR | last post by:
Why is its substantialy slower to load 50GB of gzipped file (20GB gzipped file) then loading 50GB unzipped data? im using System.IO.Compression.GZipStream and its not maxing out the cpu while loading the gzip data! Im using the default buffer of the stream that i open on the 20GB gzipped file and pass it into the GZipStream ctor. then System.IO.Compression.GZipStream takes an hour! when just loading 50GB file of data takes a few minutes!
2
4985
by: =?Utf-8?B?TWFydGluIE1hZHJlemE=?= | last post by:
Hi, i've got problems with CryptoStream and GZipStream. Someone tried that? Or got any idea why this wont work? i tried every combination, first i used CryptoStream, after that GZipStream. Or first i create a file with GZipStream and crypt that file. But become back the orginal is impossible... I hope anyone got an idea how this could work.
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10325
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10148
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10091
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3646
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.