Re: Re: Deflate Encoding
mark W <[email protected]>
| Newsgroups | gmane.network.gnutella.devel |
|---|---|
| Message-ID | <[email protected]> |
Hi,
I wrote a sample application which compresses/decompresses a simple text file, which works fine for text files or any file stored locally. The methods are as follows:
//For Compression using Deflate
private void compressFile(string inFile, string outFile)
{
System.IO.FileStream inFileStream = new System.IO.FileStream(inFile, System.IO.FileMode.Open);
System.IO.FileStream outFileStream = new System.IO.FileStream(outFile, System.IO.FileMode.Create);
System.IO.Compression.DeflateStream outZStream = new System.IO.Compression.DeflateStream(outFileStream,System.IO.Compression.CompressionMode.Compress);
try
{
byte[] buffer = new byte[2000];
int len;
while ((len = inFileStream.Read(buffer, 0, 2000)) > 0)
{
outZStream.Write(buffer, 0, len);
}
outZStream.Flush();
}
finally
{
outZStream.Close();
outFileStream.Close();
inFileStream.Close();
}
}
//For Decompression using Deflate
private void decompressFile(string inFile, string outFile)
{
System.IO.FileStream inFileStream = new System.IO.FileStream(inFile, System.IO.FileMode.Open);
System.IO.FileStream outFileStream = new System.IO.FileStream(outFile, System.IO.FileMode.Create);
System.IO.Compression.DeflateStream outZStream = new System.IO.Compression.DeflateStream(inFileStream,System.IO.Compression.CompressionMode.Decompress,true);
byte[] buffer = new byte[327680];
using (MemoryStream ms = new MemoryStream())
{
while (true)
{
int read = outZStream.Read(buffer, 0, buffer.Length);
if (read <= 0)
{
buffer = ms.ToArray();
break;
}
ms.Write(buffer, 0, read);
}
ms.Flush();
ms.Close();
}
outFileStream.Write(buffer, 0, buffer.Length);
outZStream.Flush();
outFileStream.Flush();
outZStream.Close();
outFileStream.Close();
}
This above code works fine. I tried to dump the compressed stream on to a file which I get after handshake. I tried to match the bytes using HexEditor of both the dump and the local compressed file, both are different.
Got stuck here. Please provide inputs.
Thanx,
Waugh
[email protected] wrote: Quoting mark W <[email protected]> from ml.gnutella.dev-forum:
:API ---- I am using .Net based Api something like this:
:
:Byte [] buffer; // it has compressed data
:Byte[] deComp;//Decompressed data
:Stream streamObj = new System.IO.Compression.DeflateStream(new
:MemoryStream(buffer), CompressionMode.Decompress);
:streamObj.Read(deComp,0,buffer.Length);
:streamObj.close;
Hah! The error is obvious!!
Try to find it. In the small snippet you have posted above, there is
a grave logic bug. Unless you can find it, you're not up with the
task of writing a Gnutella client. :-)
You can formulate your hypothesis here, to increase your understanding,
but I won't tell you the bug because that would kill your learning process.
Good luck.
Raphael
Send instant messages to your online friends http://uk.messenger.yahoo.com
[Non-text portions of this message have been removed]