不是吧,没有人知道。
看来只有我自己研究了。
好像在压缩文件的时候有问题。
不过压缩字符串还是不错的,好像是字符串的可逆加密。
大家可以看看。
using System;
using System.Collections.Generic;
using System.Text;
using System.IO.Compression;
using System.IO;
namespace ConsoleApplication4
{
class Program
{
//压缩字符串
private static string zipStrFunction(string zipstr)
{
byte[] btdata = System.Text.UTF8Encoding.UTF8.GetBytes(zipstr);
MemoryStream ms = new MemoryStream();
Stream s = new GZipStream(ms, CompressionMode.Compress);
s.Write(btdata, 0, btdata.Length);
s.Close();
byte[] compressedData = (byte[])ms.ToArray();
return System.Convert.ToBase64String(compressedData, 0, compressedData.Length);
}
//解压缩字符串
public static string unzipFunction(string unzipstr)
{
System.Text.StringBuilder uncompressedString = new System.Text.StringBuilder();
byte[] writeData = new byte[4096];
byte[] bytData = System.Convert.FromBase64String(unzipstr);
int size = 0;
MemoryStream ms = new MemoryStream(bytData);
GZipStream zipstream = new GZipStream(ms, CompressionMode.Decompress);
while ((size = zipstream.Read(writeData, 0, writeData.Length))>0)
{
uncompressedString.Append(System.Text.Encoding.UTF8.GetString(writeData, 0, size));
}
zipstream.Close();
return uncompressedString.ToString();
}
public static void Main(string[] args)
{
string zipStr, compressedStr,uncompressedStr;
Console.WriteLine("输入要压缩的字符串");
zipStr = Console.ReadLine();
compressedStr = zipStrFunction(zipStr);
Console.WriteLine("压缩后的:");
Console.WriteLine(compressedStr);
Console.WriteLine("解压:");
uncompressedStr = unzipFunction(compressedStr);
Console.WriteLine(uncompressedStr);
Console.ReadLine();
}
}
}