How to do it...

  1. Create a class called ExtensionMethods that will contain two extension methods called CompressStream() and DecompressStream(). Both these extension methods will act on a byte array and return a byte array:
        public static class ExtensionMethods
{
public static byte[] CompressStream(this byte[] originalSource)
{

}

public static byte[] DecompressStream(this byte[] originalSource)
{

}
}
  1. Looking at the CompressStream() extension method, you need to create a new MemoryStream to return to the calling code. Make use of the using statement so that objects are properly disposed of when they move out of scope. Next, add a new GZipStream object that will compress whatever we give it into the outStream object. You will notice that  CompressionMode.Compress is passed as a parameter to the GZipStream object. Lastly, write originalSource to the GZipStream object, compressing it and returning it to the calling method:
        public static byte[] CompressStream(this byte[] originalSource)
{
using (var outStream = new MemoryStream())
{
using (var gzip = new GZipStream(outStream,
CompressionMode.Compress))
{
gzip.Write(originalSource, 0, originalSource.Length);
}

return outStream.ToArray();
}
}
  1. Turn your attention to the DecompressStream() extension method next. The process is actually really simple. Create a new MemoryStream from originalSource and call it sourceStream. Create another MemoryStream called outStream to return to the calling code. Next, create a new GZipStream object and pass it  sourceStream while setting the CompressionMode.Decompress value. Copy the decompressed stream to  outStream and return it to the calling code:
        public static byte[] DecompressStream(this byte[] originalSource)
{
using (var sourceStream = new MemoryStream(originalSource))
{
using (var outStream = new MemoryStream())
{
using (var gzip = new GZipStream(sourceStream,
CompressionMode.Decompress))
{
gzip.CopyTo(outStream);
}
return outStream.ToArray();
}
}
}
  1. I created a method called InMemCompressDecompress() to illustrate the use of in-memory compression and decompression. I'm reading the contents of the file at C:tempDocumentsfile 3.txt into a variable called inputString. I then  use the default encoding to get the bytes, the original length, compressed length, and decompressed length. If you want to get the original text back, be sure to include the line newString = Encoding.Default.GetString(newFromCompressed); in your code and output that to the console window. A word of warning, though: if you are reading a lot of text, it's probably not going to make much sense to display that in the console window. Write it to a file instead to check if the text is the same as what was compressed:
        private static void InMemCompressDecompress()
{
string largeFile = @"C: empDocumentsfile 3.txt";

string inputString = File.ReadAllText(largeFile);
var bytes = Encoding.Default.GetBytes(inputString);

var originalLength = bytes.Length;
var compressed = bytes.CompressStream();
var compressedLength = compressed.Length;

var newFromCompressed = compressed.DecompressStream();
var newFromCompressedLength = newFromCompressed.Length;

WriteLine($"Original string length = {originalLength}");
WriteLine($"Compressed string length = {compressedLength}");
WriteLine($"Uncompressed string length =
{newFromCompressedLength}");

// To get the original Test back, call this
//var newString = Encoding.Default.GetString(newFromCompressed);
}
  1. Ensure that you have a file called File 3.txt in the correct directory, as stated previously. Also, ensure that the file contains some text. You can see that the file I am going to compress in-memory is about 1.8 MB in size:
  1. Running your console application will display the original length of the file, the compressed length and then the decompressed length. As would be expected, the decompressed length is the same length as the original string length:
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.200.3