ASP. NET implements online File compression and decompression
[Source: http://www.52jc.com.cn/netschool/biancheng/ASPNET/200704/netschool_20070407045831.html ]
//----------------------------------------------
// 3. zipclass. CS
//----------------------------------------------
Using 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
{
/// <Summary>
/// Compressed file
/// </Summary>
Public class zipclass
{
Public void zipfile (string filetozip, string zipedfile, int compressionlevel, int blocksize, string password)
{
// If the file is not found, an error is returned.
If (! System. Io. file. exists (filetozip ))
{
Throw new system. Io. filenotfoundexception ("the specified file" + filetozip + "cocould 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 = 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)
{
// Open the compressed file
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 shocould 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 ();
}
}
}
//---------------------------------------------
// 4. webform1.aspx
//---------------------------------------------
<% @ Page Language = "C #" codebehind = "webform1.aspx. cs" autoeventwireup = "false" inherits = "webzipunzip. webform1" %> & nbsp;
<Meta C name = generator>
<Meta C # name = code_language>
<Meta C name = vs_defaultclientscript>
<Meta C: // schemas.microsoft.com/intelliisense/ie5 name = vs_targetschema>
<Form ID = form1 method = post runat = "server"> <? XML: namespace prefix = ASP/> <asp: button id = button1 style = "Z-INDEX: 101; left: 56px; position: absolute; top: 64px "runat =" server "text =" compression "> </ASP: button> <asp: button id = button2 style =" Z-INDEX: 102; left: Adjust PX; position: absolute; top: 64px "runat =" server "text =" Unzip "> </ASP: button> <input id = file1 style =" Z-INDEX: 103; left: 32px; position: absolute; top: 24px "type = file name = file1 runat =" server "> </form> </body> //-------------------------------------------
// 5. webform1.aspx. CS
//-------------------------------------------
Using 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>
/// Summary description for webform1.
/// </Summary>
Public class 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;
Private void page_load (Object sender, system. eventargs E)
{
// Put user code to initialize the page here
}
# Region web form designer generated code
Override protected void oninit (eventargs E)
{
//
// Codegen: This call is required by the ASP. NET web form designer.
//
Initializecomponent ();
Base. oninit (E );
}
/// <Summary>
/// Required method for designer support-do not modify
/// The contents of this method with the code editor.
/// </Summary>
Private void initializecomponent ()
{
This. button1.click + = new system. eventhandler (this. button#click );
This. button2.click + = new system. eventhandler (this. button2_click );
This. Load + = new system. eventhandler (this. page_load );
}
# Endregion
# Region Compression
Private void button#click (Object sender, system. eventargs E)
{
String [] fileproperties = new string [2];
String fullname = This. file1.postedfile. filename; // C: \ test \ a.txt
String destpath = system. Io. Path. getdirectoryname (fullname); // C: \ test
// File to be compressed
Fileproperties [0] = fullname;
// Target file after compression
Fileproperties [1] = destpath + "\" + system. Io. Path. getfilenamewithoutextension (fullname) + ". Zip ";
Zipclass ZC = new zipclass ();
ZC. zipfilemain (fileproperties );
// Delete the file before compression
System. Io. file. Delete (fullname );
}
# Endregion
# Region Decompression
Private void button2_click (Object sender, system. eventargs E)
{
String fullname = This. file1.postedfile. filename; // C: \ test \ a.zip
// Decompress the file
// Attachmentunzip. upzip (fullname );
// String [] fileproperties = new string [2];
// Fileproperties [0] = fullname; // file to be decompressed
// Fileproperties [1] = system. Io. Path. getdirectoryname (fullname); // The target directory to be placed after decompression
// Unzipclass unzc = new unzipclass ();
// Unzc. Unzip (fileproperties );
String dir = system. Io. Path. getdirectoryname (fullname );
String filename = system. Io. Path. getfilename (fullname );
Unzipclass. unzipfile (filename, DIR );
}
# Endregion
}
}