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.
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... -
byte[] myByte;
-
using (FileStream f1 = new FileStream(@"C:\log.txt", FileMode.Open))
-
{
-
myByte = new byte[f1.Length];
-
f1.Read(myByte, 0, (int)f1.Length);
-
}
-
-
-
using (FileStream f2 = new FileStream(@"C:\log123.txt", FileMode.Create))
-
using (GZipStream gz = new GZipStream(f2, CompressionMode.Compress, false))
-
{
-
gz.Write(myByte, 0, myByte.Length);
-
}
-
-
To get the length of the data written... on the other file .. i guess you will have to read it ... -
using (FileStream f1 = new FileStream(@"C:\log123.txt", FileMode.Open))
-
{
-
myByte = new byte[f1.Length];
-
f1.Read(myByte, 0, (int)f1.Length);
-
}
-
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." -
byte[] more = System.Text.UnicodeEncoding.Unicode.GetBytes("Prr");
-
-
using (MemoryStream ms = new MemoryStream())
-
{
-
using (GZipStream GZ = new GZipStream(ms, CompressionMode.Compress, false))
-
{
-
GZ.Write(more, 0, more.Length);
-
// string sss = ms.Length.ToString();
-
-
// sss = ms.ToArray().Length.ToString();
-
//as GZipStream writes additional data including information when its been //disposed... you should not do the above
-
}
-
-
byte[] bb = ms.ToArray();
-
-
string len = bb.Length.ToString();
-
//You will get the length here....
-
-
-
}
-
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.
Plater 7,872
Recognized Expert Expert
Look at the underlying stream's size? -
FileStream fs = new FileStream(@"c:\tempzip.zip", FileMode.Create);
-
System.IO.Compression.GZipStream gz = new System.IO.Compression.GZipStream(fs, System.IO.Compression.CompressionMode.Compress) ;
-
byte[] fred= Encoding.ASCII.GetBytes("Billy joe has a lot of bottles of rum");
-
gz.Write(fred, 0, fred.Length);
-
Int64 SizeOfCompressedBytes = fs.Length;
-
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.
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.
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: -
int _BUFFER_SIZE = 4096;
-
FileStream inputStream = new FileStream(@"C:\someFile.zip", FileMode.Open);
-
byte[] readBuffer = new byte[_BUFFER_SIZE];
-
long totalBytes = 0;
-
int bytesRead = 0;
-
do {
-
// read _BUFFER_SIZE bytes from inputStream into the buffer
-
bytesRead = inputStream.Read(readBuffer, 0, _BUFFER_SIZE);
-
totalBytes += bytesRead;
-
}
-
// until no more bytes are read from the input stream
-
while (bytesRead > 0);
-
-
Console.WriteLine("{0} : {1} bytes.", inputStream.Name, totalBytes);
-
-
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?
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?
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.
Plater 7,872
Recognized Expert Expert
I have had zero trouble using the .Length property on the BaseStream
Sign in to post your reply or Sign up for a free account.
Similar topics |
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?
|
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
|
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...
|
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...
|
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)
| |
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();
|
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...
|
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.
|
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!
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
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...
| |