473,804 Members | 3,894 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

GZipStream compressed bytes written

3 New Member
When using a GZipStream, is there any way to know how many compressed bytes were written?

For example:

// "buffer" is data from a text file
// "offset" is 0
// "count" is 100

stream.Write(bu ffer, offset, count);

This is going to write the compressed data to the underlying data store. Is there any way to get the length of this data?

Note: I know you can use a MemoryStream to get the size but this inefficient.
Sep 30 '08 #1
11 7593
PRR
750 Recognized Expert Contributor
When using a GZipStream, is there any way to know how many compressed bytes were written?
For example:

// "buffer" is data from a text file
// "offset" is 0
// "count" is 100

stream.Write(bu ffer, offset, count);

This is going to write the compressed data to the underlying data store. Is there any way to get the length of this data?

Note: I know you can use a MemoryStream to get the size but this inefficient.
could you explain more? i dont know whether i got u right... but heres wat i can explain....

"count" is number of bytes compressed...

Expand|Select|Wrap|Line Numbers
  1. byte[] myByte;
  2.             using (FileStream f1 = new FileStream(@"C:\log.txt", FileMode.Open))
  3.             {
  4.                 myByte = new byte[f1.Length];
  5.                 f1.Read(myByte, 0, (int)f1.Length);
  6.             }
  7.  
  8.  
  9.             using (FileStream f2 = new FileStream(@"C:\log123.txt", FileMode.Create))
  10.             using (GZipStream gz = new GZipStream(f2, CompressionMode.Compress, false))
  11.             {
  12.                 gz.Write(myByte, 0, myByte.Length);
  13.             }
  14.  
  15.  
To get the length of the data written... on the other file .. i guess you will have to read it ...

Expand|Select|Wrap|Line Numbers
  1. using (FileStream f1 = new FileStream(@"C:\log123.txt", FileMode.Open))
  2.             {
  3.                 myByte = new byte[f1.Length];
  4.                 f1.Read(myByte, 0, (int)f1.Length);
  5.             }
  6.  
theres also a length property of GZipStream... but its no longer supported... so i wont know wat exactly it does....
"GZipStream.Len gth Property :This property is not supported and always throws a NotSupportedExc eption."


Expand|Select|Wrap|Line Numbers
  1. byte[] more = System.Text.UnicodeEncoding.Unicode.GetBytes("Prr");
  2.  
  3.             using (MemoryStream ms = new MemoryStream())
  4.             {
  5.                 using (GZipStream GZ = new GZipStream(ms, CompressionMode.Compress, false))
  6.                 {
  7.                     GZ.Write(more, 0, more.Length);
  8.                    // string sss = ms.Length.ToString();
  9.  
  10.                    // sss = ms.ToArray().Length.ToString();
  11. //as GZipStream writes additional data including  information when its been //disposed... you should not do the above
  12.                 }
  13.  
  14.                 byte[] bb = ms.ToArray();
  15.  
  16.                 string len = bb.Length.ToString();
  17. //You will get the length here....
  18.  
  19.  
  20.             }
  21.  
Sep 30 '08 #2
mach77
3 New Member
In my example, "count" was the uncompressed size. So 100 uncompressed bytes pass into the stream, but the stream compresses them before sending it to the underlying data source. I want to know how many bytes are sent to the underlying source.
Sep 30 '08 #3
Plater
7,872 Recognized Expert Expert
Look at the underlying stream's size?

Expand|Select|Wrap|Line Numbers
  1. FileStream fs = new FileStream(@"c:\tempzip.zip", FileMode.Create);
  2. System.IO.Compression.GZipStream gz = new System.IO.Compression.GZipStream(fs, System.IO.Compression.CompressionMode.Compress) ;
  3. byte[] fred= Encoding.ASCII.GetBytes("Billy joe has a lot of bottles of rum");
  4. gz.Write(fred, 0, fred.Length);
  5. Int64  SizeOfCompressedBytes = fs.Length;
  6.  
If you are doing multiple writes and want how much came from each write, you could probably implement logic to look at the change in the .Length property.
Sep 30 '08 #4
mldisibio
190 Recognized Expert New Member
Just to reiterate what dirtBag noted, closing a GZipStream flushes the buffers and writes some additional EOF bytes that the decompressor needs. Therefore, the most accurate byte count can only be done after the GZipStream is closed or disposed.

Also, just a warning, if checking the underlying stream length after a series of atomic writes you must take into account that stream buffering does not always write all the read bytes with each iteration unless the buffer is Flushed to the stream.
Sep 30 '08 #5
mldisibio
190 Recognized Expert New Member
Also, if you are worried about reading a large file into a memory stream, an alternative is to count bytes in chunks from a buffer size to your liking:

Expand|Select|Wrap|Line Numbers
  1.       int _BUFFER_SIZE = 4096;
  2.       FileStream inputStream = new FileStream(@"C:\someFile.zip", FileMode.Open);
  3.       byte[] readBuffer = new byte[_BUFFER_SIZE];
  4.       long totalBytes = 0;
  5.       int bytesRead = 0;
  6.         do {
  7.           // read _BUFFER_SIZE bytes from inputStream into the buffer
  8.           bytesRead = inputStream.Read(readBuffer, 0, _BUFFER_SIZE);
  9.           totalBytes += bytesRead;
  10.         }
  11.         // until no more bytes are read from the input stream
  12.         while (bytesRead > 0);
  13.  
  14.         Console.WriteLine("{0} : {1} bytes.", inputStream.Name, totalBytes);
  15.  
  16.  
Sep 30 '08 #6
mach77
3 New Member
The examples given are all pretty straight forward, but what happens when you don't have access to the underlying stream or the underlying stream throws an exception when you call Length on it?
Oct 2 '08 #7
Plater
7,872 Recognized Expert Expert
Well you always have access to the underlying stream via the .BaseStream proeprty. Not sure which stream types would throw an exception on the Length proeprty, but I guess it could happen?

What is your underlying stream type?
Oct 2 '08 #8
mldisibio
190 Recognized Expert New Member
That is exactly what dirtBag pointed out. GZipStream does not support Length or Position while open, so neither will its BaseStream.

As you can see, the implementation of GZip compression does not allow a real-time feedback of incremental bytes written...which is what you want.

Unless you write your own compression algorithm, using the GZipStream class means you will only know how may compressed bytes were written by retrieving the length of the entire compressed stream after it has been fully written and closed.

At least that is my conclusion. If someone else can show otherwise, please correct me.
Oct 2 '08 #9
Plater
7,872 Recognized Expert Expert
I have had zero trouble using the .Length property on the BaseStream
Oct 2 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

8
8847
by: Dennis Hotson | last post by:
Hi, I'm trying to write a function that adds a file-like-object to a compressed tarfile... eg ".tar.gz" or ".tar.bz2" I've had a look at the tarfile module but the append mode doesn't support compressed tarfiles... :( Any thoughts on what I can do to get around this?
0
2197
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
3309
by: KJ | last post by:
I am using the System.Compression.GZipStream class, and I noticed that in certain cases, the resultant compressed file is actually larger than the original. This is almost a constant when compressing already-compressed formats such as jpeg. Another less-consistent example of this behavior is found in the case of compressing TIFFs, wherein some files end up greatly compressed (the larger ones up to 93% compression ratio) and others end up...
1
3095
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
2154
by: prince.matt | last post by:
Hi, I am using GZipStream to compress a simple text file approx 24MB in size. I am finding that the destination "compressed" file is several MB larger than the source file (31MB). The code is as follows: Dim sourcefile As FileStream = File.OpenRead(inFileName) Dim destFile As FileStream = File.Open(outFilename, FileMode.Create, FileAccess.Write)
1
3639
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
10209
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...
4
3460
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.
5
2950
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!
0
9710
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
9589
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10085
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9163
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7626
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5527
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
5663
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4304
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
3
3000
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.