C # compress and decompress all files in the web space

Source: Internet
Author: User
Tags crc32

Using system;
Using icsharpcode. sharpziplib;
Using icsharpcode. sharpziplib. checksums;
Using system. IO;
Using icsharpcode. sharpziplib. Zip;
Using system. collections;

/// <Summary>
/// Summary of common.
/// </Summary>
Public class commrar
{
Public commrar ()
{
//
// Todo: add the constructor logic here
//
}

//


// compressed file http://www.my400800.cn
//
// compressed file name set
// compressed file name
// / password
Public static void zipfile (string path, string destfilename)
{< br> CRC32 CRC = new CRC32 ();
string strparpath = path;

// Define
System. Io. directoryinfo mydir = new directoryinfo (PATH );
// 0-store only to 9-means best compression
If (mydir. exists = true)
{
System. Io. fileinfo [] myfileary = mydir. getfiles ();

Zipoutputstream S = new zipoutputstream (file. Create (destfilename ));
S. Password = "";
S. setlevel (6 );

// Extracts information from each file in the folder cyclically,
Foreach (fileinfo objfiles in myfileary)
{
If (objfiles. fullname. indexof (". rar")> 0) continue;
Filestream FS = file. openread (objfiles. fullname );
Byte [] buffer = new byte [fs. Length];
FS. Read (buffer, 0, buffer. Length );
Zipentry entry = new zipentry (objfiles. fullname. substring (path. Length ));
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 );
}
Foreach (directoryinfo objdire in mydir. getdirectories ())
{
Zipfolder (S, objdire. fullname, CRC, strparpath );

}

S. Finish ();
S. Close ();
}
}

Private Static void zipfolder (zipoutputstream S, string path, CRC32 CRC, string strparpath)
{

// Define
System. Io. directoryinfo mydir = new directoryinfo (PATH );
System. Io. fileinfo [] myfileary = mydir. getfiles ();
// Extracts information from each file in the folder cyclically,
Foreach (fileinfo objfiles in myfileary)
{
If (objfiles. fullname. indexof (". rar")> 0) continue;
Filestream FS = file. openread (objfiles. fullname );
Byte [] buffer = new byte [fs. Length];
FS. Read (buffer, 0, buffer. Length );
Zipentry entry = new zipentry (objfiles. fullname. substring (strparpath. Length ));
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 );
}

Foreach (directoryinfo objdire in mydir. getdirectories ())
{
Zipfolder (S, objdire. fullname, CRC, strparpath );

}
}

/// <Summary>
/// Decompress the file
/// </Summary>
/// <Param name = "sourcefilename"> name of the extracted file </param>
/// <Param name = "destpath"> decompressed file directory </param>
/// <Param name = "password"> password </param>
Public static void unzipfile (string sourcefilename, string destpath, string filetype)
{
Zipinputstream S = new zipinputstream (file. openread (sourcefilename ));
Zipentry theentry;
Arraylist Al = new arraylist ();
String strrootpath = "";
While (theentry = S. getnextentry ())! = NULL)
{
String filename = (theentry. Name );
If (filename! = "")
{
Filename = destpath + "\" + filename;
Strrootpath = path. getdirectoryname (filename );
If (! Directory. exists (strrootpath ))
{
Directory. createdirectory (strrootpath );
}
Filestream streamwriter = file. Create (filename );
Int size = 2048;
Byte [] DATA = new byte [2048];
S. Password = "";
While (true)
{
Size = S. Read (data, 0, Data. Length );
If (size> 0)
{
Streamwriter. Write (data, 0, size );
}
Else
{
Break;
}
}
Streamwriter. Close ();
}

}
S. Close ();

}

}

Code download

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.