Go [C #] Compression decompression class GZipStream

Source: Internet
Author: User

This article is from: Https://msdn.microsoft.com/zh-cn/library/system.io.compression.gzipstream (v=vs.100). aspx
usingSystem;usingSystem.IO;usingSystem.IO.Compression;namespacezip{ Public classProgram { Public Static voidMain () {//Path to directory of files to compress and decompress.            stringDirpath =@"C:\users\public\reports"; DirectoryInfo di=NewDirectoryInfo (Dirpath); //Compress The directory ' s files.            foreach(FileInfo fiinchdi.            GetFiles ()) {Compress (FI); }            //decompress all *.gz files in the directory.            foreach(FileInfo fiinchDi. GetFiles ("*.gz") {Decompress (FI); }        }         Public Static voidCompress (FileInfo fi) {//Get The stream of the source file.            using(FileStream inFile =fi. OpenRead ()) {//Prevent compressing hidden and//already compressed files.                if( file.getattributes (FI. FullName)&Fileattributes.hidden)! = Fileattributes.hidden & fi. Extension! =". GZ")                {                    //Create the compressed file.                    using(FileStream outFile =File.create (FI. FullName+". GZ"))                    {                        using(GZipStream Compress =NewGZipStream (OutFile, compressionmode.compress)) {                            //Copy The source file into//The compression stream.Infile.copyto (Compress); Console.WriteLine ("Compressed {0} from {1} to {2} bytes.", FI. Name, FI.                        Length.tostring (), outFile.Length.ToString ()); }                    }                }            }        }         Public Static voidDecompress (FileInfo fi) {//Get The stream of the source file.            using(FileStream inFile =fi. OpenRead ()) {//Get Original file extension, for example//"Doc" from report.doc.gz.                stringCurfile =fi.                FullName; stringOrigName = Curfile.remove (Curfile.length-fi.                Extension.length); //Create the decompressed file.                using(FileStream outFile =file.create (OrigName)) {                    using(GZipStream decompress =NewGZipStream (InFile, compressionmode.decompress)) {                        //Copy the decompression stream//Into the output file.Decompress.copyto (OutFile); Console.WriteLine ("decompressed: {0}", FI.                    Name); }                }            }        }    }}

Go [C #] Compression decompression class GZipStream

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.