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

What are the application skills of .NET Framework Compression functions?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

What are the functional application skills of .NET Framework Compression? for this question, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.

.net Framework can provide developers with a suitable WEB application deployment platform to help them easily complete the development and creation of various programs. In the past, when you were working on a project, you needed to provide file compression. At that time, I used an open source class library called ZipLib, which was very convenient to use. In .net 2.0, Microsoft added the System.IO.Compression namespace to System.IO, and the .NET Framework Compression function provides a class related to compression, GZipStream.

The use of this class is similar to that of a normal file stream. I didn't analyze its internal implementation, but I guessed that I decorated Stream in Decorator mode and applied the. NET Framework Compression function algorithm from it. It writes the contents of buffer to another file stream through the Write () method. For example, if the source file is sourceFile and the compressed file is targetFile, the method is as follows:

Byte [] buffer = null

FileStream sourceStream = null

FileStream targetStream = null

GZipStream compressedStream = null

SourceStream = new FileStream

(sourceFile,FileMode.Open,FileAccess.

Read,FileShare.Read)

Buffer = new byte [sourceStream.Length]

SourceStream.Read (buffer,0,buffer.Length)

TargetStream = new FileStream

(targetFile,FileMode.OpenOrCreate

FileAccess.Write)

/ / point CompressedStream to targetStream

CompressedStream = new GZipStream

(targetStream,CompressionMode.

Compress,true)

CompressStream.Write (buffer,0

Buffer.Length)

When using GZipStream, you need to add a reference:

Using System.IO; using System.IO.Compression

The extraction of the .NET Framework Compression feature is similar to the previous method, still using the GZipStream file stream:

/ / Read in the compressed source stream

SourceStream = new FileStream

(sourceFile, FileMode.Open)

/ / Create a compression stream pointing

To the destiantion stream

DecompressedStream = new GZipStream

(sourceStream, CompressionMode.

Decompress, true)

/ / Read the footer to determine the

Length of the destiantion file

QuartetBuffer = new byte [4]

Int position = (int) sourceStream.Length-4

SourceStream.Position = position

SourceStream.Read (quartetBuffer, 0,4)

SourceStream.Position = 0

Int checkLength = BitConverter.ToInt32

(quartetBuffer, 0)

Byte [] buffer = new byte [checkLength + 100]

Int offset = 0

Int total = 0

/ / Read the compressed data into the buffer

While (true)

{

Int bytesRead = decompressedStream.Read

(buffer, offset, 100)

If (bytesRead = = 0) break

Offset + = bytesRead; total + = bytesRead

}

/ / Now write everything to the destination file

DestinationStream = new FileStream

(destinationFile, FileMode.Create)

DestinationStream.Write (buffer, 0, total)

/ / and flush everyhting to clean out the buffer

DestinationStream.Flush ()

This is the answer to the question about the application skills of .NET Framework Compression functions. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.

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