asp.net实现文件夹及文件压缩,并实现下载

asp.net实现文件夹及文件压缩,并实现下载,第1张

步骤

)先引用 ICSharpCode SharpZipLib dll

) cs文件引入

using System IOusing System Text

using ICSharpCode SharpZipLib Checksumsusing ICSharpCode SharpZipLib Zipusing ICSharpCode SharpZipLib GZip

代码

#region 下列代码为压缩并下载代码        ZipOutputStream zos = null       String strBaseDir = ""

       void dlZipDir(string strPath string strFileName)        {            MemoryStream ms = null           Response ContentType = "application/octet stream"           strFileName = HttpUtility UrlEncode(strFileName) Replace( + )           Response AddHeader("Content Disposition" "attachment   filename=" + strFileName + " zip")           ms = new MemoryStream()           zos = new ZipOutputStream(ms)           strBaseDir = strPath + ""           addZipEntry(strBaseDir)           zos Finish()           zos Close()           Response Clear()           Response BinaryWrite(ms ToArray())           Response End()       }

void addZipEntry(string PathStr)        {            DirectoryInfo di = new DirectoryInfo(PathStr)           foreach (DirectoryInfo item in di GetDirectories())            {                addZipEntry(item FullName)           }            foreach (FileInfo item in di GetFiles())            {                FileStream fs = File OpenRead(item FullName)               byte[] buffer = new byte[fs Length]               fs Read(buffer buffer Length)               string strEntryName = item FullName Replace(strBaseDir "")               ZipEntry entry = new ZipEntry(strEntryName)               zos PutNextEntry(entry)               zos Write(buffer buffer Length)               fs Close()           }      昌慎  }  

PRotected void Button _Click(object sender EventArgs e)        敏逗 {       耐拿敬      string userPath ="D:华海实训(qqview )各级账号和密码"                        dlZipDir(userPath "华海实训(qqview )服务账号")

}

#endregion

lishixinzhi/Article/program/net/201311/14232

如果机器安装有winRar软租亏件,就可以通猜型拦过shell来借用他的功能达到压缩文件的效果;

参考代码如下:

Dim DeliveryF As String = Server.MapPath("..\Temp\DeliveryFactors.xls") '原始文件 (压缩前)

Dim TruckInfo As String = Server.MapPath("..\Temp\TruckInformation.xls")

Dim QDetail As String = Server.MapPath("..\Temp\QuotationDetail.xls")

'用shell命令调用winrar.exe创建压缩文件()

Dim winRarexe As String = "C:\Program Files\WinRAR\Rar" 'winzip 执行文件的位置

Dim wtarget As String = "C:\temp\QuotationVAComparsion.zip" '目地文件 (压缩后)

Dim command As String = winRarexe &" a " &wtarget &" " &DeliveryF &" " &TruckInfo &" " &QDetail

'这个命令你可以查看winrar的命令集

Dim retval As Double 'Shell 指穗胡令传回值

retval = Shell(command, AppWinStyle.MinimizedFocus)

.NET自带类支持GZIP算法压缩/解压流。

System.IO.Compression.GZipStream

这个类不能直接用来解压.zip文件,看看老外怎么说的

It is possible, with extra code, to produce or read a zip file, with the help of GZipStream, but it is not trivial.

If you want to read or write Zip files, the best bet is to use a third party library, like DotNetZip. This library is free, and enables your applications to read or write zip files that contain multiple compressed files. These are standard zip files that can be opened by Windows Explorer, or WinZip, or other zip tools. It's simple to use. It works with VB, or c#, or any .NET language

总之,如果你要伏中裂解压缺闭ZIP文件,还是用第三方库吧培乱。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/12245144.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-22
下一篇 2023-05-22

发表评论

登录后才能评论

评论列表(0条)

保存