In addition to Weibo, there is also WeChat
Please pay attention

WeChat public account
Shulou
2025-11-04 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.

The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope





About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.