• 遍历文件夹及其子文件夹下的.pdf文件,并解压文件夹下所有的压缩包


      List<PDFPATH> pdfpath = new List<PDFPATH>();
            List<string> ziplist = new List<string>();
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                FolderBrowserDialog dialog = new FolderBrowserDialog();
                dialog.Description = "请选择文件路径";
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    string foldPath = dialog.SelectedPath;
                    ListFilesZIPandRaR(new DirectoryInfo(foldPath));
                    foreach (string p in ziplist)
                    {
                        UnpackFile(p, p.Substring(0,p.LastIndexOf('\')));
                    }
    
                    ListFilesPDF(new DirectoryInfo(foldPath));
                    dataGridView1.DataSource = pdfpath;
                }
    
            }
            /// <summary>
            /// 获取文件夹及其子文件夹下的所有pdf
            /// </summary>
            /// <param name="info"></param>
            public void  ListFilesPDF(FileSystemInfo info)
            {
                
                if (!info.Exists) return;
    
                DirectoryInfo dir = info as DirectoryInfo;
                //不是目录
                if (dir == null) return;
    
                FileSystemInfo[] files = dir.GetFileSystemInfos();
                for (int i = 0; i < files.Length; i++)
                {
                    FileInfo file = files[i] as FileInfo;
                    //是文件
                    if (file != null)
                    {
                        //Console.WriteLine(file.FullName + "	 " + file.Length);
                        if (file.FullName.Substring(file.FullName.LastIndexOf(".")) == ".pdf")
                        //此处为显示JPG格式,不加IF可遍历所有格式的文件
                        {
                            PDFPATH p = new PDFPATH();
                            p.path = file.FullName;
                            pdfpath.Add(p );
                        }
    
                    }
                    //对于子目录,进行递归调用
                    else
                    {
                        ListFilesPDF(files[i]);
                    }
    
                }
    
            }
    
            /// <summary>
            /// 获取文件夹及其子文件夹下的所有ZIP,RaR
            /// </summary>
            /// <param name="info"></param>
            public void ListFilesZIPandRaR(FileSystemInfo info)
            {
    
                if (!info.Exists) return;
    
                DirectoryInfo dir = info as DirectoryInfo;
                //不是目录
                if (dir == null) return;
    
                FileSystemInfo[] files = dir.GetFileSystemInfos();
                for (int i = 0; i < files.Length; i++)
                {
                    FileInfo file = files[i] as FileInfo;
                    //是文件
                    if (file != null)
                    {
                        //Console.WriteLine(file.FullName + "	 " + file.Length);
                        if (file.FullName.Substring(file.FullName.LastIndexOf(".")) == ".zip" || file.FullName.Substring(file.FullName.LastIndexOf(".")) == ".rar")
                        //此处为显示JPG格式,不加IF可遍历所有格式的文件
                        {
                            ziplist.Add(file.FullName);
                        }
    
                    }
                    //对于子目录,进行递归调用
                    else
                    {
                        ListFilesZIPandRaR(files[i]);
                    }
    
                }
    
            }
            #region 解压 文件 zip 格式 rar 格式
            /// <summary>
            ///解压文件
            /// </summary>
            /// <param name="fileFromUnZip">解压前的文件路径(绝对路径)</param>
            /// <param name="fileToUnZip">解压后的文件目录(绝对路径)</param>
            public static void UnpackFile(string fileFromUnZip, string fileToUnZip)
            {
                 
                //获取压缩类型
                string unType = fileFromUnZip.Substring(fileFromUnZip.LastIndexOf(".") + 1, 3).ToLower();
                switch (unType)
                {
                    case "rar":
                        UnRar(fileFromUnZip, fileToUnZip);
                        break;
                    case "zip":
                        UnZip(fileFromUnZip, fileToUnZip);
                        break;
                }
            }
            //解压rar格式的文件
            private static void UnRar(string fileFromUnZip, string fileToUnZip)
            {
                using (Process Process1 = new Process())// 开启一个进程 执行解压工作
                {
                    string ServerDir = @"C:Program Files (x86)HaoZipHaoZip.exe";//rar工具的安装路径   必须要安装 WinRAR     //例于:C:Program Files (x86)WinRARRAR.exe
                    Process1.StartInfo.UseShellExecute = false;
                    Process1.StartInfo.RedirectStandardInput = true;
                    Process1.StartInfo.RedirectStandardOutput = true;
                    Process1.StartInfo.RedirectStandardError = true;
                    Process1.StartInfo.CreateNoWindow = true;
                    Process1.StartInfo.FileName = ServerDir;
                    Process1.StartInfo.Arguments = " x -inul -y " + fileFromUnZip + " " + fileToUnZip;
                    Process1.Start();//解压开始  
                    Process1.WaitForExit();
                    Process1.Close();
                }
            }
            // 解压zip 文件
            public static void UnZip(string fileFromUnZip, string fileToUnZip)
            {
                ZipInputStream inputStream = new ZipInputStream(File.OpenRead(fileFromUnZip));
                ZipEntry theEntry;
                while ((theEntry = inputStream.GetNextEntry()) != null)
                {
                    fileToUnZip += "/";
                    string fileName = Path.GetFileName(theEntry.Name);
                    string path = Path.GetDirectoryName(fileToUnZip) + "\";
                    // Directory.CreateDirectory(path);//生成解压目录
                    if (fileName != String.Empty)
                    {
                        FileStream streamWriter = File.Create(path + fileName);//解压文件到指定的目录 
                        int size = 2048;
                        byte[] data = new byte[2048];
                        while (true)
                        {
                            size = inputStream.Read(data, 0, data.Length);
                            if (size > 0)
                            {
                                streamWriter.Write(data, 0, size);
                            }
                            else
                            {
                                break;
                            }
                        }
                        streamWriter.Close();
                    }
                }
                inputStream.Close();
            }
            #endregion
    
            private void Form1_Load(object sender, EventArgs e)
            {
    
            }
    
        }
        public class PDFPATH
        {
            public string path { get; set; }
        }
    
  • 相关阅读:
    解决AJAX中使用UpdatePanel后再用无法弹出对话框问题
    <转>JavaScript 参考教程资料之文档对象
    C#中图片的重绘
    csv数据源的创建(二)
    开始试用windows live writer
    与BW相关各模块常用tcode及Table
    SAP中的client(也叫集团)的意义
    SAP中的client(也叫集团)的意义(中文版)
    BW报表igs服务配置解决中文显示问题
    开源.net论坛有那些?
  • 原文地址:https://www.cnblogs.com/liuruitao/p/4313162.html
Copyright © 2020-2023  润新知