Csharp 利用ICSharpCode.SharpZipLib解壓文件
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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | /* *http://www.koders.com/csharp/fid7241B3C8598C20BA18652B5BB5C19D220988E2D4.aspx?s=file * http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx * http://www.eggheadcafe.com/tutorials/aspnet/9ce6c242-c14c-4969-9251-af95e4cf320f/zip--unzip-folders-and-f.aspx * http://www.eggheadcafe.com/community/csharp/2/10060149/zip-a-file-using-icsharpcodesharpziplibzip.aspx * http://www.eggheadcafe.com/tutorials/csharp/9ce6c242-c14c-4969-9251-af95e4cf320f/zip--unzip-folders-and-files-with-c.aspx * */ using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Management; using ICSharpCode.SharpZipLib.Checksums; using ICSharpCode.SharpZipLib.Zip; using ICSharpCode.SharpZipLib.GZip; using System.IO; using System.Collections; using System.Windows; namespace CompressFolders { /// <summary> /// 20120530 /// 涂聚文 Geovin Du /// </summary> public partial class Form1 : Form { string strBaseDir = "" ; /// <summary> /// /// </summary> public Form1() { InitializeComponent(); } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Form1_Load( object sender, EventArgs e) { //指定文件 //this.saveFileDialog1.Filter = "压缩文件(zip)|*.zip"; //this.saveFileDialog1.Title = "保存文件"; //this.saveFileDialog1.FileName = "数据备份"; //this.saveFileDialog1.InitialDirectory =Application.StartupPath; //this.saveFileDialog1.RestoreDirectory = true; //string Fname = ""; //if (this.saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) //{ // string[] filenames ={ "Properties", "Form1.resx", "frmMain.Designer.cs", "Program.cs", "Form1.Designer.cs", "Form1.resx", "frmMain.cs" };//可以自己定义所压缩的文件 // Fname = this.saveFileDialog1.FileName; // ZipOutputStream s = new ZipOutputStream(File.Create(Fname));//创建压缩文件的目录及名称 // s.Password = "geovindu";//设置压缩文件的密码 // s.SetLevel(6);// 0 - store only to 9 - means best compression // foreach (string file in filenames) // { // if (Directory.Exists(Application.StartupPath + @"/" + file)) // { // strBaseDir = Application.StartupPath + @"/" + file + @"/";//压缩文件夹 // addZipEntry(strBaseDir, s); // } // else // { // FileStream fs = File.OpenRead(Application.StartupPath + @"/" + file);//打开文件读取 // byte[] buffer = new byte[fs.Length]; // fs.Read(buffer, 0, buffer.Length); // string strEntryName = file; // ZipEntry entry = new ZipEntry(strEntryName); // s.PutNextEntry(entry); // s.Write(buffer, 0, buffer.Length); // fs.Close(); // } // } // s.Finish(); // s.Close(); //} } /// <summary> /// 壓縮文件夾 /// </summary> /// <param name="PathStr"></param> /// <param name="s"></param> private void addZipEntry( string PathStr, ZipOutputStream s) { DirectoryInfo di = new DirectoryInfo(PathStr); foreach (DirectoryInfo item in di.GetDirectories()) { addZipEntry(item.FullName, s); } foreach (FileInfo item in di.GetFiles()) { if (item.FullName.Replace(strBaseDir, "" ) != "Thumbs.db" ) { FileStream fs = File.OpenRead(item.FullName); byte [] buffer = new byte [fs.Length]; fs.Read(buffer, 0, buffer.Length); string strEntryName = item.FullName.Replace(strBaseDir, "" ); strEntryName = @"Properties/" + strEntryName; ZipEntry entry = new ZipEntry(strEntryName); s.PutNextEntry(entry); s.Write(buffer, 0, buffer.Length); fs.Close(); } } } /// <summary> /// 解压缩 /// </summary> /// <param name="zipfile"></param> /// <param name="directory"></param> private void Unzip( string zipfile, string directory) { if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } ZipInputStream zis = new ZipInputStream(File.OpenRead(zipfile)); string str = zis.Password; zis.Password = "塗聚文" ; ZipEntry theEntry = null ; while ((theEntry = zis.GetNextEntry()) != null ) { ZipConstants.DefaultCodePage = 936; string directoryName = Path.GetDirectoryName(theEntry.Name); //directoryName=null string fileName = Path.GetFileName(theEntry.Name); if (directoryName != string .Empty) { if ((directoryName == "Properties" ) && (!Directory.Exists(directory + @"/" + directoryName))) { Directory.CreateDirectory(directory + @"/" + directoryName); } } if (fileName != string .Empty) { FileStream streamWriter = File.Create(Path.Combine(directory, theEntry.Name)); int size = 2048; byte [] data = new byte [2048]; while ( true ) { size = zis.Read(data, 0, data.Length); if (size > 0) { streamWriter.Write(data, 0, size); } else { break ; } } streamWriter.Close(); } } zis.Close(); } /// <summary> ///壓縮 100M大文件進程不行 /// 塗聚文 Geovin Du /// </summary> /// <param name="inputFolderPath"></param> /// <param name="outputPathAndFile"></param> /// <param name="password"></param> public static void ZipFiles( string inputFolderPath, string outputPathAndFile, string password) { ArrayList ar = GenerateFileList(inputFolderPath); // generate file list int TrimLength = (Directory.GetParent(inputFolderPath)).ToString().Length; // find number of chars to remove // from orginal file path TrimLength += 1; //remove '\' FileStream ostream; byte [] obuffer; string outPath = inputFolderPath + @"\" + outputPathAndFile; ZipOutputStream oZipStream = new ZipOutputStream(File.Create(outPath)); // create zip stream if (password != null && password != String.Empty) oZipStream.Password = password; oZipStream.SetLevel(9); // maximum compression ZipEntry oZipEntry; foreach ( string Fil in ar) // for each file, generate a zipentry { oZipEntry = new ZipEntry(Fil.Remove(0, TrimLength)); oZipStream.PutNextEntry(oZipEntry); if (!Fil.EndsWith( @"/" )) // if a file ends with '/' its a directory { ostream = File.OpenRead(Fil); obuffer = new byte [ostream.Length]; ostream.Read(obuffer, 0, obuffer.Length); oZipStream.Write(obuffer, 0, obuffer.Length); } } oZipStream.Finish(); oZipStream.Close(); } /// <summary> /// /// </summary> /// <param name="Dir"></param> /// <returns></returns> public static ArrayList GenerateFileList( string Dir) { ArrayList fils = new ArrayList(); bool Empty = true ; foreach ( string file in Directory.GetFiles(Dir)) // add each file in directory { fils.Add(file); Empty = false ; } if (Empty) { if (Directory.GetDirectories(Dir).Length == 0) // if directory is completely empty, add it { fils.Add(Dir + @"/" ); } } foreach ( string dirs in Directory.GetDirectories(Dir)) // recursive { foreach ( object obj in GenerateFileList(dirs)) { fils.Add(obj); } } return fils; // return file list } /// <summary> /// 打開選擇文件夾 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnOpen_Click( object sender, EventArgs e) { FolderBrowserDialog fbd = new FolderBrowserDialog(); if (fbd.ShowDialog() == DialogResult.OK) { //MessageBox.Show(fbd.SelectedPath); this .textBox1.Text = fbd.SelectedPath; } } /// <summary> /// 保存 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void buttonSave_Click( object sender, EventArgs e) { ZipFiles( this .textBox1.Text.Trim(), this .textBox2.Text.Trim(), "geovindu" ); } /// <summary> /// 選擇文件夾 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click( object sender, EventArgs e) { FolderBrowserDialog fbd = new FolderBrowserDialog(); if (fbd.ShowDialog() == DialogResult.OK) { //MessageBox.Show(fbd.SelectedPath); this .textBox2.Text = fbd.SelectedPath; } } } } |
還可以用其他方法壓縮或解壓方法:
可以引用:
1 2 3 4 5 6 7 8 | using System.IO; using System.IO.Compression; // 此方法也可以壓縮或解壓,GZipStream() 不能用第三方解壓 using Shell32; //Microsoft Shell Controls And Automation //此方法也是可以 using System.Threading; using System.Runtime.InteropServices; //using java.io;//此方法也是可以 //using java.util;//WINDOWS\Microsoft.NET\Framework\v2.0.50727\vjslib.dll //using java.util.zip; |
以上都寫過測試程序通過(2012-08-25)。Shell大文件夾也可以。
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
2011-05-30 C# 逻辑运算符