Deflate String

Chilkat.Compression

 compress = new
 Chilkat.Compression

();

// Any string argument automatically begins a 30-day trial.
bool success;
success = compress.UnlockComponent ("30-day trial" );
if (success != true ) {
MessageBox.Show("Compression component unlock failed" );
return ;
}

compress.Algorithm = "deflate" ;

string strData;
int i;

// Create a long string to deflate.
strData = "abcdefg1122334455667788" ;
strData = strData + "/r/n" ;
for (i = 0 ; i <= 99 ; i++) {
strData = strData + "abcdefg1122334455667788" ;
strData = strData + "/r/n" ;
}

// Depending on the programming language, the string
// may be Unicode. Tell the component to convert to
// ansi (1-byte/char) prior to compression:
compress.Charset = "ansi" ;

// Compressed data is binary, meaning that all byte values
// from 0 to 255 are possible. The result would not be
// a printable string. To receive a printable string (us-ascii),
// the resultant compressed data should be encoded using
// an encoding such as base64. Set the EncodingMode
// property to determine the output encoding:
// (Valid choices are "base64", "hex", "url", and others)
compress.EncodingMode = "base64" ;

// Note: Base64 encoding expands the compressed data by
// 4/3rds because each 3 bytes of binary data are represented
// by 4 printable chars. Hex, for example, doubles the output
// size because each byte is represented by 2 printable chars.

// Deflate the string:
string deflatedStr;
deflatedStr = compress.CompressStringENC (strData);
if (deflatedStr == null ) {
MessageBox.Show(compress.LastErrorText );
return ;
}

// Display the deflated string.
// The result will be:
// S0xKTklNSzc0NDIyNjYxMTU1MzM3t7Dg5UoclRiVGJUYlRiVGJUYlRiVGJUYchIA
textBox1.Text += deflatedStr + "/r/n";

// Decompress back to the original:
string strOriginal;
strOriginal = compress.DecompressStringENC (deflatedStr);

// Display the uncompressed original:
textBox1.Text += "--- Original ---" + "/r/n";
textBox1.Text += strOriginal + "/r/n";
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
缩,有什么推荐的库或者工具吗? 对于Java实现string字符串的deflate算法的压缩和解压缩,您可以考虑使用Java自带的压缩库java.util.zip中的Deflater和Inflater类。下面是一个简单的样例代码,可以实现对字符串进行deflate压缩和解压缩: ```java import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.zip.Deflater; import java.util.zip.Inflater; public class StringDeflater { public static byte[] compress(String str) throws IOException { byte[] input = str.getBytes(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); Deflater compresser = new Deflater(); compresser.setInput(input); compresser.finish(); byte[] buffer = new byte[1024]; while (!compresser.finished()) { int count = compresser.deflate(buffer); baos.write(buffer, 0, count); } compresser.end(); return baos.toByteArray(); } public static String decompress(byte[] input) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); Inflater decompresser = new Inflater(); decompresser.setInput(input); byte[] buffer = new byte[1024]; while (!decompresser.finished()) { int count = decompresser.inflate(buffer); baos.write(buffer, 0, count); } decompresser.end(); return baos.toString(); } public static void main(String[] args) throws IOException { String str = "Hello, world!"; System.out.println("Original string: " + str); byte[] compressed = compress(str); System.out.println("Compressed string: " + new String(compressed, "UTF-8")); String decompressed = decompress(compressed); System.out.println("Decompressed string: " + decompressed); } } ``` 在这个样例代码中,compress和decompress分别实现了数据的压缩和解压缩,使用Deflater和Inflater类来完成deflate操作。您可以通过调用compress方法将字符串压缩,并将返回值存储到byte数组中,也可以使用decompress方法解压缩byte数组,并将解压缩后的字符串返回。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值