[C#] C#解压缩单个zip格式文件 →→→→→进入此内容的聊天室

来自 , 2019-05-01, 写在 C#, 查看 118 次.
URL http://www.code666.cn/view/3d57fe6d
  1. using System.IO;
  2. using System.IO.Compression;
  3. string sourceFile=@"D:\2.zip";
  4. string destinationFile=@"D:\1.txt";
  5.             private const long BUFFER_SIZE = 20480;
  6.             // make sure the source file is there
  7.             if (File.Exists ( sourceFile ))
  8.             {
  9.             FileStream sourceStream = null;
  10.             FileStream destinationStream = null;
  11.             GZipStream decompressedStream = null;
  12.             byte[] quartetBuffer = null;
  13.             try
  14.             {
  15.                 // Read in the compressed source stream
  16.                 sourceStream = new FileStream ( sourceFile, FileMode.Open );
  17.  
  18.                 // Create a compression stream pointing to the destiantion stream
  19.                 decompressedStream = new DeflateStream ( sourceStream, CompressionMode.Decompress, true );
  20.  
  21.                 // Read the footer to determine the length of the destiantion file
  22.                 quartetBuffer = new byte[4];
  23.                 int position = (int)sourceStream.Length - 4;
  24.                 sourceStream.Position = position;
  25.                 sourceStream.Read ( quartetBuffer, 0, 4 );
  26.                 sourceStream.Position = 0;
  27.                 int checkLength = BitConverter.ToInt32 ( quartetBuffer, 0 );
  28.  
  29.                 byte[] buffer = new byte[checkLength + 100];
  30.  
  31.                 int offset = 0;
  32.                 int total = 0;
  33.  
  34.                 // Read the compressed data into the buffer
  35.                 while ( true )
  36.                 {
  37.                     int bytesRead = decompressedStream.Read ( buffer, offset, 100 );
  38.  
  39.                     if ( bytesRead == 0 )
  40.                         break;
  41.  
  42.                     offset += bytesRead;
  43.                     total += bytesRead;
  44.                 }
  45.  
  46.                 // Now write everything to the destination file
  47.                 destinationStream = new FileStream ( destinationFile, FileMode.Create );
  48.                 destinationStream.Write ( buffer, 0, total );
  49.  
  50.                 // and flush everyhting to clean out the buffer
  51.                 destinationStream.Flush ( );
  52.             }
  53.             catch ( ApplicationException ex )
  54.             {
  55.                 Console.WriteLine(ex.Message, "解压文件时发生错误:");
  56.             }
  57.             finally
  58.             {
  59.                 // Make sure we allways close all streams
  60.                 if ( sourceStream != null )
  61.                     sourceStream.Close ( );
  62.  
  63.                 if ( decompressedStream != null )
  64.                     decompressedStream.Close ( );
  65.  
  66.                 if ( destinationStream != null )
  67.                     destinationStream.Close ( );
  68.             }
  69.             }
  70.  
  71. //csharp/1113

回复 "C#解压缩单个zip格式文件"

这儿你可以回复上面这条便签

captcha