C # Use SharpZipLib to generate gzip/decompress files

Source: Internet
Author: User

C # Use SharpZipLib to generate gzip/decompress files
Code tells all:

using System;using System.IO;using ICSharpCode.SharpZipLib.GZip;using ICSharpCode.SharpZipLib.Core;namespace CNKIDataExport{    class Program    {        public static void gZipFile(string filePath, string zipFilePath)        {            Stream s = new GZipOutputStream(File.Create(zipFilePath));            FileStream fs = File.OpenRead(filePath);            int size;            byte[] buf = new byte[4096];            do            {                size = fs.Read(buf, 0, buf.Length);                s.Write(buf, 0, size);            } while (size > 0);            s.Close();            fs.Close();        }        public static void gunZipFile(string zipFilePath, string filePath)        {            using (Stream inStream = new GZipInputStream(File.OpenRead(zipFilePath)))            using (FileStream outStream = File.Create(filePath))            {                byte[] buf = new byte[4096];                StreamUtils.Copy(inStream, outStream, buf);            }        }        static void Main(string[] args)        {            string src = @"D:\test\in.txt"            string dest = @"D:\test\out.gz"            string ori = @"D:\test\ori.txt"            gZipFile(src, dest);            Console.WriteLine("gzip over!");            gunZipFile(dest, ori);            Console.WriteLine("gunzip over!");            Console.ReadKey();        }    }}

 


Related Article

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.