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 implement the C # System.IO.Compression namespace

2025-03-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "how to realize the C # System.IO.Compression namespace". In the daily operation, I believe that many people have doubts about how to implement the C # System.IO.Compression namespace. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about how to realize the C # System.IO.Compression namespace. Next, please follow the editor to study!

C # System.IO.Compression namespace, note: this namespace is new in .NET Framework version 2.0. The C # System.IO.Compression namespace contains classes that provide basic stream compression and decompression services. GZipStream provides methods and properties for compressing and decompressing streams. Let's take GZipStream as an example

Note: this class is new in .NET Framework version 2.0.

Provides methods and properties for compressing and decompressing streams.

Namespace: System.IO.Compression

Assemblies: System (in system.dll)

Grammar

Visual Basic (declaration)

Public Class GZipStream

Inherits Stream

Visual Basic (usage)

Dim instance As GZipStream

C#

Public class GZipStream: Stream

C++

Public ref class GZipStream: public Stream

J#

Public class GZipStream extends Stream

JScript

Public class GZipStream extends Stream

ClassclsZip

... {

PublicvoidCompressFile (stringsourceFile,stringdestinationFile)

... {

/ / makesurethesourcefileisthere

If (File.Exists (sourceFile) = = false)

ThrownewFileNotFoundException ()

/ / Createthestreamsandbytearraysneeded

Byte [] buffer=null

FileStreamsourceStream=null

FileStreamdestinationStream=null

GZipStreamcompressedStream=null

Try

... {

/ / Readthebytesfromthesourcefileintoabytearray

SourceStream=newFileStream (sourceFile,FileMode.Open,FileAccess.Read,FileShare.Read)

/ / Readthesourcestreamvaluesintothebuffer

Buffer=newbyte [sourceStream.Length]

IntcheckCounter=sourceStream.Read (buffer,0,buffer.Length)

If (checkCountermeasure isolation buffer.Length)

... {

ThrownewApplicationException ()

}

/ / OpentheFileStreamtowriteto

DestinationStream=newFileStream (destinationFile,FileMode.OpenOrCreate,FileAccess.Write)

/ / Createacompressionstreampointingtothedestiantionstream

CompressedStream=newGZipStream (destinationStream,CompressionMode.Compress,true)

/ / Nowwritethecompresseddatatothedestinationfile

CompressedStream.Write (buffer,0,buffer.Length)

}

Catch (ApplicationExceptionex)

... {

MessageBox.Show (ex.Message, "error compressing file:", MessageBoxButtons.OK,MessageBoxIcon.Error)

}

Finally

... {

/ / Makesureweallwayscloseallstreams

If (sourceStreamstreams null)

SourceStream.Close ()

If (invalid sedStream streams null)

CompressedStream.Close ()

If (destinationStream streams null)

DestinationStream.Close ()

}

}

PublicvoidDecompressFile (stringsourceFile,stringdestinationFile)

... {

/ / makesurethesourcefileisthere

If (File.Exists (sourceFile) = = false)

ThrownewFileNotFoundException ()

/ / Createthestreamsandbytearraysneeded

FileStreamsourceStream=null

FileStreamdestinationStream=null

GZipStreamdecompressedStream=null

Byte [] quartetBuffer=null

Try

... {

/ / Readinthecompressedsourcestream

SourceStream=newFileStream (sourceFile,FileMode.Open)

/ / Createacompressionstreampointingtothedestiantionstream

DecompressedStream=newGZipStream (sourceStream,CompressionMode.Decompress,true)

/ / Readthefootertodeterminethelengthofthedestiantionfile

QuartetBuffer=newbyte [4]

Intposition= (int) sourceStream.Length-4

SourceStream.Position=position

SourceStream.Read (quartetBuffer,0,4)

SourceStream.Position=0

IntcheckLength=BitConverter.ToInt32 (quartetBuffer,0)

Byte [] buffer= Newbyte [checkLength + 100]

Intoffset=0

Inttotal=0

/ / Readthecompresseddataintothebuffer

While (true)

... {

IntbytesRead=decompressedStream.Read (buffer,offset,100)

If (bytesRead==0)

Break

Offset+=bytesRead

Total+=bytesRead

}

/ / Nowwriteeverythingtothedestinationfile

DestinationStream=newFileStream (destinationFile,FileMode.Create)

DestinationStream.Write (buffer,0,total)

/ / andflusheveryhtingtocleanoutthebuffer

DestinationStream.Flush ()

}

Catch (ApplicationExceptionex)

... {

MessageBox.Show (ex.Message, "error occurred while decompressing the file:"

MessageBoxButtons.OK,MessageBoxIcon.Error)

}

Finally

... {

/ / Makesureweallwayscloseallstreams

If (sourceStreamstreams null)

SourceStream.Close ()

If (null for pressedStreamstreams)

DecompressedStream.Close ()

If (destinationStream streams null)

DestinationStream.Close ()

}

}

}

At this point, the study on "how to implement the C # System.IO.Compression namespace" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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