csharp: Zip and Unzip files using GZipStream

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
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());
            }
        }
    }
}

  

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

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

posted @   ®Geovin Du Dream Park™  阅读(19)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2017-08-23 web print
2009-08-23 css 表格和层的虚线的用法(兼容各浏览器IE8可以)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示