Pilihan binari dalam nz46 comments
Day trading platform brokerage
Compressing data is a great way to reduce its size. This helps us reduce storage requirements as well as the bandwidth and latency of network transmissions. We will use the. Compression namespace , although it is also possible to use a third party library such as SharpZipLib. In its simplest form, GZipStream takes an underlying stream and a compression mode as parameters.
The compression mode determines whether you want to compress or decompress; the underlying stream is manipulated according to that compression mode. In the code above, we are using a memory stream as our underlying output stream. The GZipStream effectively wraps the output stream. When we write our input data into the GZipStream, it goes into the output stream as compressed data. By wrapping the write operation in a using block by itself, we ensure that the data is flushed. The bytes resulting from the GZip compression are actually binary data.
They are not intelligible when rendered , and may also cause problems when transmitted over a network due to byte ordering, for instance. One way to deal with this is to encode the compressed bytes in base Update 28th January As some people pointed out, it is not necessary to baseencode compressed data, and it will transmit fine over a network even without it.
However I do recall having issues transmitting binary compressed data via RabbitMQ, so you may want to apply base64 encoding as needed in order to render compressed data or work around issues. Base64, however, is far from a compact representation. In this specific example, the length of the output string goes from 32 bytes binary to 44 base64 , reducing the effectiveness of compression. However, for larger strings, this still represents significant savings over the plain, uncompressed string.
Which brings us to the next question: Thus, compression should typically be applied only to data whose length exceeds an arbitrary threshold.
When decompressing, the underlying stream is an input stream. The GZipStream still wraps it, but the flow is inverted so that when you read data from the GZipStream, it translates compressed data into uncompressed data.
There are different ways to implement this, even if we just focus on decompressing from a string to a string. However, a low-level buffer read such as the following will not work:. The Length property is not supported in a GZipStream, so the above code gives a runtime error. Rather than read the entire length of the buffer, you could read block by block until you reach the end of the stream. One way to get this working with very little effort is to introduce a third stream, and copy the GZipStream into it:.
Or you might want to work exclusively with bytes rather than converting back to a string. In any case, hopefully the code in this article will give you a head start when you need to compress and decompress some data. Your email address will not be published. Notify me of follow-up comments by email. Notify me of new posts by email. Write inputBytes, 0, inputBytes. GetString outputBytes ; Console. WriteLine outputStr ; Console. ToBase64String outputBytes ; Console. WriteLine outputbase64 ; Console.
The basic workflow looks something like this: However, a low-level buffer read such as the following will not work: One way to get this working with very little effort is to introduce a third stream, and copy the GZipStream into it: WriteLine decompressed ; Console. Leave a Reply Cancel reply Your email address will not be published. You learn by doing, and by falling over.