• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

C#压缩单个zip格式文件

OC/C/C++ 水墨上仙 1872次浏览

C#压缩单个zip格式文件


using System.IO;
using System.IO.Compression;
string sourceFile=@"C:\1.txt";
string destinationFile=@"D:\2.zip";
	private const long BUFFER_SIZE = 20480;
       if ( File.Exists ( sourceFile ))
       {
            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 );

                // 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 DeflateStream ( destinationStream, CompressionMode.Compress, true );
            long bufferSize = sourceStream.Length < BUFFER_SIZE ? sourceStream.Length : BUFFER_SIZE;   
            byte[] buffer = new byte[bufferSize];   
            int bytesRead = 0;   
            long bytesWritten = 0;   
            while ((bytesRead = sourceStream.Read(buffer, 0, buffer.Length)) != 0)   
            {   
                compressedStream.Write(buffer, 0, bytesRead);   
                bytesWritten += bufferSize;   
            }   

            }
            catch ( ApplicationException ex )
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                // Make sure we allways close all streams
                if ( sourceStream != null )
                    sourceStream.Close ( );

                if ( compressedStream != null )
                    compressedStream.Close ( );

                if ( destinationStream != null )
                    destinationStream.Close ( );
            }
        }


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明C#压缩单个zip格式文件
喜欢 (0)
加载中……