如何确定字符串的大小并对其进行压缩 [英] How to determine size of string, and compress it

查看:50
本文介绍了如何确定字符串的大小并对其进行压缩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在 C# 中开发一个使用 Amazon SQS 的应用程序消息的大小限制为 8kb.

I'm currently developing an application in C# that uses Amazon SQS The size limit for a message is 8kb.

我有一个类似的方法:

public void QueueMessage(string message)

在这个方法中,我想首先压缩消息(大多数消息是作为json传入的,所以已经相当小了)

Within this method, I'd like to first of all, compress the message (most messages are passed in as json, so are already fairly small)

如果压缩后的字符串仍然大于8kb,我会将其存储在S3中.

If the compressed string is still larger than 8kb, I'll store it in S3.

我的问题是:

如何轻松测试字符串的大小,以及压缩它的最佳方法是什么?我不是在寻求大幅缩小尺寸,只是想要一些不错且简单的东西 - 并且易于解压另一端.

How can I easily test the size of a string, and what's the best way to compress it? I'm not looking for massive reductions in size, just something nice and easy - and easy to decompress the other end.

推荐答案

要知道字符串的大小"(以 kb 为单位),我们需要知道编码.如果我们假设 UTF8,那么它(不包括 BOM 等)如下(但如果它不是 UTF8,则交换编码):

To know the "size" (in kb) of a string we need to know the encoding. If we assume UTF8, then it is (not including BOM etc) like below (but swap the encoding if it isn't UTF8):

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

重新打包;我建议通过 UTF8 使用 GZIP,如果必须是字符串,则可以选择后跟 base-64:

Re packing it; I would suggest GZIP via UTF8, optionally followed by base-64 if it has to 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
    }

这篇关于如何确定字符串的大小并对其进行压缩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆