压缩/解压封装的类
来源:互联网 发布:放置江湖ios数据修改 编辑:程序博客网 时间:2024/06/05 03:24
//主要功能是可以用来减轻网络数据的传输数据量,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 ( ); } }
0 0
- 压缩/解压封装的类
- 用.NET 2.0压缩/解压封装的类
- C#中关于zip压缩解压帮助类的封装
- CAB文件压缩解压封装类
- 一个封装了 7z 压缩解压功能的接口
- GZip 压缩解压 --- Pako 的压缩解压
- linux的压缩解压
- GZIP压缩解压类
- GZIP压缩解压类
- 压缩与解压2---文件的压缩
- 流的压缩和解压
- 文件的压缩和解压
- 字符串的压缩和解压
- 压缩文件的压缩和解压
- 字符串的压缩与解压
- Huffman压缩解压的思考
- Linux的压缩与解压
- zip 的 压缩与解压
- OC中的栈和堆
- linux下的多线程学习
- 代码操作XML(增删改)
- java配置环境变量问题,环境变量都正确了,但是JAVA_HOME不能识别,导致javac java -version不出现
- 上传本地代码到github
- 压缩/解压封装的类
- KMP算法——C++实现版
- sql server系统表的用法
- 欢迎界面iOS——初学者福利
- 第一次来到CSDN
- 在T-SQL语句中访问远程数据库(openrowset/opendatasource/openquery)
- 大话#import和@class区别
- 古典音乐与R数据分析
- soj 3296: Windy's S