How to determine the size of a string and compress it

I am currently developing a C # application that uses Amazon SQS. The message size limit is 8kb.

I have a method that looks something like this:

public void QueueMessage(string message) 

As part of this method, I would like to first of all compress the message (most messages are sent as json, so they are quite small)

If the compressed line is even larger than 8kb, I will store it in S3.

My question is:

How can I easily check the size of a string and what is the best way to compress it? I'm not looking for massive size reduction, just something nice and light - and it’s easy to unzip the other end.

+6
string c # amazon-sqs
source share
2 answers

To find out the "size" (in kb) of a string, we need to know the encoding. If we assume UTF8, then this (not including the specification, etc.) As shown below (but replace the encoding if it is not UTF8):

 int len = Encoding.UTF8.GetByteCount(longString); 

Put it down; I would suggest GZIP via UTF8, optionally followed by base-64 if it should be a string:

  using (MemoryStream ms = new MemoryStream()) { using (GZipStream gzip = new GZipStream(ms, CompressionMode.Compress, true)) { byte[] raw = Encoding.UTF8.GetBytes(longString); gzip.Write(raw, 0, raw.Length); gzip.Close(); } byte[] zipped = ms.ToArray(); // as a BLOB string base64 = Convert.ToBase64String(zipped); // as a string // store zipped or base64 } 
+12
source share

Give unzip bytes to this function. The best I could come up with was

 public static byte[] ZipToUnzipBytes(byte[] bytesContext) { byte[] arrUnZipFile = null; if (bytesContext.Length > 100) { using (var inFile = new MemoryStream(bytesContext)) { using (var decompress = new GZipStream(inFile, CompressionMode.Decompress, false)) { byte[] bufferWrite = new byte[4]; inFile.Position = (int)inFile.Length - 4; inFile.Read(bufferWrite, 0, 4); inFile.Position = 0; arrUnZipFile = new byte[BitConverter.ToInt32(bufferWrite, 0) + 100]; decompress.Read(arrUnZipFile, 0, arrUnZipFile.Length); } } } return arrUnZipFile; } 
+1
source share

All Articles