//主要功能是可以用来减轻网络数据的传输数据量,
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.IO;
using System.IO.Compression;
namespace CompressionService
{
public class Compression
{
//压缩
public int Compress(byte[] source, ref byte[] dest)
{
int resultcode;
try
{
MemoryStream ms = new MemoryStream();
//Use the Newly Created Memory Stream for the Compressed Data.
GZipStream compressedzipStream = new GZipStream(ms, CompressionMode.Compress, true);
compressedzipStream.Write(source, 0, source.Length);
compressedzipStream.Close();
ms.Position = 0;
dest = ms.ToArray();
ms.Close();
resultcode = 1;
}
catch
{
resultcode = 0;
}
return resultcode;
}
//解压
public int UnCompress(byte[] source, ref byte[] dest)
{
int resultcode;
try
{
MemoryStream tempms = new MemoryStream(source);
tempms.Position = 0;
GZipStream tempzip = new GZipStream(tempms, CompressionMode.Decompress);
dest = GetDeData(tempzip, source);
tempzip.Close();
//tempms.Close();
// Console.WriteLine("临时解码数据:{0}", System.Text.Encoding.Default.GetString(dest));
resultcode = 1;
}
catch
{
resultcode = 0;
}
return resultcode;
}
public byte[] GetDeData(Stream stream,byte[] temp) //取得解压后的文件数据
{
int totalcount = 0;
int datalen = temp.Length;
int bytecount = datalen;
while (true)
{
byte[] tempdata = new byte[bytecount];
int bytesread = stream.Read(tempdata, 0, bytecount);
if (bytesread == 0)
break;
totalcount += bytesread;
bytecount =totalcount+bytesread;
}
stream.Close();
MemoryStream ms = new MemoryStream(temp);
GZipStream gs = new GZipStream(ms, CompressionMode.Decompress);
byte[] alltempdata =new byte[totalcount];
gs.Read(alltempdata, 0, alltempdata.Length);
gs.Close();
return alltempdata;
}
}
}
调用类P如下:
public static void Main()
{
CompressionService.Compression ct = new CompressionService.Compression();
byte[] origialdata = OpenFile();
Console.WriteLine("原始数据长度:{0}", origialdata.Length);
byte[] test = null;
ct.Compress(origialdata, ref test);
Console.WriteLine("压缩后数据长度:{0}", test.Length);
byte[] uncompressdata = null;
ct.UnCompress(test,ref uncompressdata);
Console.WriteLine("解压数据:{0},大小为:{1}",System.Text.Encoding.Default.GetString(uncompressdata).ToString(),uncompressdata.Length);
}
public static byte[] OpenFile()
{
//string test = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzddafdsfsfsfsdafdfa,我们是中国人";
FileStream infile = new FileStream("C:\\test.txt", FileMode.Open, FileAccess.Read, FileShare.Read);
byte[] buffer = new byte[infile.Length];
infile.Read(buffer, 0, buffer.Length);
Console.WriteLine("原始数据:{0}", System.Text.Encoding.Default.GetString(buffer));
// System.Text.Encoding.ASCII.GetString(test ));
return buffer;
}
如果用来在线压缩和解压缩文件,如下:
public void CompressFile (string sourceFile, string destinationFile )
{
// make sure the source file is there
if ( File.Exists ( sourceFile ) == false )
throw new FileNotFoundException ( );
// Create the streams and byte arrays needed
byte[] buffer = null;
FileStream sourceStream = null;
FileStream destinationStream = null;
GZipStream compressedStream = null;
try
{
// Read the bytes from the source file into a byte array
sourceStream = new FileStream ( sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read );
// Read the source stream values into the buffer
buffer = new byte[sourceStream.Length];
int checkCounter = sourceStream.Read ( buffer, 0, buffer.Length );
if ( checkCounter != buffer.Length )
{
throw new ApplicationException ( );
}
// Open the FileStream to write to
destinationStream = new FileStream ( destinationFile, FileMode.OpenOrCreate, FileAccess.Write );
// Create a compression stream pointing to the destiantion stream
compressedStream = new GZipStream ( destinationStream, CompressionMode.Compress, true );
// Now write the compressed data to the destination file
compressedStream.Write ( buffer, 0, buffer.Length );
}
catch ( ApplicationException ex )
{
// MessageBox.Show ( ex.Message, "压缩文件时发生错误:", MessageBoxButtons.OK, MessageBoxIcon.Error );
}
finally
{
// Make sure we allways close all streams
if ( sourceStream != null )
sourceStream.Close ( );
if ( compressedStream != null )
compressedStream.Close ( );
if ( destinationStream != null )
destinationStream.Close ( );
}
}
public void DecompressFile ( string sourceFile, string destinationFile )
{
// make sure the source file is there
if ( File.Exists ( sourceFile ) == false )
throw new FileNotFoundException ( );
// Create the streams and byte arrays needed
FileStream sourceStream = null;
FileStream destinationStream = null;
GZipStream decompressedStream = null;
byte[] quartetBuffer = null;
try
{
// Read in the compressed source stream
sourceStream = new FileStream (sourceFile, FileMode.Open );
// Create a compression stream pointing to the destiantion stream
decompressedStream = new GZipStream (sourceStream, CompressionMode.Decompress, true );
// Read the footer to determine the length of the destiantion file
quartetBuffer = new byte[4];
int position = (int)sourceStream.Length - 4;
sourceStream.Position = position;
sourceStream.Read (quartetBuffer, 0, 4 );
sourceStream.Position = 0;
int checkLength = BitConverter.ToInt32 ( quartetBuffer, 0 );
byte[] buffer = new byte[checkLength + 100];
int offset = 0;
int total = 0;
// Read the compressed data into the buffer
while ( true )
{
int bytesRead = decompressedStream.Read ( buffer, offset, 100 );
if ( bytesRead == 0 )
break;
offset += bytesRead;
total += bytesRead;
}
// Now write everything to the destination file
destinationStream = new FileStream ( destinationFile, FileMode.Create );
destinationStream.Write ( buffer, 0, total );
// and flush everyhting to clean out the buffer
destinationStream.Flush ( );
}
catch ( ApplicationException ex )
{
//MessageBox.Show(ex.Message, "解压文件时发生错误:", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
// Make sure we allways close all streams
if ( sourceStream != null )
sourceStream.Close ( );
if ( decompressedStream != null )
decompressedStream.Close ( );
if ( destinationStream != null )
destinationStream.Close ( );
}
}
压缩/解压封装的类
最新推荐文章于 2023-11-29 16:12:00 发布