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

ASP.NET利用RAR实现文件压缩解压缩 介绍在ASP.NET中怎么利用WinRAR实现文件压缩/解压缩 立即下载
 
/// <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:\ConsultSystem\mgr\user.zip";
        string strZipTopDirectoryPath = @"D:\ConsultSystem\mgr\";
        int intZipLevel = 6;
        string strPassword = "";
        string[] filesOrDirectoriesPaths = new string[] { @"D:\ConsultSystem\mgr\user.txt" };
        Zip(strZipPath, strZipTopDirectoryPath, intZipLevel, strPassword, filesOrDirectoriesPaths);
使用C#在ASP.NET中上传文件并创建Zip文件 使用C#在ASP.NET中上载文件并创建一个Zip文件。 立即下载

相关推荐

十天学会ASP.NET教程十天学会ASP.NET教程.rar

十天学会ASP.NET教程.rar十天学会ASP.NET教程.rar

asp.net利用RAR实现文件压缩解压缩

如果服务器上安装了RAR程序,那么asp.net可以调用RAR实现文件压缩与解压缩。不过要注意的是,由于Web程序不能直接调用客户端的程序(除非用ActiveX,ActiveX几乎被废弃),所以如果要想实现让用户把本地文件用网页解压缩只有把文件上传到服务器上再调用服务器上的RAR压缩,同理要解压缩本地的RAR文件可以把文件上传到服务器解压再拿回来。本文讲怎么在服务器端的目录解压缩文件

高山流水 742

十天学会asp.net教程 rar压缩文件

十天学会asp.net教程,压缩文件,让你轻松上手 .net

ASP.NET生成压缩文件rar打包)ICSharpCode.SharpZipLib.dll

<br />因为项目需要打包文件,就在同事的建议下用ICSharpCode写了个打包函数.ICSharpCode的介绍就不说了.具体请到官方网站 http://www.icsharpcode.net/ 上了解. <br />  首先引用ICSharpCode.SharpZipLib.dll,没有在这里下载:http://files.cnblogs.com/KenBlove/ICSharpCode.SharpZipLib.rar<br />  代码实现多文件,自定义文件,整目录打包等功能.好了..奉上代码:<

UnOpenMyCode的专栏 3829

asp.net 实现文件的压缩和解压

如果该博客能给您带来帮助,请给博主一个评论谢谢!!话不多说下面请看具体的实现步骤。 1.首先在web项目中引用ICSharpCode.SharpZipLib.dll文件,可在博主的资源中下载。 2.具体的压缩和解压方法实现如下(代码中有详细的备注) /// <summary> /// 生成压缩文件 /// </summary> /// <param

qq_31975127的博客 1237

ASP.NET中实现压缩多个文件为.zip文件,实现批量下载功能

使用ICSharpCode.SharpZipLib.dll;  下载地址   http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx 下面是对#ZipLib进行.net下的解压缩的方法的介绍。     1.BZip2           加入ICSharpCode.SharpZipLib.dll的引用,在#Devel

黑色幽默 2361

ASP.NET生成压缩文件rar打包)1【转】

// /// 生成压缩文件 /// /// 生成的zip文件的路径 /// 源文件的上级目录 /// T压缩等级 /// 压缩包解压密码 /// 源文件路径 /// private bool Zip(string strZipPath, string strZipTopDirectoryPath, int intZipLevel, string strPassword, string[] f

u010158775的专栏 964

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

using System.IO; using System.IO.Compression; public static void fileCompress(string f_name) { string filename =HttpContext.Current.Server.MapPath(f_name); FileStream infile = F

u010158775的专栏 910

Asp.net 2.0 C#实现压缩/解压功能 (示例代码下载)

 (). 实现功能    对文件及目录的压缩及解压功能(). 运行图片示例  ().代码   1. 压缩类  1/**////   2/// 压缩类  3///   4public class ZipClass  5{     6    public static void ZipFile(string FileToZip, string Zi

★(ChengKing)【夜战鹰】【产品级性能调优与故障诊断分析】★ 7194

菜鸟详听ASP中也能解压缩rar文件

菜鸟详听ASP中也能解压缩rar文件      文/陈程        有没有想过在线解压缩压缩文件呢?呵呵,有时上传一个压缩包以后,利用Asp程序解压缩的确很方便,尤其是经常更新网站的站长们,由于文本文件

谁谁的程序技术专栏 1178

Asp.net 2.0 C#实现压缩/解压功能

Asp.net 2.0 C#实现压缩/解压功能 (示例代码下载)   (). 实现功能     对文件及目录的压缩及解压功能 (). 运行图片示例   ().代码    1. 压缩类   1///    2/// 压缩类   3///    4public class ZipClass   5{      6    public s

潘晓宇(panxiaoyu)的专栏 628

使用C#压缩解压rar和zip格式文件

为了便于文件在网络中的传输和保存,通常将文件进行压缩操作,常用的压缩格式有rar、zip和7z,本文将介绍在C#中如何对这几种类型的文件进行压缩和解压,并提供一些在C#中解压缩文件的开源库。 在C#.NET中压缩解压rar文件 rar格式是一种具有专利文件的压缩格式,是一种商业压缩格式,不开源,对解码算法是公开的,但压缩算法是私有的,需要付费,如果需要在您的商业软件中使用rar格式进行

moonpure的专栏 1万+
上一篇: 請求業務
永远专注NET
博客等级 码龄24年 12粉丝 24原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值