首页  ·  知识 ·  
Label
      编辑:  图片来源:网络

需要SharpZipLib引用,ASP.NET 在线压缩与在线解压缩源码:using System;  

002 using System.IO;  

003 using ICSharpCode.SharpZipLib.Checksums;  

004 using ICSharpCode.SharpZipLib.Zip;  

005 using ICSharpCode.SharpZipLib.Zip.Compression;  

006 using ICSharpCode.SharpZipLib.Zip.Compression.Streams;  

007   

008 /// <summary>  

009 /// 2005-7-9  

010 /// 垃圾猪  

011 /// ewebapp.net  

012 /// </summary>  

013 namespace eWebapp.Upload  

014 {  

015     /// <summary>  

016     /// 压缩解压处理。  

017     /// </summary>  

018     public class eZip  

019     {  

020         #region 私有域  

021         private string zipname = string.Empty;  

022         private string password = string.Empty;  

023         private int level = 9;  

024         private string unzipdir = string.Empty;  

025         private Crc32 crc;  

026         ZipOutputStream s;  

027         #endregion  

028   

029         #region 属性  

030   

031         /// <summary>  

032         /// 压缩文件名称  

033         /// </summary>  

034         public string Zipname  

035         {  

036             set{ zipname = value;}  

037         }  

038   

039         /// <summary>  

040         /// 解压文件夹  

041         /// </summary>  

042         public string Unzipdir  

043         {  

044             set { unzipdir = value;}  

045         }  

046         /// <summary>  

047         /// 压缩文件密码  

048         /// </summary>  

049         public string Password  

050         {  

051             set {password = value;}  

052         }  

053         /// <summary>  

054         /// 压缩级别  

055         /// </summary>  

056         public int Level  

057         {  

058             set {level = value;}  

059         }  

060   

061         public eZip()  

062         {  

063             //  

064             // TODO: 在此处添加构造函数逻辑  

065             //  

066         }  

067   

068         #endregion  

069   

070         /// <summary>  

071         /// 创建压缩文件  

072         /// </summary>  

073         public void CreateZip()  

074         {  

075             crc = new Crc32();  

076             s = new ZipOutputStream(File.Create(this.zipname));  

077             s.SetLevel(level);  

078             s.Password = password;  

079         }  

080         /// <summary>  

081         /// 结束操作  

082         /// </summary>  

083         public void FinishZip()  

084         {  

085             s.Finish();  

086             s.Close();  

087         }  

088   

089         /// <summary>  

090         /// 增加一个文件  

091         /// </summary>  

092         /// <param name="_crc"></param>  

093         /// <param name="_s"></param>  

094         /// <param name="_filename"></param>  

095         /// <param name="_filesize"></param>  

096         /// <param name="_buffer"></param>  

097         private void addFile(Crc32 _crc,ZipOutputStream _s,string _filename,long _filesize,byte[] _buffer)  

098         {  

099             // 添加一个文件到压缩文件中  

100             ZipEntry entry = new ZipEntry(_filename);  

101                

102             entry.DateTime = DateTime.Now;  

103   

104             entry.Size = _filesize;  

105   

106                

107             _crc.Reset();  

108             _crc.Update(_buffer);  

109                

110             entry.Crc  = _crc.Value;  

111                

112             _s.PutNextEntry(entry);  

113                

114             _s.Write(_buffer, 0, _buffer.Length);  

115   

116         }  

117   

118         /// <summary>  

119         /// 添加一个文件到压缩文件中  

120         /// </summary>  

121         /// <param name="_filename"></param>  

122         public void AddFile(string _filename)  

123         {  

124   

125             FileStream fs = File.OpenRead(_filename);  

126             byte[] buffer = new byte[fs.Length];  

127             fs.Read(buffer, 0, buffer.Length);  

128             string filename = System.IO.Path.GetFileName(_filename);  

129             long m_fLength = fs.Length;  

130             fs.Close();  

131             addFile(this.crc,this.s,filename,m_fLength,buffer);  

132         }  

133   

134         /// <summary>  

135         /// 新建文本文件  

136         /// </summary>  

137         /// <param name="_crc"></param>  

138         /// <param name="_s"></param>  

139         /// <param name="_filename"></param>  

140         /// <param name="_text"></param>  

141         private void addText(Crc32 _crc,ZipOutputStream _s,string _filename,string _text)  

142         {  

143             byte[] text = System.Text.Encoding.Default.GetBytes(_text);  

144             addFile(_crc,_s,_filename,text.Length,text);  

145         }  

146   

147         /// <summary>  

148         /// 添加一个指定内容的文本文件到压缩文件中  

149         /// </summary>  

150         /// <param name="_filename"></param>  

151         /// <param name="_text"></param>  

152         public void AddText(string _filename,string _text)  

153         {  

154             addText(this.crc,this.s,_filename,_text);  

155         }  

156   

157   

158         /// <summary>  

159         /// 解压处理  

160         /// </summary>  

161         public void Unzip()  

162         {  

163                        

164             string unzipdirectoryroot = this.unzipdir;  

165                

166                

167             // 调用组件中读取压缩文件的对象  

168             ZipInputStream s = new ZipInputStream(File.OpenRead(this.zipname));  

169             s.Password = this.password;  

170   

171             ZipEntry theEntry;  

172                

173   

174             while ((theEntry = s.GetNextEntry()) != null)  

175             {  

176                 // 当前解压到的目录,Path.GetDirectoryName(theEntry.Name)为提取压缩文件目录  

177                 string directoryName = Path.Combine(unzipdirectoryroot,Path.GetDirectoryName(theEntry.Name));  

178                 // 当前正在解压的文件  

179                 string fileName = Path.Combine(directoryName,Path.GetFileName(theEntry.Name));  

180                 // 创建目录  

181                 Directory.CreateDirectory(directoryName);  

182   

183                 if (fileName != String.Empty)   

184                 {  

185                     try 

186                     {  

187                         // 创建解压文件  

188                         FileStream streamWriter = File.Create(fileName);  

189                         // 将数据写入文件  

190                         int size = 2048;  

191                         byte[] data = new byte[2048];  

192                         while (true)   

193                         {  

194                             size = s.Read(data, 0, data.Length);  

195                             if (size > 0)   

196                             {  

197                                 streamWriter.Write(data, 0, size);  

198                             }   

199                             else  

200                             {  

201                                 break;  

202                             }  

203                         }  

204                    

205                         streamWriter.Close();  

206                     }  

207                     catch 

208                     {  

209                     }  

210                        

211                 }  

212             }  

213             s.Close();  

214         }  

215   

216     }  

217 }
 

 

本文作者:网友 来源: http://www.cnblogs.com/undefine/
CIO之家 www.ciozj.com 微信公众号:imciow
   
免责声明:本站转载此文章旨在分享信息,不代表对其内容的完全认同。文章来源已尽可能注明,若涉及版权问题,请及时与我们联系,我们将积极配合处理。同时,我们无法对文章内容的真实性、准确性及完整性进行完全保证,对于因文章内容而产生的任何后果,本账号不承担法律责任。转载仅出于传播目的,读者应自行对内容进行核实与判断。请谨慎参考文章信息,一切责任由读者自行承担。
延伸阅读
也许感兴趣的
我们推荐的
主题最新
看看其它的
收藏至微信