• ASP.NET生成压缩文件(rar打包)


    首先引用ICSharpCode.SharpZipLib.dll,没有在这里下载:http://files.cnblogs.com/files/cang12138/ICSharpCode.SharpZipLib.rar

    压缩打包代码:

        /// <summary>
        /// 生成压缩文件
        /// </summary>
        /// <param name="strZipPath">生成的zip文件的路径</param>
        /// <param name="strZipTopDirectoryPath">源文件的上级目录</param>
        /// <param name="intZipLevel">T压缩等级</param>
        /// <param name="strPassword">压缩包解压密码</param>
        /// <param name="filesOrDirectoriesPaths">源文件路径</param>
        /// <returns></returns>
        private bool Zip(string strZipPath, string strZipTopDirectoryPath, int intZipLevel, string strPassword, string[] filesOrDirectoriesPaths)
        {
            try
            {
                List<string> AllFilesPath = new List<string>();
                if (filesOrDirectoriesPaths.Length > 0) // get all files path
                {
                    for (int i = 0; i < filesOrDirectoriesPaths.Length; i++)
                    {
                        if (File.Exists(filesOrDirectoriesPaths[i]))
                        {
                            AllFilesPath.Add(filesOrDirectoriesPaths[i]);
                        }
                        else if (Directory.Exists(filesOrDirectoriesPaths[i]))
                        {
                            GetDirectoryFiles(filesOrDirectoriesPaths[i], AllFilesPath);
                        }
                    }
                }
    
                if (AllFilesPath.Count > 0)
                {
    
                    ZipOutputStream zipOutputStream = new ZipOutputStream(File.Create(strZipPath));
                    zipOutputStream.SetLevel(intZipLevel);
                    zipOutputStream.Password = strPassword;
    
                    for (int i = 0; i < AllFilesPath.Count; i++)
                    {
                        string strFile = AllFilesPath[i].ToString();
                        try
                        {
                            if (strFile.Substring(strFile.Length - 1) == "") //folder
                            {
                                string strFileName = strFile.Replace(strZipTopDirectoryPath, "");
                                if (strFileName.StartsWith(""))
                                {
                                    strFileName = strFileName.Substring(1);
                                }
                                ZipEntry entry = new ZipEntry(strFileName);
                                entry.DateTime = DateTime.Now;
                                zipOutputStream.PutNextEntry(entry);
                            }
                            else //file
                            {
                                FileStream fs = File.OpenRead(strFile);
    
                                byte[] buffer = new byte[fs.Length];
                                fs.Read(buffer, 0, buffer.Length);
    
                                string strFileName = strFile.Replace(strZipTopDirectoryPath, "");
                                if (strFileName.StartsWith(""))
                                {
                                    strFileName = strFileName.Substring(0);
                                }
                                ZipEntry entry = new ZipEntry(strFileName);
                                entry.DateTime = DateTime.Now;
                                zipOutputStream.PutNextEntry(entry);
                                zipOutputStream.Write(buffer, 0, buffer.Length);
    
                                fs.Close();
                                fs.Dispose();
                            }
                        }
                        catch
                        {
                            continue;
                        }
                    }
    
                    zipOutputStream.Finish();
                    zipOutputStream.Close();
    
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                return false;
            }
        }
    
        /// <summary>
        /// Gets the directory files.
        /// </summary>
        /// <param name="strParentDirectoryPath">源文件路径</param>
        /// <param name="AllFilesPath">所有文件路径</param>
        private void GetDirectoryFiles(string strParentDirectoryPath, List<string> AllFilesPath)
        {
            string[] files = Directory.GetFiles(strParentDirectoryPath);
            for (int i = 0; i < files.Length; i++)
            {
                AllFilesPath.Add(files[i]);
            }
            string[] directorys = Directory.GetDirectories(strParentDirectoryPath);
            for (int i = 0; i < directorys.Length; i++)
            {
                GetDirectoryFiles(directorys[i], AllFilesPath);
            }
            if (files.Length == 0 && directorys.Length == 0) //empty folder
            {
                AllFilesPath.Add(strParentDirectoryPath);
            }
        }
    
    
    
            //调用
    
            string strZipPath = @"D:ConsultSystemmgruser.zip";
            string strZipTopDirectoryPath = @"D:ConsultSystemmgr";
            int intZipLevel = 6;
            string strPassword = "";
            string[] filesOrDirectoriesPaths = new string[] { @"D:ConsultSystemmgruser.txt" };
            Zip(strZipPath, strZipTopDirectoryPath, intZipLevel, strPassword, filesOrDirectoriesPaths);

    出自:http://www.cnblogs.com/qiuweiguo/archive/2011/08/09/2132061.html

  • 相关阅读:
    洛谷P2414 阿狸的打字机
    洛谷P4362 贪吃的九头龙
    洛谷P4198 楼房重建
    JSP九大对象
    关于ErrorPage
    通过自己制作网页解决几个简单问题的方法
    九项重要的职业规划提示(转自W3School )
    jquery 使用方法(转载)
    <script language = "javascript">, <script type = "text/javascript">和<script language = "application/javascript">(转)
    微软当年的浏览器之战
  • 原文地址:https://www.cnblogs.com/cang12138/p/5520692.html
Copyright © 2020-2023  润新知