知识大全 c#2.0中新增的两个压缩类
Posted 知
篇首语:人还是要乐观,心碎了就对自己说,碎碎平安。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 c#2.0中新增的两个压缩类相关的知识,希望对你有一定的参考价值。
class clsZip 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 pressedStream = 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 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 pression stream pointing to the destiantion stream pressedStream = new GZipStream ( destinationStream CompressionMode Compress true ); // Now write the pressed data to the destination file pressedStream Write ( buffer 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 ( pressedStream != null ) pressedStream Close ( ); if ( destinationStream != null ) destinationStream Close ( ); public void DepressFile ( 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 depressedStream = null; byte[] quartetBuffer = null; try // Read in the pressed source stream sourceStream = new FileStream ( sourceFile FileMode Open ); // Create a pression stream pointing to the destiantion stream depressedStream = new GZipStream ( sourceStream CompressionMode Depress true ); // Read the footer to determine the length of the destiantion file quartetBuffer = new byte[ ]; int position = (int)sourceStream Length ; sourceStream Position = position; sourceStream Read ( quartetBuffer ); sourceStream Position = ; int checkLength = BitConverter ToInt ( quartetBuffer ); byte[] buffer = new byte[checkLength + ]; int offset = ; int total = ; // Read the pressed data into the buffer while ( true ) int bytesRead = depressedStream Read ( buffer offset ); if ( bytesRead == ) break; offset += bytesRead; total += bytesRead; // Now write everything to the destination file destinationStream = new FileStream ( destinationFile FileMode Create ); destinationStream Write ( buffer 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 ( depressedStream != null ) depressedStream Close ( ); if ( destinationStream != null ) destinationStream Close ( ); cha138/Article/program/net/201311/12971相关参考