473,545 Members | 666 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

compression - dropping the last byte

It might just be my tired eyes, but I can't see what is wrong in the
following:

I have a byte array filled with random data (with a fixed seed to make
reproducable); I then compress and decompress this using the 2.0 GZip
compression classes - however, as you see, the last byte is getting dropped.

Any ideas? Almost certainly me having a blond moment ;-(

Marc

Overview:
GetByteData : returns an array of random data
Describe : details the first / last 5 values to the console (first: last,
second : penultimate, etc...)
CopyStream : reads / writes between two streams until the source is
exhausted (is there an wasier way of doing this?)
Compress / Decompress : use the GZip classes to manipulate the data

Results:

* 50000
0=170 49999=187
1=200 49998=183
2=81 49997=158
3=81 49996=216
4=66 49995=154

* 49999
0=170 49998=183
1=200 49997=158
2=81 49996=216
3=81 49995=154
4=66 49994=133
Code : watch for wrap
=============== =

using System;
using System.IO;
using System.IO.Compr ession;
namespace ConsoleApplicat ion1 {
static class Program {
public static void Main() {
byte[] data = GetByteData();
Describe(data);
Console.WriteLi ne();
byte[] compressed = Compress(data);
byte[] decompressed = Decompress(comp ressed);
Describe(decomp ressed);
Console.ReadLin e();
}

private static void Describe(byte[] data) {
Console.WriteLi ne("* " + data.Length.ToS tring());
int length = data.Length;
for (int i = 0; i < 5; i++) {
int otherEnd = length - 1 - i;
Console.WriteLi ne(string.Forma t("{0}={1}\t{2} ={3}",i,data[i],otherEnd,data[otherEnd]));
}
}
private static byte[] Compress(byte[] data) {
using (Stream input = new MemoryStream(da ta))
using (MemoryStream output = new MemoryStream())
using (Stream zipper = new GZipStream(outp ut,
CompressionMode .Compress, true)) {
CopyStream(inpu t, zipper);
return output.ToArray( );
}
}
private static byte[] Decompress(byte[] data) {
using (Stream input = new MemoryStream(da ta))
using (Stream unzipper = new GZipStream(inpu t,
CompressionMode .Decompress, false))
using (MemoryStream output = new MemoryStream()) {
CopyStream(unzi pper, output);
return output.ToArray( );
}
}

private const int SEED = 141566, SIZE = 50000;

public static byte[] GetByteData() {
Random rand = new Random(SEED); // some seed to make
reproducable
byte[] data = new byte[SIZE];
rand.NextBytes( data);
return data;

}

public static long CopyStream(Syst em.IO.Stream source,
System.IO.Strea m destination) {
const int BUFFER_SIZE = 512;
long copied = 0;
byte[] buffer = new byte[BUFFER_SIZE];
int bytes;
while ((bytes = source.Read(buf fer, 0, BUFFER_SIZE)) > 0) {
destination.Wri te(buffer, 0, bytes);
copied += bytes;
}
return copied;
}
}
}

Feb 1 '06 #1
3 2602
Marc Gravell wrote:
It might just be my tired eyes, but I can't see what is wrong in the
following:

I have a byte array filled with random data (with a fixed seed to make
reproducable); I then compress and decompress this using the 2.0 GZip
compression classes - however, as you see, the last byte is getting dropped.

Any ideas? Almost certainly me having a blond moment ;-(


Move the return output.ToArray( ); call *outside* the "using (Stream
zipper...)" block. That way the GZipStream is disposed of (and
therefore flushed and terminated appropriately) before you return the
data.

Making that change (which requires extra bracing) makes your sample
work.

Jon

Feb 1 '06 #2
A huge thanks to you Jon - that does indeed fix it. Quite a subtle one! Even
calling zipper.Flush() (just before ToArray) doesn't fix it...

I guess there's a cautionary lesson in there for all of use regarding
bracing etc : at a casual glance the following look functionally identical -
but one works and one doesn't... a bug that I think got introduced when
"tidying" the braces because they obviously [sic] do the same thing...

// works
using (Stream input = new MemoryStream(da ta))
using (MemoryStream output = new MemoryStream()) {
using (Stream zipper = new GZipStream(outp ut, CompressionMode .Compress,
true)) {
CopyStream(inpu t, zipper);
}
return output.ToArray( );
}

and

// fails
using (Stream input = new MemoryStream(da ta))
using (MemoryStream output = new MemoryStream())
using (Stream zipper = new GZipStream(outp ut, CompressionMode .Compress,
true)) {
CopyStream(inpu t, zipper);
return output.ToArray( );
}

Marc
Feb 1 '06 #3
one would think flush should have solved this. Is this by design?

--
William Stacey [MVP]

"Marc Gravell" <mg******@rm.co m> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
|A huge thanks to you Jon - that does indeed fix it. Quite a subtle one!
Even
| calling zipper.Flush() (just before ToArray) doesn't fix it...
|
| I guess there's a cautionary lesson in there for all of use regarding
| bracing etc : at a casual glance the following look functionally
identical -
| but one works and one doesn't... a bug that I think got introduced when
| "tidying" the braces because they obviously [sic] do the same thing...
|
| // works
| using (Stream input = new MemoryStream(da ta))
| using (MemoryStream output = new MemoryStream()) {
| using (Stream zipper = new GZipStream(outp ut, CompressionMode .Compress,
| true)) {
| CopyStream(inpu t, zipper);
| }
| return output.ToArray( );
| }
|
| and
|
| // fails
| using (Stream input = new MemoryStream(da ta))
| using (MemoryStream output = new MemoryStream())
| using (Stream zipper = new GZipStream(outp ut, CompressionMode .Compress,
| true)) {
| CopyStream(inpu t, zipper);
| return output.ToArray( );
| }
|
| Marc
|
|
Feb 2 '06 #4

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

Similar topics

3
4564
by: orekinbck | last post by:
Hi There Is there any easy way to use the System.Compression tools in .NET 2.0 to compress an entire directory ? All my source code is kept in a single directory so I have written a utility that recursively backs up the directory and compresses each file as it goes. The utility has no GUI. I have attached the program here, I would be...
17
3137
by: dunric | last post by:
After writing the computing urban legend "The Helsinki Code", I spent several nights thinking up how in the world Gustav Larsson, the Finnish PDP-8 computer programmer, could have managed to receive such a miraculous message from God. Surely, for a 1-byte computer program such as "@" to compile successfully (in RTPS FORTRAN), a miracle...
2
2481
by: TerryStone | last post by:
Thanks to anyone who reads this. Below is some C# that compresses an array of bytes, and then decompresses, and compares the original data with the new. Firstly, the length of the decompressed data is shorter than the original. So some loss of data has occured. But the content up until the early truncation matches. So am I flushing...
1
7812
by: Smokey Grindel | last post by:
I have a bitmap object I want to return as a JPEG image with a compression set at 90% and progressive passes enabled, how can I do this in .NET 2.0? Progressive passes are not necessary but the compression ratio is.. thanks!
6
1550
by: Fla | last post by:
Hi! I would like to use Compression namespace for Array, i.e. use .NET native Compression for compress a String, or an Array of Integer whose elements are returned values of AscW for each char of the String. I tried with the code available @ http://msdn2.microsoft.com/en-us/library/system.io.compression.gzipstream(VS.80).aspx but with no...
5
5950
by: jeremyje | last post by:
I'm writing some code that will convert a regular string to a byte for compression and then beable to convert that compressed string back into original form. Conceptually I have.... For compression string ->(Unicode Conversion) byte -(Compression + Unicode Conversion) string
20
2906
by: chance | last post by:
Hello, I want to add compression to a memory stream and save it in an Oracle database. This is the code I have so far: //save the Word document to a binary field, MemoryStream dataStream = new MemoryStream(); doc.Save(dataStream, SaveFormat.Doc); //now compress it GZipStream compressedZipStream = new GZipStream(dataStream,
6
2368
by: mike_dba | last post by:
I have been testing compression for update operations. Can anyone tell me why I require more log for an update of a compressed table than I do for the same table that is not compressed ? I tried an update for the same number of rows for two copies of a table, one compressed and one not. The compressed UOW exceeds my log allocation while...
6
1792
by: pooppoop | last post by:
Hi, and thanks for viewing my post. i have an odd result when trying to compress and decompress a string. it seems that when i replace the Zero's in the input stream it works, if not the string decompresses to the first Zero. For some string it just cuts off the last characters. Thank in advanced. The Code:
0
7401
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...
0
7656
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. ...
0
7807
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...
1
5326
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...
0
4944
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3450
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...
0
3442
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1014
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
703
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...

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.