• 压缩解压


    public class ZipHerlper
        {
            #region 压缩zip
            public static bool CreateZipDirectory(string dirPath, string zipFilePath)
            {
                bool success = false;
                FileInfo zipFile = new FileInfo(zipFilePath);
                if (zipFile.Exists)
                {
                    zipFile.Delete();
                }
    
                ZipOutputStream zipStream = new ZipOutputStream(File.Create(zipFilePath));
                try
                {
                    if (dirPath[dirPath.Length - 1] != System.IO.Path.DirectorySeparatorChar)
                        dirPath += System.IO.Path.DirectorySeparatorChar;
                    zipStream.SetLevel(9);// 压缩级别 0-9
                    byte[] buffer = new byte[4096]; //缓冲区大小
                    CreateZipFiles(dirPath, zipStream, dirPath, buffer);
                    success = true;
                }
                catch (Exception ex)
                {
                    success = false;
                }
                finally
                {
                    zipStream.Finish();
                    zipStream.Close();
                    zipFile = new FileInfo(zipFilePath);
                    if (!success && zipFile.Exists)
                    {
                        zipFile.Delete();
                    }
                }
                return success;
            }
    
            public static void CreateZipFiles(string sourceFilePath, ZipOutputStream zipStream, string staticFile, byte[] buffer)
            {
                string[] filesArray = Directory.GetFileSystemEntries(sourceFilePath);
                foreach (string file in filesArray)
                {
                    if (!file.EndsWith(".pdb") && !file.Contains(".vshost.exe"))
                    {
                        if (Directory.Exists(file))
                        {
                            CreateZipFiles(file, zipStream, staticFile, buffer);
                        }
                        else
                        {
                            ZipEntry entry = new ZipEntry(file.Substring(staticFile.LastIndexOf("\") + 1));
                            entry.DateTime = DateTime.Now;
                            zipStream.PutNextEntry(entry);
                            using (FileStream fs = File.OpenRead(file))
                            {
                                int sourceBytes;
                                do
                                {
                                    sourceBytes = fs.Read(buffer, 0, buffer.Length);
                                    zipStream.Write(buffer, 0, sourceBytes);
                                } while (sourceBytes > 0);
                            }
                        }
                    }
                }
            }
            #endregion
    
            /// <summary>
            /// 解压
            /// </summary>
            /// <param name="zip"></param>
            /// <param name="path"></param>
            public static void UnZip(string zip, string path)
            {
                ZipInputStream s = new ZipInputStream(File.OpenRead(zip));
                ZipEntry theEntry;
                while ((theEntry = s.GetNextEntry()) != null)
                {
    
                    string directoryName = Path.GetDirectoryName(path);
                    string fileName = Path.GetFileName(theEntry.Name);
                    //生成解压目录
    
                    if (!Directory.Exists(directoryName))
                    {
                        Directory.CreateDirectory(directoryName);
                    }
                    if (theEntry.Name.Contains("\") || theEntry.Name.Contains("/"))
                    {
                        string subPath = Path.GetDirectoryName(path + theEntry.Name);
                        if (!Directory.Exists(subPath))
                        {
                            Directory.CreateDirectory(subPath);
                        }
                    }
                    if (fileName != String.Empty)
                    {
                        //解压文件到指定的目录
                        FileStream streamWriter = File.Create(path + theEntry.Name);
    
                        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);
                            }
                            else
                            {
                                break;
                            }
                        }
    
                        streamWriter.Close();
                    }
                }
                s.Close();
            }
        }
    ZipHelper
            private void button1_Click(object sender, EventArgs e)
            {
                FolderBrowserDialog dialog = new FolderBrowserDialog();
                dialog.Description = "请选择文件路径";
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    string foldPath = dialog.SelectedPath;
                    ZipHerlper.CreateZipDirectory(foldPath, foldPath+".Zip");
                    MessageBox.Show("压缩完成");
                }
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                OpenFileDialog fileDialog = new OpenFileDialog();
                fileDialog.Multiselect = true;
                fileDialog.Title = "请选择文件";
                fileDialog.Filter = "所有文件(*.*)|*.*";
                if (fileDialog.ShowDialog() == DialogResult.OK)
                {
                    string file = fileDialog.FileName;
                    string ss = file.Substring(0, file.LastIndexOf('.'));
                    ZipHerlper.UnZip(file, file.Substring(0,file.LastIndexOf('.'))+"\");
                    MessageBox.Show("解压完成");
                }
            }
    调用

    ICSharpCode.SharpZipLib.dll需要引用

  • 相关阅读:
    MFC新建菜单项
    java连接mysql
    装visio 2007遇到了1706错误,解决办法
    Oracle协议适配器错误解决办法
    powershell 开启开发人员仪表盘
    sharepoint stsadm 创建网站脚本
    网站安全修复笔记1
    sharepoint ribbon添加菜单
    解决 由于代码已经过优化或者本机框架位于调用堆栈之上,无法计算表达式的值
    RDA实现SQL CE与SQL Server间数据存取
  • 原文地址:https://www.cnblogs.com/junhuang/p/6437899.html
Copyright © 2020-2023  润新知