MemoryStream Compression

zhaozj2021-02-16  115

Introduction

Hello, this is my first article on CodeProject. I have been a long time reader, and the CodeProject resource has been an endless supply of answers to many questions. After searching CodeProject, I found that the .NET section lacked any articles on compression, SO I Thought I Would Write this article.

Sharpziplib from ICSharpcode

First Of All, This Article Depends on The Sharpziplib Which IS 100% Free To Use, in Any Sort of Projects. Details on The license and Download Links Are Available Here.

Purpose

A friend asked me to teach him C # .NET, and as a project to teach him, I decided to start writing a revision control system utilizing both server and client, we've both had our share of pitfalls with CVS. One of the features he wanted involved compression, so I sought out this library, but its documentation is sketchy unless you use it purely for an API reference. Also, the documentation only shows examples of file based compression. However, in our project, we wanted the ability to work in memory (with custom diff-type patches). Originally, I found that looked promising this library on a forum that said this was not possible, but after digging into the library documentation, I found some Stream-oriented classes. An hour OR SO of Playing Around, and this Simple and Short Code Was The Result. Since, I Have Not Included Any Source Or Demo Files To Download. I Hope Some Finds this Useful!

Compression

For Convenience Sake, WE Localize the namespa, text, and sharpziplib:

Using system;

Using system.io;

Using system.text;

Using ICsharpcode.sharpziplib.bzip2;

First Of All, We'll Start With Compression. Since We're Using MemoryStreams, Let's Create A New ONE: MemoryStream Mscompressed = New MemoryStream ();

Simple enough, right? For this example, I will use BZip2. You can use Zip, or Tar, however, they require implementing a dummy FileEntry, which is extra overhead that is not needed. My choice of BZip2 over GZip comes from the experience That Larger Data Can Be Compressed Smaller, At The Cost of a Slightly Larger Header (Discussed Below).

NEXT, WE CREATE A BZIP2 OUTPUT Stream, Passing IN Our MemoryStream.

Bzip2outputstream ZoscompRessed = New Bzip2OutputStream (Mscompressed);

Pretty easy ... Now however, is a good time to address the header overhead I mentioned above. In my practical tests, compressing a 1 byte string, rendered a 28 byte overhead from the headers alone when using GZip, plus the additional byte that could not be compressed any further. The same test with BZip2 rendered a 36 byte overhead from the headers alone. In practice, compressing a source file from a test project of 12892 bytes was compressed to 2563 bytes, about a 75% compression rate give or Take My Bad Math, Using Bzip2. Similarly, Another Test Revealed 730 Bytes Compressed to 429 Bytes. And a final test, a 174 bytes compressed to 161 bytes.

Obviously, with any compression, The More Data IS Available, The Better The Algorithm CAN Compress Patterns.

So with That Little Bit of theory Out of The Way, Back to The Code ... from here, We Start Writing Data To The Bzip2outputStream:

String sbuffer = "this represents Some data being"; "

Byte [] bytesbuffer = encoding.ascii.getbytes (sbuffer);

Zoscompressed.write (bytesbuffer, 0, bytesbuffer.length);

Zoscompressed.Finalize (); zoscompressed.close ();

Pretty easy. As with most IO and stream methods, byte arrays are used instead of strings. So we encode our output as a byte array, then write it to the compression stream, which in turn compresses the data and writes it to the inner stream , Which is Our memoryStream.

Bytesbuffer = mscompressed.toArray ();

String scompressed = encoding.ascii.getstring (bytesbuffer);

So now, the MemoryStream contains the compressed data, so we pull it out as a byte array and convert it back to a string. Note that this string is NOT readable, attempting to put this string into a textbox will render strange results. If you want to view the data, the way I did it was to convert it into a Base64 string, but this increases the size, anyone has any suggestions to that are welcome to comment. The result of running this specific code renders the 43 byte uncompressed data AS 74 BYTE COMPRESSED DATA, AND WHEN ENCODED AS 100 CHARACTERS as Fort IS 100 Characters As Follows:

QlpootfbwsztwzxkipsaaAmtgeabbaa 49waiaaxttixmteimjhnndibvq

ayehiwn49ldoknqkn2c9zug5 luskckehomhfng =

Obviously, these are not desirable results. However, I believe the speed of which the library compresses short strings of data could be extended into a method which returns either a compressed or uncompressed string with a flag indicating which was more efficient.

Uncompression

Now of course, to test our code above, we need some uncompression code I will put all the code together, since it's pretty much the same, just using a BZip2InputStream instead of a BZip2OutputStream, and Read instead of Write.:

MemoryStream Msuncompressed =

New memorystream (Encoding.ASCII.GetBytes (Scompressed));

Bzip2InputStream Zisuncompressed = New Bzip2InputStream (MsunCompRessed);

Bytesbuffer = new byte [zisuncompressed.length];

Zisuncompressed.Read (Bytesbuffer, 0, Bytesbuffer.Length);

Zisuncompressed.Close ();

Msuncompressed.close ();

String suncompressed = encoding.ascii.getstring (bytesbuffer);

Now, a quick check on sUncompressed should reveal the original string intact ... No files involved, however, if you wanted to load a file, there are a few ways you can do it, and I leave it to your imagination.

转载请注明原文地址:https://www.9cbs.com/read-10451.html

New Post(0)