C#中实现压缩文件和文件夹 .

来源:互联网 发布:免费手机工作日志软件 编辑:程序博客网 时间:2024/06/05 17:59
 
  1. 【【【【C#压缩文件】】】】  
  2. 方法1:  
  3.     //【filepath想要压缩文件的地址】   
  4.     //【zippath输出压缩文件的地址】   
  5.     private void GetFileToZip(string filepath,string zippath)  
  6.     {  
  7.   
  8.         FileStream fs = File.OpenRead(filepath);  
  9.             byte[] buffer = new byte[fs.Length];  
  10.             fs.Read(buffer, 0, buffer.Length);  
  11.             fs.Close();  
  12.   
  13.             FileStream ZipFile = File.Create(zippath);  
  14.             ZipOutputStream ZipStream = new ZipOutputStream(ZipFile);  
  15.             ZipEntry ZipEntry = new ZipEntry(输出的文件名称);  
  16.             ZipStream.PutNextEntry(ZipEntry);  
  17.             ZipStream.SetLevel(6);  
  18.   
  19.             ZipStream.Write(buffer, 0, buffer.Length);  
  20.             ZipStream.Finish();  
  21.             ZipStream.Close();  
  22.     }  
  23. 方法2:  
  24.      private void FileToZip(string path,string address)   
  25.         {  
  26.             string name = openFileDialog1.FileName.Substring(openFileDialog1.FileName.LastIndexOf("\\")+1);  
  27.             FileStream StreamToZip = new FileStream(path, FileMode.Open, FileAccess.Read);  
  28.             FileStream ZipFile = File.Create(address);  
  29.             ZipOutputStream ZipStream = new ZipOutputStream(ZipFile);  
  30.             //压缩文件   
  31.             ZipEntry ZipEntry = new ZipEntry(name);  
  32.             ZipStream.PutNextEntry(ZipEntry);  
  33.             ZipStream.SetLevel(6);                                         
  34.             byte[] buffer = new byte[StreamToZip.Length];  
  35.             StreamToZip.Read(buffer, 0, Convert.ToInt32(StreamToZip.Length));  
  36.             ZipStream.Write(buffer, 0, Convert.ToInt32(StreamToZip.Length));  
  37.             ZipStream.Finish();  
  38.             ZipStream.Close();  
  39.             StreamToZip.Close();  
  40.   
  41.         }  
  42. 【【【【【【C#解压文件】】】】】】  
  43.     private void ZipToFile(string path, string addres)  
  44.         {  
  45.             ZipInputStream s = new ZipInputStream(File.OpenRead(path));  
  46.             ZipEntry fileEntry;   
  47.             while ((fileEntry = s.GetNextEntry()) != null)  
  48.             {  
  49.                 string filename = Path.GetFileName(fileEntry.Name);  
  50.                 if (filename != "")  
  51.                 {  
  52.                     filename = addres + "\\" + filename;  
  53.                     FileStream streamWriter = File.Create(filename);  
  54.                     int size = 2048;  
  55.                     byte[] buffer = new byte[s.length];  
  56.   
  57.                     size = s.Read(buffer, 0, size);  
  58.                     streamWriter.Write(buffer, 0, size);  
  59.                     streamWriter.Close();                     
  60.                 }  
  61.             }  
  62.             s.Close();  
  63.         }  
  64. 【【【【【【C#压缩目录】】】】】】  
  65. 方法1:  
  66.     //【arg[0]要压缩的目录】   
  67.     //【arg[1]输出的压缩文件】   
  68.        private void DirectoryToZip(string path, string address)  
  69.            {  
  70.              //获取当前文件夹中所有的文件   
  71.                 string[] filenames = Directory.GetFiles(path);    
  72.              Crc32 crc = new Crc32();  
  73.              //创建输出文件(ZIP格式的文件)  
  74.              ZipOutputStream zos = new ZipOutputStream(File.Create(address));  
  75.               zos.SetLevel(6);  
  76.              //遍历所有的文件   
  77.              foreach (string name in filenames)  
  78.              {  
  79.                     FileStream fs = File.OpenRead(name);  
  80.                    byte[] buffer = new byte[fs.Length];  
  81.                   //读取文件   
  82.                    fs.Read(buffer, 0, Convert.ToInt32(fs.Length));  
  83.                    //获取文件的文件名称和后缀名  
  84.                   string file = Path.GetFileName(name);  
  85.                   //输出文件的名称   
  86.                   ZipEntry entry = new ZipEntry(file);                  
  87.                   crc.Reset();  
  88.                   crc.Update(buffer);  
  89.                  entry.Crc = crc.Value;  
  90.                  zos.PutNextEntry(entry);  
  91.                  zos.Write(buffer, 0, Convert.ToInt32(fs.Length));  
  92.                  fs.Close();  
  93.               }  
  94.               zos.Finish();  
  95.                zos.Close();  
  96.             }  
  97.   
  98.   
  99.   
  100. 【【【【【【【【C#读取压缩文件(将压缩文件转换为二进制)】】】】】】】】  
  101.     private void GetZipToByte(){  
  102.         string path = @"C:\Documents and Settings\Administrator\桌面\文件.rar";  
  103.             FileStream fs = new FileStream(path, FileMode.Open);  
  104.             bytes = new byte[fs.Length];  
  105.             int count = Convert.ToInt32(fs.Length);  
  106.             fs.Read(bytes, 0, count);  
  107.     }  
  108. 【【【【【【【【C#将二进制转换为压缩文件】】】】】】】】  
  109.     private void GetByteToZip()  
  110.     {  
  111.         string path = @"F:\dom.rar";//压缩文件的地址  
  112.         File.WriteAllBytes(path, bytes);  
  113.     }  
原创粉丝点击