很早前就想做文件的解壓、壓縮、下載了,不過(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
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注