• 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());
                }
            }
        }
    }
    

      

    大家閨秀的典雅,小家碧玉的清純,村姑的樸實,灰姑娘的癡情,山妞的野性,牧羊女的浪漫,修女的貞潔

    大家闺秀的典雅,小家碧玉的清纯,村姑的朴实,灰姑娘的痴情,山妞的野性,牧羊女的浪漫,修女的贞洁

  • 相关阅读:
    [原创]FineUI秘密花园(二十九) — 用户控件概述
    FineUI v3.2.4发布了!
    [原创]FineUI秘密花园(二十六) — 选项卡控件概述
    [原创]FineUI秘密花园(三十) — ViewState与XState (本系列文章最后一篇,兑现承诺,现提供完整PDF版下载!)
    FineUI 将不再内置 ExtJS (严格遵守 ExtJS 的开源规则)
    fineui.com 可以正常访问了
    [原创]22行JavaScript代码实现QQ群成员提取器,绿色、环保、无病毒!
    [原创]如何在FineUI中集成jQuery UI的AutoComplete组件
    《计算机程序的解释和构造》(SICP)学习2-对系统结构的两种不同的“世界观”
    报表开发最后一计使用iTextSharp来开发报表
  • 原文地址:https://www.cnblogs.com/geovindu/p/16615748.html
Copyright © 2020-2023  润新知