Search code examples
c#stringwinformscompressionmailto

How to shrink a string and be able to find the original later


I am working on this app that is still in beta, so I set up a logging system. The log is too long to be used in a mailto url so I thought about shrinking the text and then decrypt it. Let's say I have a 50 line long log, this should help me make something like this zef16z1e6f8 and then have a procedure to use that to find out all 50 lines of the log. I would like to note that I don't need any fancy TripleDES encryption or something.


Solution

  • First I would suggest re-looking at why you can't just mail the entire log content? Unless you have large logs (>5MB) I'd suggest just mailing the log. If you still want to pursue some shrinking strategy there are two I'd consider.

    If you want a simple reference string which can be used to lookup your log data at some later stage you can just associate some sort of identifier with the data (e.g. a GUID as suggested by Eugene). This has the benefit of having a constant length, irrespective of the log size.

    Alternatively you could just compress the log, this will shrink the data somewhat (anything up to about 90%, as Dan mentioned). However this has the downside of having a variable length and for very large logs may still exceed your size limitations. If you go this route you could do something like this (not tested):

    private string GetCompressedString()
    {
        byte[] byteArray = Encoding.UTF8.GetBytes("Some long log string");
        using (var ms = new MemoryStream())
        {
            using (var gz = new GZipStream(ms, CompressionMode.Compress, true))
            {
                ms.Write(byteArray, 0, byteArray.Length);
            }
    
            ms.Position = 0;
    
            var compressedBytes = new byte[ms.Length];
            ms.Read(compressedBytes, 0, compressedBytes.Length);
    
            return Convert.ToBase64String(compressedBytes);
        }
    }