. NET 4.5 Compression

Source: Internet
Author: User

The compressed namespace and method added in. NET 4.5. You can discard the class library ICSharpCode. SharpZipLib. dll. Performance comparable. But it can greatly simplify your code. If you start to use. NET FrameWork4.5 for compression, try the built-in compression method.

Traditionally, the code written by ICSharpCode. SharpZipLib. dll is used.


Static void Main (string [] args)
{
Stopwatch watch = new Stopwatch ();
Watch. Start ();
String path = @ "E :\";
Compress (Directory. GetFiles (path), @ "F: \ 4.0.zip ");
Watch. Stop ();
Console. WriteLine ("consumed time: {0}", watch. ElapsedMilliseconds );
FileInfo f = new FileInfo (@ "F: \ 4.0.zip ");
Console. WriteLine ("file size {0}", f. Length );
}

Static void Compress (string [] filePaths, string zipFilePath)
{
Byte [] _ buffer = new byte [4096];
If (! Directory. Exists (zipFilePath ))
Directory. CreateDirectory (Path. GetDirectoryName (zipFilePath ));
Using (ZipOutputStream zip = new ZipOutputStream (File. Create (zipFilePath )))
{
Foreach (var item in filePaths)
{
If (! File. Exists (item ))
{
Console. WriteLine ("the file {0} not exist! ", Item );
}
Else
{
ZipEntry entry = new ZipEntry (Path. GetFileName (item ));
Entry. DateTime = DateTime. Now;
Zip. PutNextEntry (entry );
Using (FileStream fs = File. OpenRead (item ))
{
Int sourceBytes;
Do
{
SourceBytes = fs. Read (_ buffer, 0, _ buffer. Length );
Zip. Write (_ buffer, 0, sourceBytes );
} While (sourceBytes> 0 );
}
}
}
Zip. Finish ();
Zip. Close ();
}
}
Use the. NET FrameWork 4.5 self-contained compression.


Static void Main (string [] args)
{
Stopwatch watch = new Stopwatch ();
Watch. Start ();
String path = @ "E :\";
Compress (path, @ "F: \ 4.5.zip ");
Watch. Stop ();
Console. WriteLine ("consumed time: {0}", watch. ElapsedMilliseconds );
FileInfo f = new FileInfo (@ "F: \ 4.5.zip ");
Console. WriteLine ("file size {0}", f. Length );
}
Static void Compress (string filePath, string zipFilePath)
{
ZipFile. CreateFromDirectory (filePath, zipFilePath, CompressionLevel. Fastest, false );
}
How is the code much simpler?

 

 

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.