How to achieve minimum size when compressing small amount of data lossless?

  1. I don’t understand the answer to ”Why does gzip/deflate compressing a small file result in many trailing zeroes?” (Why does gzip/deflate compressing a small file result in many trailing zeroes?)
  2. How would you go about compressing small amount of data ½-2 Kbyte to minimum size in a .NET-environment? (Runtime is not an issue for me. Can I trade speed for size? Should I use 3rd party products? Developer license fees are OK, but runtime license not.)
  3. Any suggestions about how I can improve the code below for:
    (a) Higher compression ratio?
    (b) More proper use of streams?

Here is the C#-code that needs to be improved:

private static byte[] SerializeAndCompress(MyClass myObject) { using (var inStream = new System.IO.MemoryStream()) { Serializer.Serialize< MyClass >(inStream, myObject); // PROTO-buffer serialization. (Code not included here.) byte[] gZipBytearray = GZipCompress(inStream); return gZipBytearray; } } private static Byte[] GZipCompress(MemoryStream inStream) { inStream.Position = 0; byte[] byteArray; { using (MemoryStream outStream = new MemoryStream()) { bool LeaveOutStreamOpen = true; using (GZipStream compressStream = new GZipStream(outStream, CompressionMode.Compress, LeaveOutStreamOpen)) { // Copy the input stream into the compression stream. // inStream.CopyTo(Compress); TODO: "Uncomment" this line and remove the next one after upgrade to .NET 4 or later. CopyFromStreamToStream(inStream, compressStream); } byteArray = CreateByteArrayFromStream(outStream); // outStream is complete first after compressStream have been closed. } } return byteArray; } private static void CopyFromStreamToStream(Stream sourceStream, Stream destinationStream) { byte[] buffer = new byte[4096]; int numRead; while ((numRead = sourceStream.Read(buffer, 0, buffer.Length)) != 0) { destinationStream.Write(buffer, 0, numRead); } } private static byte[] CreateByteArrayFromStream(MemoryStream outStream) { byte[] byteArray = new byte[outStream.Length]; outStream.Position = 0; outStream.Read(byteArray, 0, (int)outStream.Length); return byteArray; }

Category:stream Time:2011-03-03 Views:1

Related post

  • Is there a size limit to the amount of data you can send to a WCF service? 2009-08-28

    Is there a size limit to the amount of data you can send to a WCF service? I send an array of objects over and when the array gets to be a certain size, I get a 404 bad request exception. Is this a limit of httpHosting? Would another type of hosting

  • Is there a size limit to the amount of data returned by an Ajax Request? 2009-12-18

    I've been having this problem in the Chrome browser. i use jquery's ajax post. Ajax is supposed to return a really long raw html. When I do it in FF3 and IE8 it works fine. But in chrome the data seems to be truncated. --------------Solutions--------

  • Good minimum size for single background image 2012-04-07

    I have a single image covering the entire background. Do sites that do this usually choose an image size via JavaScript? And if I didn't want to use JavaScript, what's a good minimum size to cover the vast majority of screens? Thanks. --------------S

  • Overcome OS Imposed Windows Form Minimum Size Limit 2009-06-14

    In an application I am developing, I need to be able to make a windows form smaller than the minimum height limit imposed by the operating system (36 px in Vista). I have tried intercepting WM_GETMINMAXINFO and providing my own information to overrid

  • How to set a xulrunner main window's minimum size? 2009-06-23

    I want to set a minimum size for my xulrunner applications' main window.I have tried minwidth and minheight but didnt work.wat am i missing out? --------------Solutions------------- Here is an example: window.addEventListener("resize", function() { i

  • Which files does not reduce its size after compression 2009-07-16

    I have written a java program for compression. I have compressed some text file. The file size after compression reduced. But when I tried to compress PDF file. I dinot see any change in file size after compression. So I want to know what other files

  • Any guaranteed minimum sizes for types in C? 2009-11-15

    I asked a similar question before but I'm still confused... can you generally make any assumptions about the minimum size of a data type? What I have read so far char: 1 Byte short: 2 Byte int: 2 Byte, typically 4 Byte long: 4 Byte float??? double???

  • Crop a PNG image to its minimum size 2009-12-15

    How to cut off the blank border area of a PNG image and shrink it to its minimum size using Python? --------------Solutions------------- PIL's getbbox is working for me im.getbbox() => 4-tuple or None Calculates the bounding box of the non-zero re

  • Setting minimum size limit for a window in java swing 2010-05-06

    I have a JFrame which has 3 JPanels in GridBagLayout.. Now, when I minimize a windows, after a certain limit, the third JPanel tends to disappear. I tried setting minimizing size of JFrame using setMinimumSize(new Dimension(int,int)) but no success.

  • Minimum size that matches aspect ratio 2010-08-18

    I need to find the minimum size that has an aspect ratio of exactly (or within 0.001) some value. Are there any quick math tricks or framework tricks for doing this? Here's the pseudo code for the current bad idea I had running in O(n^2): epsilon = 0

  • custom view minimum size 2010-12-17

    Is there a way to enforce minimum size for a Custom View (NSView)? I know with window one can just set the minimum size, but there are no such setting for custom view. Is it possible to subclass NSView for this purpose? The reason I need this is beca

  • Form has minimum size after restoring from minimize state 2011-02-23

    I Added ClientSize to Application Settings and DataBindings in Properties window, in order to save size of the form after it was closed. And that worked. But when I minimize form and then activate it back, it has minimum size . Is it a bug or I'm doi

  • find the minimum size dominating set for a tree using greedy algorithm 2011-03-15

    Dominating Set (DS) := given an undirected graph G = (V;E), a set of vertices S V is a dominating set if for every vertex in V , there is a vertex in S that is adjacent to v. Entire vertex set V is a trivial dominating set in any graph. Find minimum

  • How to get and/or overwrite the minimum size of windows in Mac OSX 2011-04-24

    I want to resize any window on my machine, which I accomplish with AppleScript, using GUI Scripting. The script looks similar to the following: tell application "System Events" set appProcess to the first process whose name is "Finder" set appWindow

  • Minimum size of 2D barcode that a 2MP mobile camera can scan 2011-04-27

    I am getting a mobile application done. I want to know what is minimum size of 2D barcode that a 2MP mobile camera can scan. Barcodes what I am printing is <1cm Regards, Saurabh --------------Solutions------------- For small barcodes, it's hard to

  • What is the maximum and minimum size of connection pool ADO.Net Supports in the connection string? 2011-06-01

    What is the maximum and minimum size of connection pool ADO.Net Supports in the connection string. Min Pool Size=[max size ?] Max Pool Size=[min size] --------------Solutions------------- Default Max Pool Size 100 Min Pool Size 0 Connection Pooling f

  • Minimum size/width of a QPushButton that is created from code 2011-07-10

    I created 2 rows of push buttons, each row is inside a QHBoxLayout. I create the buttons in the code: static const char* buttonText = "23456789TJQKA"; for (int ii = 0; buttonText[ii]; ii++) { QPushButton* pushButton = new QPushButton(this); pushButto

  • Java window contents resize, but not beyond a minimum size 2011-09-14

    I have a Java apps I've written using NetBeans (yes, I've read plenty of complaints here about NetBeans). When I resize the window, the contents resize with the window, until I shrink the window (the height, actually) below a certain size. Then the w

  • How do you change the minimum size of a QTableWidget? 2011-10-19

    I am embedding some QTableWidget(s) recursively inside of each other. The final GUI will have 4 to a couple thousand squares. Here is an example screenshot only showing 16 squares.: Because there are so many squares (thousands) that need to be displa

Copyright (C), All Rights Reserved.

processed in 0.468 (s). 13 q(s)