From: Finn Stampe Mikkelsen on
Hi

I've tried below code, working with the SelfPaced Training Kit for FrameWork
2.0 (MS) and also tried to play around with the classes otherwise.. I can't
seem to get a result that actually compresses any data...

I have tried with files from around 1mb up to the below file of 250mb and
there always seems to be the same result. The files come out with a bigger
size, actually proportionat to the initial size..

Whats up with that?? What am I and aparrently MS doing wrong, since they
claim that the below code will shrink the sourcefile...

FileStream sourceFile =
File.OpenRead(@"Y:\DaffReport_20-04-2010.csv");
FileStream destFile =
File.Create(@"Y:\DaffReport_20-04-2010.zip");

//GZipStream compStream = new GZipStream(destFile,
CompressionMode.Compress);
DeflateStream compStream = new DeflateStream(destFile,
CompressionMode.Compress);

int theByte = sourceFile.ReadByte();

while (theByte != -1)
{
compStream.WriteByte((byte)theByte);
theByte = sourceFile.ReadByte();
}

sourceFile.Close();
destFile.Close();

/Finn
--
Der er 10 slags mennesker - Dem som forst�r bin�r og dem som ikke g�r.
There are 10 kinds of people. Those who understand binary and those who
don't.
Es gibt 10 Arten von Menschen. Die, die Bin�r verstehen, bzw. die, die es
nicht tuhen.

From: Arne Vajhøj on
On 20-04-2010 20:21, Finn Stampe Mikkelsen wrote:
> I've tried below code, working with the SelfPaced Training Kit for
> FrameWork 2.0 (MS) and also tried to play around with the classes
> otherwise.. I can't seem to get a result that actually compresses any
> data...
>
> I have tried with files from around 1mb up to the below file of 250mb
> and there always seems to be the same result. The files come out with a
> bigger size, actually proportionat to the initial size..
>
> Whats up with that?? What am I and aparrently MS doing wrong, since they
> claim that the below code will shrink the sourcefile...
>
> FileStream sourceFile = File.OpenRead(@"Y:\DaffReport_20-04-2010.csv");
> FileStream destFile = File.Create(@"Y:\DaffReport_20-04-2010.zip");
>
> //GZipStream compStream = new GZipStream(destFile,
> CompressionMode.Compress);
> DeflateStream compStream = new DeflateStream(destFile,
> CompressionMode.Compress);
>
> int theByte = sourceFile.ReadByte();
>
> while (theByte != -1)
> {
> compStream.WriteByte((byte)theByte);
> theByte = sourceFile.ReadByte();
> }
>
> sourceFile.Close();
> destFile.Close();

Those compression classes has a bug/feature where it only compresses
when you call Write with large byte arrays and not WriteByte with a
single byte.

So either you call Write with a byte array or you wrap the
stream with a BufferedStream.

Arne



From: Arne Vajhøj on
On 20-04-2010 20:45, Arne Vajh�j wrote:
> On 20-04-2010 20:21, Finn Stampe Mikkelsen wrote:
>> I've tried below code, working with the SelfPaced Training Kit for
>> FrameWork 2.0 (MS) and also tried to play around with the classes
>> otherwise.. I can't seem to get a result that actually compresses any
>> data...
>>
>> I have tried with files from around 1mb up to the below file of 250mb
>> and there always seems to be the same result. The files come out with a
>> bigger size, actually proportionat to the initial size..
>>
>> Whats up with that?? What am I and aparrently MS doing wrong, since they
>> claim that the below code will shrink the sourcefile...
>>
>> FileStream sourceFile = File.OpenRead(@"Y:\DaffReport_20-04-2010.csv");
>> FileStream destFile = File.Create(@"Y:\DaffReport_20-04-2010.zip");
>>
>> //GZipStream compStream = new GZipStream(destFile,
>> CompressionMode.Compress);
>> DeflateStream compStream = new DeflateStream(destFile,
>> CompressionMode.Compress);
>>
>> int theByte = sourceFile.ReadByte();
>>
>> while (theByte != -1)
>> {
>> compStream.WriteByte((byte)theByte);
>> theByte = sourceFile.ReadByte();
>> }
>>
>> sourceFile.Close();
>> destFile.Close();
>
> Those compression classes has a bug/feature where it only compresses
> when you call Write with large byte arrays and not WriteByte with a
> single byte.
>
> So either you call Write with a byte array or you wrap the
> stream with a BufferedStream.

Example:

using System;
using System.IO;
using System.IO.Compression;

namespace E
{
public class DeflateTest
{
public void WithOneByte(string fromfnm, string tofnm)
{
using(FileStream fromf = File.OpenRead(fromfnm))
using(FileStream tof = File.Create(tofnm))
using(DeflateStream cmp = new DeflateStream(tof,
CompressionMode.Compress))
{
int c;
while((c = fromf.ReadByte()) >= 0)
{
cmp.WriteByte((byte)c);
}
}
}
public void WithBuffer(string fromfnm, string tofnm)
{
using(FileStream fromf = File.OpenRead(fromfnm))
using(FileStream tof = File.Create(tofnm))
using(DeflateStream cmp = new DeflateStream(tof,
CompressionMode.Compress))
{
byte[] b = new byte[100000];
int n;
while((n = fromf.Read(b, 0, b.Length)) > 0)
{
cmp.Write(b, 0, n);
}
}
}
public void WithCombo(string fromfnm, string tofnm)
{
using(FileStream fromf = File.OpenRead(fromfnm))
using(FileStream tof = File.Create(tofnm))
using(DeflateStream cmp = new DeflateStream(tof,
CompressionMode.Compress))
using(BufferedStream buf = new BufferedStream(cmp))
{
int c;
while((c = fromf.ReadByte()) >= 0)
{
buf.WriteByte((byte)c);
}
}
}
public void WithComboX(string fromfnm, int bufsiz, string tofnm)
{
using(FileStream fromf = File.OpenRead(fromfnm))
using(FileStream tof = File.Create(tofnm))
using(DeflateStream cmp = new DeflateStream(tof,
CompressionMode.Compress))
using(BufferedStream buf = new BufferedStream(cmp, bufsiz))
{
int c;
while((c = fromf.ReadByte()) >= 0)
{
buf.WriteByte((byte)c);
}
}
}
public static void Main(string[] args)
{
DeflateTest gzt = new DeflateTest();
gzt.WithOneByte(@"C:\foobar.txt", @"C:\foobar.gz1");
gzt.WithBuffer(@"C:\foobar.txt", @"C:\foobar.gz2");
gzt.WithCombo(@"C:\foobar.txt", @"C:\foobar.gz3");
gzt.WithComboX(@"C:\foobar.txt", 256, @"C:\foobar.gz4");
gzt.WithComboX(@"C:\foobar.txt", 16, @"C:\foobar.gz5");
}
}
}


20-04-2010 20:49 3.863 foobar.gz1
20-04-2010 20:49 2.210 foobar.gz2
20-04-2010 20:49 2.210 foobar.gz3
20-04-2010 20:49 2.221 foobar.gz4
20-04-2010 20:49 2.465 foobar.gz5
04-04-2005 15:28 4.351 foobar.txt

Arne