这几天在做把数据库里的数据 导出 并且压缩 学到了一些东西

 

导出xml 刚开始想到的是 用XmlDocument 动态建立xml文件, 并对数据库里的数据遍历 动态生成table 这种方法太麻烦了。。。。 菜鸟的弯路还真多啊。

 然后 找到了 dataset 直接就有方法导出 带字段的和 数据的方法  

DataSet dt = new DataSet();

dt.WriteXml(xxfliepath, XmlWriteMode.WriteSchema);

其中xxfliepath 是已经定义好的文件的绝对路径加上文件的名字

这里我又遇到的路径的问题 通过相对路径获取绝对路径, 如果相对路径和绝对路径的感念不清楚 可以百度查一查 string xmlpath = this.Server.MapPath(@"FilesUpload)

然后做文件的压缩 刚开始找到了一个压缩类,但是不知道为什么传参数的时候 怎么传都找不到文件 可以分享一下这个类

#region .rar文件帮助类
public class FileRar
{

    static public bool Exists()
    {
        RegistryKey the_Reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe");
        return !string.IsNullOrEmpty(the_Reg.GetValue("").ToString());
    }


    ///// <summary>
    ///// 压缩文件
    ///// </summary>
    ///// <param name="DFilePath">需要压缩的文件夹或者单个文件</param>
    ///// <param name="DRARName">生成压缩文件的文件名</param>
    ///// <param name="DRARPath">生成压缩文件保存路径</param>
    ///// <returns></returns>
    //public static bool RAR(string DFilePath, string DRARName, string DRARPath)
    //{
    //    String the_rar;
    //    RegistryKey the_Reg;
    //    Object the_Obj;
    //    String the_Info;
    //    ProcessStartInfo the_StartInfo;
    //    Process the_Process;
    //   // DRARPath += DRARName;
    //    //DRARName = DRARPath + DRARName;
       
    //    try
    //    {
    //        the_Reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe");
    //        the_Obj = the_Reg.GetValue("");
    //        the_rar = the_Obj.ToString();
    //        the_Reg.Close();
    //        the_rar = the_rar.Substring(1, the_rar.Length - 7);
    //        the_Info = " a    " + " " + DRARName + "  " + DFilePath; //命令 + 压缩后文件名 + 被压缩的文件或者路径
    //        the_StartInfo = new ProcessStartInfo();
    //        the_StartInfo.FileName = the_rar;
    //        the_StartInfo.Arguments = the_Info;
    //        the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    //        the_StartInfo.WorkingDirectory = DRARPath; //RaR文件的存放目录。
    //        the_Process = new Process();
    //        the_Process.StartInfo = the_StartInfo;
    //        the_Process.Start();
    //        return true;
    //    }
    //    catch (Exception ex)
    //    {
      
    //        return false;
    //    }
    //}

    /// <summary>
    /// 解压
    /// </summary>
    /// <param name="unRarPatch">解压文件路径</param>
    /// <param name="rarPatch">压缩文件路径</param>
    /// <param name="rarName">压缩文件名称</param>
    /// <returns></returns>
    public static bool unCompressRAR(string unRarPatch, string rarPatch, string rarName, out string err)
    {
        string the_rar;
        RegistryKey the_Reg;
        object the_Obj;
        string the_Info;
        bool flag = false;
        err = "";
        try
        {
            the_Reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe");
            the_Obj = the_Reg.GetValue("");
            the_rar = the_Obj.ToString();
            the_Reg.Close();
            //the_rar = the_rar.Substring(1, the_rar.Length - 7);

            if (Directory.Exists(unRarPatch) == false)
            {
                Directory.CreateDirectory(unRarPatch);
            }
            the_Info = "x " + rarName + " " + unRarPatch + " -y";

            ProcessStartInfo the_StartInfo = new ProcessStartInfo();
            the_StartInfo.FileName = the_rar;
            the_StartInfo.Arguments = the_Info;
            the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            the_StartInfo.WorkingDirectory = rarPatch;//获取压缩包路径

            Process the_Process = new Process();
            the_Process.StartInfo = the_StartInfo;
            the_Process.Start();
            the_Process.WaitForExit();
            the_Process.Close();
            flag = true;
        }
        catch (Exception ex)
        {
            throw ex;
            err = ex.Message.ToString();
            // Logger.writeLogger("解压文件出现异常" + ex.Message.ToString());
        }
        return flag;
    }
}

然后想到的是调用bat 命令 但是在动态 写bat命令的时候总是出错

最后在网上直接找到SharpZipLib

然后写了个压缩方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ICSharpCode.SharpZipLib.Zip;
using System.IO;
using ICSharpCode.SharpZipLib.Checksums;


/// <summary>
///zipheper 的摘要说明
/// </summary>
public class zipheper
{
	public zipheper()
	{
		//
		//TODO: 在此处添加构造函数逻辑
		//
	}
    public static void ZipFile(string filefolder, string zipfilename)
    {

            if (Directory.Exists(filefolder))
            {
                FastZip fastZip = new FastZip();
                //zip filename is full file name
                fastZip.CreateZip(zipfilename, filefolder, true, "");
            }
        //if (strFile[strFile.Length - 1] != Path.DirectorySeparatorChar)
        //    strFile += Path.DirectorySeparatorChar;
        //ZipOutputStream s = new ZipOutputStream(File.Create(strZip));
        //s.SetLevel(9); // 0 - store only to 9 - means best compression
        //zip(strFile, s, "D:\\TDDOWNLOAD\\photos.aa");
        //s.Finish();
        //s.Close();
    }

    private static void zip(string strFile, ZipOutputStream s, string staticFile)
    {
        if (strFile[strFile.Length - 1] != Path.DirectorySeparatorChar)
            strFile += Path.DirectorySeparatorChar;
        Crc32 crc = new Crc32();
        string[] filenames = Directory.GetFileSystemEntries(strFile);
        foreach (string file in filenames)
        {
            if (Directory.Exists(file))
            {
                zip(file, s, staticFile);
            }

            else // 否则直接压缩文件
            {
                //打开压缩文件
                FileStream fs = File.OpenRead(file);

                byte[] buffer = new byte[fs.Length];
                fs.Read(buffer, 0, buffer.Length);
                string tempfile = file.Substring(staticFile.LastIndexOf("\\") + 1);
                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);
            }
        }
    }
}

  

原文地址:https://www.cnblogs.com/haihang/p/2826709.html