asp.net在线压缩和解压缩的实现
【IT168技术文档】
我们经常会遇到批量上传的问题,也会遇到将某个目录下所有文件都上传到服务器上的问题。那么,如何解决此类问题呢?以前的技术一般采用ActiveX等方式,这里我采用SharpZlib来实现,听说VS2005已有压缩和解压缩的解决方案,我还没有时间用VS2005,所以就只好使用VS2003 + SharpZlib来解决问题了。
// 1. AttachmentUnZip.cs
System;
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;
namespace WebZipUnzip
{
AttachmentUnZip
{
public AttachmentUnZip()
{
}
zipFile)
{
];
FileProperties[ UnZipClass();
UnZc.UnZip(FileProperties);
}
}
}
// ---------------------------------------------
// 2. UnZipClass.cs
System;
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;
namespace WebZipUnzip
{
UnZipClass
{
/// 解压文件
[] args)
{
ZipInputStream s ]));
try
{
ZipEntry theEntry;
)
{
]);
Path.GetFileName(theEntry.Name);
//生成解压目录 Directory.CreateDirectory(directoryName);
String.Empty)
{
//解压文件到指定的目录fileName);
;
];
)
{
size , data.Length);
)
{
streamWriter.Write(data, 0, size);
}
else
{
break;
}
}
streamWriter.Close();
}
}
s.Close();
}
catch(Exception eu)
{
throw eu;
}
finally
{
s.Close();
}
}//end UnZip dir)
{
try
{
Directory.Exists(dir))
Directory.CreateDirectory(dir);
Path.Combine(dir,file);
ZipInputStream s ZipInputStream(File.OpenRead( fileFullName ));
ZipEntry theEntry;
)
{
Path.GetDirectoryName(theEntry.Name);
Path.GetFileName(theEntry.Name);
String.Empty)
Directory.CreateDirectory( Path.Combine(dir, directoryName));
String.Empty)
{
FileStream streamWriter = File.Create( Path.Combine(dir,theEntry.Name) );
;
];
)
{
size , data.Length);
)
{
streamWriter.Write(data, 0, size);
}
else
{
break;
}
}
streamWriter.Close();
}
}
s.Close();
;
}
catch (Exception)
{
throw;
}
}
}
// ----------------------------------------------
// 3. ZipClass.cs
System;
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;
namespace WebZipUnzip
{
/// 压缩文件
ZipClass
{
password)
{
//如果文件没有找到,则报错 System.IO.File.Exists(FileToZip))
{
);
}
System.IO.FileStream StreamToZip System.IO.FileStream(FileToZip,System.IO.FileMode.Open , System.IO.FileAccess.Read);
System.IO.FileStream ZipFile = System.IO.File.Create(ZipedFile);
ZipOutputStream ZipStream ZipOutputStream(ZipFile);
ZipEntry ZipEntry );
ZipStream.PutNextEntry(ZipEntry);
ZipStream.SetLevel(CompressionLevel);
[BlockSize];
System.Int32 size ,buffer.Length);
ZipStream.Write(buffer,0,size);
try
{
StreamToZip.Length)
{
,buffer.Length);
ZipStream.Write(buffer,0,sizeRead);
size += sizeRead;
}
}
catch(System.Exception ex)
{
throw ex;
}
ZipStream.Finish();
ZipStream.Close();
StreamToZip.Close();
}
[] args)
{
//string[] filenames = Directory.GetFiles(args[0]);]};
Crc32 crc Crc32();
ZipOutputStream s ]));
s.SetLevel( filenames)
{
File.OpenRead(file);
[fs.Length];
fs.Read(buffer, 0, buffer.Length);
ZipEntry entry 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
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();
}
}
}
// ---------------------------------------------
// 4. WebForm1.aspx
// ---------------------------------------------
//-------------------------------------------
// 5.WebForm1.aspx.cs
System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace WebZipUnzip
{
/// Summary description for WebForm1.
WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.HtmlControls.HtmlInputFile File1;
protected System.Web.UI.WebControls.Button Button2;
sender, System.EventArgs e)
{
// Put user code to initialize the page here }
#region Web Form Designer generated code OnInit(EventArgs e)
{
//// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
InitializeComponent()
{
.Button1_Click);
.Button2_Click);
.Page_Load);
}
sender, System.EventArgs e)
{
];
C:\test
//待压缩文件fullName;
//压缩后的目标文件;
ZipClass Zc=new ZipClass();
Zc.ZipFileMain(FileProperties);
//删除压缩前的文件 System.IO.File.Delete(fullName);
}
sender, System.EventArgs e)
{
C:\test\a.zip
//解压文件
//AttachmentUnZip.UpZip(fullName);
// string[] FileProperties = new string[2];
待解压的文件
解压后放置的目标目录
// UnZipClass UnZc=new UnZipClass();
// UnZc.UnZip(FileProperties); System.IO.Path.GetDirectoryName(fullName);
System.IO.Path.GetFileName(fullName);
UnZipClass.UnZipFile(fileName, dir);
}
#endregion
}
}
0
0