国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

實(shí)現(xiàn)asp.net的文件壓縮、解壓、下載

2019-11-17 01:46:50
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

實(shí)現(xiàn)asp.net的文件壓縮、解壓、下載

很早前就想做文件的解壓、壓縮、下載了,不過(guò)一直沒(méi)時(shí)間,現(xiàn)在項(xiàng)目做完了,今天弄了下。不過(guò)解壓,壓縮的方法還是看的網(wǎng)上的,嘻嘻~~不過(guò)我把它們綜合了一下哦。呵呵~~

1.先要從網(wǎng)上下載一個(gè)icsharpcode.sharpziplib.dll

2.建立類AttachmentUnZip,內(nèi)容如下:

using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using ICSharpCode.SharpZipLib.Zip;using ICSharpCode.SharpZipLib.GZip;using ICSharpCode.SharpZipLib.BZip2;using ICSharpCode.SharpZipLib.Checksums;using ICSharpCode.SharpZipLib.Zip.ComPRession;using ICSharpCode.SharpZipLib.Zip.Compression.Streams;using System.IO;/// <summary>///AttachmentUnZip 的摘要說(shuō)明/// </summary>public class AttachmentUnZip{ public AttachmentUnZip() { // //TODO: 在此處添加構(gòu)造函數(shù)邏輯 // } public static void UpZip(string zipFile) { string[] FileProperties = new string[2]; FileProperties[0] = zipFile;//待解壓的文件 FileProperties[1] = zipFile.Substring(0, zipFile.LastIndexOf("http://") + 1);//解壓后放置的目標(biāo)目錄 UnZipClass UnZc = new UnZipClass(); UnZc.UnZip(FileProperties); }}3.建立類UnZipClass,內(nèi)容如下:

using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.IO;using ICSharpCode.SharpZipLib.Zip;using ICSharpCode.SharpZipLib.GZip;using ICSharpCode.SharpZipLib.BZip2;using ICSharpCode.SharpZipLib.Checksums;using ICSharpCode.SharpZipLib.Zip.Compression;using ICSharpCode.SharpZipLib.Zip.Compression.Streams;/// <summary>///UnZipClass 的摘要說(shuō)明/// </summary>public class UnZipClass{ public UnZipClass() { // //TODO: 在此處添加構(gòu)造函數(shù)邏輯 // } /// <summary> /// 解壓文件 /// </summary> /// <param name="args">包含要解壓的文件名和要解壓到的目錄名數(shù)組</param> public void UnZip(string[] args) { ZipInputStream s = new ZipInputStream(File.OpenRead(args[0])); try { ZipEntry theEntry; while ((theEntry = s.GetNextEntry()) != null) { string directoryName = Path.GetDirectoryName(args[1]); string fileName = Path.GetFileName(theEntry.Name); //生成解壓目錄 Directory.CreateDirectory(directoryName); if (fileName != String.Empty) { //解壓文件到指定的目錄 FileStream streamWriter = File.Create(args[1] + fileName); 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(); } catch (Exception eu) { throw eu; } finally { s.Close(); } }//end UnZip public static bool UnZipFile(string file, string dir) { try { if (!Directory.Exists(dir)) Directory.CreateDirectory(dir); string fileFullName = Path.Combine(dir, file); ZipInputStream s = new ZipInputStream(File.OpenRead(fileFullName)); ZipEntry theEntry; while ((theEntry = s.GetNextEntry()) != null) { string directoryName = Path.GetDirectoryName(theEntry.Name); string fileName = Path.GetFileName(theEntry.Name); if (directoryName != String.Empty) Directory.CreateDirectory(Path.Combine(dir, directoryName)); if (fileName != String.Empty) { FileStream streamWriter = File.Create(Path.Combine(dir, 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(); return true; } catch (Exception) { throw; } }}4.建立類ZipClass,內(nèi)容如下:

using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.IO;using ICSharpCode.SharpZipLib.Zip;using ICSharpCode.SharpZipLib.GZip;using ICSharpCode.SharpZipLib.BZip2;using ICSharpCode.SharpZipLib.Checksums;using ICSharpCode.SharpZipLib.Zip.Compression;using ICSharpCode.SharpZipLib.Zip.Compression.Streams;/// <summary>///ZipClass 的摘要說(shuō)明/// </summary>public class ZipClass{ public ZipClass() { // //TODO: 在此處添加構(gòu)造函數(shù)邏輯 // } public void ZipFile(string FileToZip, string ZipedFile, int CompressionLevel, int BlockSize, string passWord) { //如果文件沒(méi)有找到,則報(bào)錯(cuò) if (!System.IO.File.Exists(FileToZip)) { throw new System.IO.FileNotFoundException("The specified file " + FileToZip + " could not be found. Zipping aborderd"); } System.IO.FileStream StreamToZip = new System.IO.FileStream(FileToZip, System.IO.FileMode.Open, System.IO.Fileaccess.Read); System.IO.FileStream ZipFile = System.IO.File.Create(ZipedFile); ZipOutputStream ZipStream = new ZipOutputStream(ZipFile); ZipEntry ZipEntry = new ZipEntry("ZippedFile"); ZipStream.PutNextEntry(ZipEntry); ZipStream.SetLevel(CompressionLevel); byte[] buffer = new byte[BlockSize]; System.Int32 size = StreamToZip.Read(buffer, 0, buffer.Length); ZipStream.Write(buffer, 0, size); try { while (size < StreamToZip.Length) { int sizeRead = StreamToZip.Read(buffer, 0, buffer.Length); ZipStream.Write(buffer, 0, sizeRead); size += sizeRead; } } catch (System.Exception ex) { throw ex; } ZipStream.Finish(); ZipStream.Close(); StreamToZip.Close(); } public void ZipFileMain(string[] args) { //string[] filenames = Directory.GetFiles(args[0]); string[] filenames = new string[] { args[0] }; Crc32 crc = new Crc32(); ZipOutputStream s = new ZipOutputStream(File.Create(args[1])); s.SetLevel(6); // 0 - store only to 9 - means best compression foreach (string file in filenames) { //打開(kāi)壓縮文件 FileStream fs = File.OpenRead(file); byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); ZipEntry entry = new ZipEntry(file); entry.DateTime = DateTime.Now; // set Size and the crc, because the information // about the size and crc should be stored in the header // if it is not set it is automatically written in the footer. // (in this case size == crc == -1 in the header) // Some ZIP programs have problems with zip files that don't store // the size and crc in the header. entry.Size = fs.Length; fs.Close(); crc.Reset(); crc.Update(buffer); entry.Crc = crc.Value; s.PutNextEntry(entry); s.Write(buffer, 0, buffer.Length); } s.Finish(); s.Close(); }}5.類建好了,接下來(lái)建立測(cè)試頁(yè)了。呵呵~~EcodeZipRar.aspx,哦,對(duì)了,忘了說(shuō)了,我還在網(wǎng)站外層目錄下建了一個(gè)文件夾FileZip,用來(lái)存放文件的。

前臺(tái)頁(yè)面顯示:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="EcodeZipRar.aspx.cs" Inherits="EcodeZipRar" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title>無(wú)標(biāo)題頁(yè)</title></head><body> <form id="form1" runat="server"> <div> 添加要壓縮的文件:<asp:fileupload ID="Fileupload1" runat="server"></asp:fileupload> <br /> <asp:Button ID="Button1" runat="server" Text="上傳文件" onclick="Button1_Click" />//對(duì)于Fileupload1操作的 <asp:Button ID="Button2" runat="server" onclick="Button2_Click1" Text="開(kāi)始解壓" />//對(duì)于Fileupload1操作的 <asp:Button ID="Button3" runat="server" Text="文件下載" onclick="Button3_Click" /><br />//對(duì)于TreeView1操作的 <asp:TreeView ID="TreeView1" runat="server" ShowCheckBoxes="All">//顯示文件夾下的所有文件 </asp:TreeView> </div> </form></body></html>后臺(tái)代碼操作:

using System;using System.Collections;using System.Configuration;using System.Data;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Text;using System.IO;using ICSharpCode.SharpZipLib.Checksums;using ICSharpCode.SharpZipLib.Zip;using ICSharpCode.SharpZipLib.GZip;using System.Runtime.InteropServices;using Microsoft.Win32;using System.Diagnostics;public partial class EcodeZipRar : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { List(); } //實(shí)現(xiàn)文件壓縮 protected void Button1_Click(object sender, EventArgs e) { string[] FileProperties = new string[2]; string fullName = Fileupload1.Poste

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 连江县| 庄河市| 繁峙县| 西昌市| 方正县| 蓝田县| 陇南市| 兴城市| 日照市| 临安市| 乡城县| 修文县| 松阳县| 屏东市| 秦皇岛市| 阳山县| 黑龙江省| 龙泉市| 辽中县| 内黄县| 肇庆市| 和平县| 庆阳市| 尼勒克县| 宜章县| 兴城市| 绥宁县| 甘孜| 庄河市| 安远县| 内乡县| 蕉岭县| 黄冈市| 定远县| 正定县| 马关县| 满洲里市| 芷江| 汉寿县| 平顶山市| 荃湾区|