当前位置:编程学习 > C#/ASP.NET >>

C# 创建XML并保存到Zip文件中

C# 创建XML并保存到Zip文件中
--------------------编程问答-------------------- 创建XML方法很多,可利用System.Xml类进行操作。
http://www.cnblogs.com/dreamof/archive/2008/08/14/1268096.html

Zip文件可system.io.compression类进行操作。
http://www.jscode.cn/develop/aspnet/v41130 --------------------编程问答-------------------- 关键是ZIP文件生成 --------------------编程问答-------------------- ICSharpCode.SharpZipLib.dll --------------------编程问答-------------------- 第三方Zip库 --------------------编程问答-------------------- 创建 xml 直接 File.Create 就行
保存压缩包 用
Ionic.Zip.dll --------------------编程问答-------------------- 这里通过调用 WinRAR 压缩为 rar 格式了,可以参考下,改成压为 zip 格式。


        private void CallWinRARButton_Click(object sender, EventArgs e)
        {

            DirectoryInfo di = new DirectoryInfo(FromDirTextBox.Text);
            FileInfo[] allXML = di.GetFiles("*.xml", SearchOption.AllDirectories);
            if (allXML.Length == 0)
                ExitButton.PerformClick();

            foreach (FileInfo fi in allXML)
            {
                
                if (DateTime.Now.Subtract(fi.CreationTime).Days >= IdleDaysNumericUpDown.Value)
                {
                    Source = fi.FullName;
                    try
                    {
                        DestiDir = fi.DirectoryName.Replace(fi.Directory.Parent.Parent.FullName, ToDirTextBox.Text.TrimEnd((char[])"\\".ToCharArray()));
                    }
                    catch
                    {
                        continue;
                    }

                    if (!Directory.Exists(DestiDir))
                        Directory.CreateDirectory(DestiDir);

                    Desti = Path.ChangeExtension(Path.Combine(DestiDir, fi.Name), "rar");
                    if (File.Exists(Desti))
                        File.Delete(Desti);

                    DoCompress(fi, Desti, Source);
                }
            }

            // 自动退出
            ExitButton.PerformClick();
        }

        private void DoCompress(FileInfo fi, String Desti, String Source)
        {
            using (Process p = new Process())
            {
                Desti = '"' + Desti + '"';
                Source = '"' + Source + '"';
                p.StartInfo = new ProcessStartInfo(winRARDir, @" a -ibck " + Desti + " " + Source);
                p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                p.StartInfo.CreateNoWindow = true;
                p.StartInfo.UseShellExecute = false;
                p.Start();//生成压缩文件
                p.WaitForExit();
                if (p.HasExited)
                {
                    if (p.ExitCode == 0)
                    {
                        //正常完成
                        try
                        {
                            fi.Delete();
                        }
                        catch
                        {

                        }
                    }
                    else
                    {
                        //有错
                    }

                }
                p.Close();
            }
        }

补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,