Asp.net compresses and decompress files

Source: Internet
Author: User
Tags crc32

I'm glad to have successfully applied for a blog in the blog Park. I 'd like to express my gratitude for writing an article.
Today, I made a project for someone else. There are not many functions, but some functions are complicated. For example, use a web page to compress and decompress files. After multiple searches, sharpziplib is used to implement the compression function.
The Code is as follows:
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;
/// <Summary>
/// Zip to compress all objects in a specified folder
/// </Summary>
Public class zip
{
# Region Constructor
/// <Summary>
/// Constructor
/// </Summary>
Public zip ()
{
//
// Todo: Add constructor logic here
//
}
# Endregion

# Region compression main function for compression
/// <Summary>
/// Compress the main function to implement the compression function
/// </Summary>
/// <Param name = "strfiledir"> </param>
Public void putinzipfile (string [] strfiledir)
{
String filenames = strfiledir [0];
CRC32 CRC = new CRC32 ();
Zipoutputstream S = new zipoutputstream (file. Create (ARGs [1]);
S. setlevel (6 );
Foreach (string file in directory. getfiles (filenames ))
{
Filestream FS = file. openread (File );
Byte [] buffer = new byte [fs. Length];
FS. Read (buffer, 0, buffer. Length );
String [] strname = file. Split ('\\');
Zipentry entry = new zipentry (strname [strname. Length-1]);
Entry. datetime = datetime. now;
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 ();
}
# Endregion
}
You can compress all the files in the specified folder into A. ZIP file.
If (! Ispostback)
{
String [] fileproperties = new string [2];
String fullname = server. mappath ("Upload ");
String destpath = system. Io. Path. getdirectoryname (fullname );
// File to be compressed
Fileproperties [0] = fullname;
// Target file after compression
Fileproperties [1] = destpath + String. Format ("\\{ 02.16.zip", "XXX ");
If (file. exists (fileproperties [1])
{
File. Delete (fileproperties [1]);
}
Zip ZC = new zipclass ();
ZC. putinzipfile (fileproperties );
This. downfile. href = string. Format ("/upload/00000).zip", "XXX ");
This. downfile. Target = "_ blank ";
}

 

 

Source: http://www.cnblogs.com/zhjp11/archive/2007/05/06/736778.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.