Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to achieve Compression in .NET 4.5

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces .NET 4.5 how to achieve compression, the article introduces in great detail, has a certain reference value, interested friends must read it!

Newly added compressed namespaces and methods in .NET 4.5. You can abandon the ICSharpCode.SharpZipLib.dll class library. It is neck and neck in performance. But it can greatly simplify your code. If you start using .NET FrameWork4.5 for compression, try your own compression method.

Traditionally use code written by ICSharpCode.SharpZipLib.dll.

The code is as follows:

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 ("time consumed: {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 compression that comes with .NET FrameWork 4.5.

The code is as follows:

Static void Main (string [] args)

{

Stopwatch watch = new Stopwatch ()

Watch.Start ()

String path = @ "E:\"

Compress (path, @ "F:\ 4.5.zip")

Watch.Stop ()

Console.WriteLine ("time consumed: {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)

}

The above is all the content of the article "how to compress .NET 4.5". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report