小方法收藏

  /// <summary>
  /// 根据数据类型确定格式
  /// </summary>
  /// <param name="ob"></param>
  /// <returns></returns>
  private static string ByType(object ob)
  {
   if(ob.GetType()==Type.GetType("System.DBNull"))
   {
    return "NULL";
   }
   if(ob.GetType()==Type.GetType("System.Decimal") ||ob.GetType()==Type.GetType("System.Double")||ob.GetType() == Type.GetType("System.Single"))
   {
    return ob.ToString();
    
   }
   else
   {
    return " '" + ChangeSTR(ob.ToString()) +"'";
   }
  }

 

 


  /// <summary>
  /// 压缩文件
  /// </summary>
  /// <param name="filePath">文件路径</param>
  /// <param name="fileName">文件名称</param>
  private static void ZipFile(string filePath,string fileName)
  {
   //Crc32算法
   Crc32 crc = new Crc32();
   ZipOutputStream s = new ZipOutputStream(File.Create(filePath+@"/"+fileName+".ZIP"));
   s.SetLevel(6); // (0~9)
  
   string[] filenames = Directory.GetFileSystemEntries(filePath);
   foreach (string file in filenames)
   {
    //打开压缩文件,开始压缩
    if(file.ToUpper().EndsWith(".XML"))
    {
     FileStream fs = File.OpenRead(file);
     byte[] buffer = new byte[fs.Length];
     fs.Read(buffer, 0, buffer.Length);
     string tempfile = file.Replace(filePath+@"/","");
     ZipEntry entry = new ZipEntry(tempfile);
     entry.DateTime = DateTime.Now;
     entry.Size = fs.Length;
     fs.Close();
     crc.Reset();
     crc.Update(buffer);
     entry.Crc = crc.Value;
     s.PutNextEntry(entry);
     s.Write(buffer, 0, buffer.Length);
     File.Delete(file);
    }
   } 
   s.Finish();
   s.Close();
  }

 


  /// ;summary;
  /// 解压缩一个 zip 文件。
  /// ;/summary;
  /// ;param name="zipFileName";要解压的 zip 文件。;/param;
  /// ;param name="extractLocation";zip 文件的解压目录。;/param;
  /// ;param name="password";zip 文件的密码。;/param;
  /// ;param name="overWrite";是否覆盖已存在的文件。;/param;
  public static string UnZipDir(string zipFileName, string extractLocation, string password, bool overWrite)
  {
   string unzipFile = string.Empty;
   #region 实现
   string myExtractLocation = extractLocation;
   if (myExtractLocation == "")
    myExtractLocation = Directory.GetCurrentDirectory();
   if (!myExtractLocation.EndsWith(@"/"))
    myExtractLocation = myExtractLocation + @"/";
   
   ZipInputStream s = new ZipInputStream(File.OpenRead(zipFileName));
   s.Password = password;
   ZipEntry theEntry;

   try
   {
    while ((theEntry = s.GetNextEntry()) != null)
    {
     string directoryName = string.Empty;
     string pathToZip = string.Empty;
     pathToZip = theEntry.Name;

     if (pathToZip != "")
      directoryName = Path.GetDirectoryName(pathToZip) + @"/";
     string fileName = Path.GetFileName(pathToZip);
     //string fileName = "temp.XML";
     Directory.CreateDirectory(myExtractLocation);
     if (fileName != string.Empty)
     {
      try
      {
       if ((File.Exists(myExtractLocation+ fileName) && overWrite) || (!File.Exists(myExtractLocation + fileName)))
       {
        unzipFile=myExtractLocation + fileName;
        FileStream streamWriter = File.Create(myExtractLocation + fileName);
        int size = 2048;
        byte[] data = new byte[2048];
        while ( true)
        {
         size = s.Read(data, 0, data.Length);
         if (size > 0)
         {
          streamWriter.Write(data, 0, size);
          if(size<2048)
           break;
         }
         else
          break;
        }
        streamWriter.Close();
       }
      }
      catch(ZipException ex)
      {
       FileStream fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "ziplog.txt", FileMode.OpenOrCreate, FileAccess.Write);
       StreamWriter sw = new StreamWriter(fs);
       sw.WriteLine(ex.Message);
      }
     }
     break;
    }
   }
   catch(ZipException ex)
   {
    throw ex;
   }
   catch(Exception e)
   {
    throw e;
   }
   finally
   {
    s.Close();
   }
   
   #endregion
   return unzipFile;
  }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值