其他分享
首页 > 其他分享> > csharp: Zip and Unzip files using GZipStream

csharp: Zip and Unzip files using GZipStream

作者:互联网

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO.Compression;
using System.IO;
using System.Net;


namespace ThreeJsDemo
{

    /// <summary>
    /// 
    /// </summary>
    public partial class GZipStreamdemo : System.Web.UI.Page
    {


        /// <summary>
        /// geovindu,Geovin Du
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {

        }



        /// <summary>
        /// Code for zip
        /// 压缩
        /// </summary>
        /// <param name="fileToCompress"></param>
        /// <param name="newfile"></param>
        public static void zipFile(FileInfo fileToCompress,string newfile)
        {

            using (FileStream originalFileStream = fileToCompress.OpenRead())
            {

                if ((File.GetAttributes(fileToCompress.FullName) & FileAttributes.Hidden) != FileAttributes.Hidden & fileToCompress.Extension != ".gz")
                {

                    using (FileStream compressedFileStream = File.Create(newfile)) //fileToCompress.FullName + ".gz"
                    {

                        using (GZipStream compressionStream = new GZipStream(compressedFileStream, CompressionMode.Compress))
                        {

                            originalFileStream.CopyTo(compressionStream);

                        }
                    }
                }
            }

        }



        /// <summary>
        /// Code for unzip
        /// 解压
        /// </summary>
        /// <param name="fileToDecompress"></param>
        public static void unzipFile(FileInfo fileToDecompress)
        {
            using (FileStream originalFileStream = fileToDecompress.OpenRead())
            {

                string currentFileName = fileToDecompress.FullName;
                string newFileName = currentFileName.Remove(currentFileName.Length - fileToDecompress.Extension.Length);
                using (FileStream decompressedFileStream = File.Create(newFileName))
                {

                    using (GZipStream decompressionStream = new GZipStream(originalFileStream, CompressionMode.Decompress))
                    {

                        decompressionStream.CopyTo(decompressedFileStream);
                    }
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="newfileName"></param>
        /// <param name="value"></param>
        public static void CompressStringToFile(string newfileName, string value)
        {
            // A.
            // Write string to temporary file.
            string temp = Path.GetTempFileName();
            File.WriteAllText(temp, value);

            // B.
            // Read file into byte array buffer.
            byte[] b;
            using (FileStream f = new FileStream(temp, FileMode.Open))
            {
                b = new byte[f.Length];
                f.Read(b, 0, (int)f.Length);
            }

            // C.
            // Use GZipStream to write compressed bytes to target file.
            using (FileStream f2 = new FileStream(newfileName, FileMode.Create))
            using (GZipStream gz = new GZipStream(f2, CompressionMode.Compress, false))
            {
                gz.Write(b, 0, b.Length);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnZip_Click(object sender, EventArgs e)
        {
            string filePath = Server.MapPath("/geovindu/");//"";
            string newfile = Server.MapPath("/geovindu/geovindu.gz");
            DirectoryInfo directorySelected;
            try
            {

                directorySelected = new DirectoryInfo(filePath);
                foreach (FileInfo fileToCompress in directorySelected.GetFiles())
                {
                    zipFile(fileToCompress,newfile);
                }
               // MessageBox.Show("Zip Created Successfully...");
               Response.Write("Zip Created Successfully...");

            }
            catch (Exception ex)
            {
                Response.Write(ex.Message.ToString());
               // MessageBox.Show(Ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnUnzip_Click(object sender, EventArgs e)
        {

            string filePath = Server.MapPath("/geovindu/");// "";
            DirectoryInfo directorySelected;
            try
            {
                directorySelected = new DirectoryInfo(filePath);
                foreach (FileInfo fileToDecompress in directorySelected.GetFiles("*.gz"))
                {

                    unzipFile(fileToDecompress);
                }
                // MessageBox.Show("Unzip Created Successfully...");
                Response.Write("Unzip Created Successfully...");
            }
            catch (Exception ex)
            {
                //MessageBox.Show(Ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
                Response.Write(ex.Message.ToString());
            }
        }
    }
}

  

标签:files,Unzip,string,Zip,FileStream,System,GZipStream,new,using
来源: https://www.cnblogs.com/geovindu/p/16615748.html