简体   繁体   English

Java-压缩大字串

[英]Java - Compress large string

In my Java application I get from some calculations a really long string (from really long string I mean of around 600000 characters or so). 在我的Java应用程序中,我从一些计算中得到了一个很长的字符串(从很长的字符串中,我的意思是大约600000个字符左右)。 But I need to send this string to a client to process it, and for this reason I need the compressed string to be of maximum 1000 characters. 但是我需要将此字符串发送给客户端进行处理,因此,我需要压缩的字符串最大为1000个字符。

I have tried using GZIPOutputStream and Inflater and Deflater classes, and in the best case I got an output string of 300000 characters, which is great compression, but in my case it's not enough. 我尝试使用GZIPOutputStream以及Inflater和Deflater类,并且在最佳情况下,我得到了300000个字符的输出字符串,这是很好的压缩方式,但对于我而言,这还不够。

I have also tried compressing the string n times, but the ouput was larger than the previous one, so only one-time compressing was successful. 我还尝试了n次压缩字符串,但是输出比前一个大,因此只有一次压缩成功。

So, what do you suggest me to try? 那么,您建议我尝试什么?

Thank you. 谢谢。

I agree with @Peter Lawrey that, strictly with those requirements, it might be impossible to deliver such a big message to the client. 我同意@Peter Lawrey的观点,严格按照这些要求,可能无法向客户传达如此重要的信息。

Anyway, I still suggest three possible solutions, depending on how flexible your requirements are: 无论如何,我仍然建议三种可能的解决方案,具体取决于您的需求的灵活性:

  1. If all of your input strings have a restricted vocabulary (it does not allow a free, random combination of letters, symbols and numbers, but it is restricted to a certain set of business words, identifiers and values), and also a simple grammar , you can try to design your own compress algorithm . 如果你所有的输入字符串有一个限制的词汇 (它不允许的字母,符号和数字的自由,随意组合,但它仅限于一特定的业务的话,标识符和值),也是一个简单的语法 ,您可以尝试设计自己的压缩算法 Example: 例:

input symbol compressed symbol ------------ ----------------- client 1 bill 2 date 3 amount 4 value 5 price 6 tax 7

If the grammar is simple but the vocabulary is not that restricted, you could perform an initial custom compression to compress the document's structure as much as you can, and then a second GZIP compression to compress the data. 如果语法很简单,但词汇量没有那么大限制,则可以执行初始自定义压缩以尽可能多地压缩文档的结构,然后执行第二次GZIP压缩以压缩数据。

And don't forget that you'll have to bundle the client application with the corresponding uncompressor. 并且不要忘记,您必须将客户端应用程序与相应的解压缩器捆绑在一起。

Anyway, it's not an easy task, I admit it. 无论如何,我承认这不是一件容易的事。

  1. Deliver the response to the client application in streaming . 中将响应传递到客户端应用程序。 If the protocol is HTTP, you could use Chunked Transfer Coding . 如果协议是HTTP,则可以使用Chunked Transfer Coding

  2. If everything else fails, you'll have to page the results and serve them to the client by pages on demand: The client makes a query, the server executes it and delivers just the first page of results. 如果其他所有操作失败,则必须分页结果,并按需分页地将结果提供给客户端:客户端进行查询,服务器执行该查询并仅提供结果的第一页。 Then, the client may chose to read the next page. 然后,客户端可以选择阅读下一页。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM