从字符串C#LZW压缩 [英] LZW compression on C# from string
问题描述
我在寻找C#中的LZW压缩算法,需要一个串,并返回一个字符串。
我google搜索了几个小时,所有我发现使用的MemoryStream,BinaryWriters等
I'm looking for a LZW compression algorithm in C# that takes a "string" and returns a string. I've googling for hours and all I've found use MemoryStream, BinaryWriters, etc.
我只是想能够做这样的事情:
I just want to be able to do something like:
串_data =12345;
string _data = "12345";
字符串_result = CompressToLZW (_数据);
string _result = CompressToLZW(_data);
,然后通过Ajax传递字符串到浏览器。我已经对JavaScript(http://rosettacode.org/wiki/LZW_compression#JavaScript)
and then pass that string via Ajax to the browser. I already have the LZW decompression algorithm for javascript (http://rosettacode.org/wiki/LZW_compression#JavaScript)
感谢.-
更新:
这是我现在使用带的 http://paste.lisp.org/display/12198
This is the code I'm using right now with http://paste.lisp.org/display/12198
string _data = "12345_12345_12345_12345";
byte[] byteArray = Encoding.ASCII.GetBytes(_data);
MemoryStream _st = new MemoryStream(byteArray);
StreamReader _sr = new StreamReader(_st);
MemoryStream streamoutput = new MemoryStream();
BinaryWriter _output= new BinaryWriter(streamoutput);
LZW.Compress(_sr, _output);
string _res = (new StreamReader(_output.BaseStream)).ReadToEnd();
return _res;
更新2
我发现在C#源代码,做的工作在的 http://code.google.com/p/sharp-lzw/source/browse/
感谢.-
UPDATE 2 I found a source code in C# that does the work at http://code.google.com/p/sharp-lzw/source/browse/ Thanks.-
推荐答案
使用这样的:
private string CompressToLZW(string input)
{
using (MemoryStream stream = new MemoryStream())
{
ComputeLZW(input, stream);
stream.Seek(0, SeekOrigin.Begin);
using (StreamReader reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
}
其中ComputeLZW()是你有LZW方法,使用流。
where ComputeLZW() is the LZW method you have that uses a stream.
这篇关于从字符串C#LZW压缩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!