压缩文件

1.引用 ICSharpCode.SharpZipLib.dll组件
2.

/// <summary>
  /// 将指定的文件打入包中
  /// </summary>
  /// <param name="l_sZipFile">压缩生成文件</param>
  /// <param name="l_sWaitZipFile">待压缩文件</param>
  /// <param name="blnDelWaitFile">是否删除待压缩文件</param>
  /// <param name="sPassWord">压缩文件口令,为“”不用口令</param>
  public static void ZipFile(string l_sZipFile,string[] l_sWaitZipFile,bool blnDelWaitFile,string sPassWord)
  {
   
   System.IO.FileStream myFileStream = new System.IO.FileStream(l_sZipFile, System.IO.FileMode.Create);    
   ICSharpCode.SharpZipLib.Zip.ZipOutputStream zos=new ZipOutputStream(myFileStream);

   if(sPassWord.Length>0)
   {
    zos.Password=sPassWord;
   }
   
   FileStream myFileStream1;
   for(int i=0;i<l_sWaitZipFile.Length;i++)
   {
    if (!System.IO.File.Exists(l_sWaitZipFile[i])) continue;

    myFileStream1=new FileStream( l_sWaitZipFile[i],System.IO.FileMode.Open);

    byte[] buffer = new byte[myFileStream1.Length];
    myFileStream1.Read(buffer, 0, buffer.Length);
    ZipEntry entry = new ZipEntry(Path.GetFileName(l_sWaitZipFile[i]));
    zos.PutNextEntry(entry);

    zos.Write(buffer, 0, buffer.Length);
    myFileStream1.Close();

    if(blnDelWaitFile)
    {
     File.Delete( l_sWaitZipFile[i]);
    }
   
   }
   zos.Close();    
    
   myFileStream.Close();
  
  }

你可能感兴趣的:(文件)